207 lines
5.6 KiB
TypeScript
207 lines
5.6 KiB
TypeScript
import { OrganizationMapper } from '../../../../src/Application/DTOs/Mappers/OrganizationMapper';
|
|
import { OrganizationState, OrganizationStateType } from '../../../../src/Domain/Organization/OrganizationAggregate';
|
|
|
|
describe('OrganizationMapper', () => {
|
|
const createMockOrganization = (overrides: any = {}) => ({
|
|
id: 'org-123',
|
|
name: 'Test Organization',
|
|
contactfname: 'John',
|
|
contactlname: 'Doe',
|
|
contactphone: '+1234567890',
|
|
contactemail: 'john@test.org',
|
|
state: OrganizationState.ACTIVE as OrganizationStateType,
|
|
regdate: new Date('2024-01-01'),
|
|
updatedate: new Date('2024-01-02'),
|
|
url: 'https://test.org',
|
|
userinorg: 5,
|
|
users: [
|
|
{ id: 'user-1', name: 'User One' },
|
|
{ id: 'user-2', name: 'User Two' }
|
|
],
|
|
...overrides
|
|
});
|
|
|
|
describe('toShortDto', () => {
|
|
it('should map OrganizationAggregate to ShortOrganizationDto correctly', () => {
|
|
// Arrange
|
|
const org = createMockOrganization();
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDto(org);
|
|
|
|
// Assert
|
|
expect(result).toEqual({
|
|
id: 'org-123',
|
|
name: 'Test Organization',
|
|
state: OrganizationState.ACTIVE,
|
|
userinorg: 5
|
|
});
|
|
});
|
|
|
|
it('should handle different organization states', () => {
|
|
// Arrange
|
|
const registeredOrg = createMockOrganization({
|
|
state: OrganizationState.REGISTERED,
|
|
userinorg: 0
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDto(registeredOrg);
|
|
|
|
// Assert
|
|
expect(result.state).toBe(OrganizationState.REGISTERED);
|
|
expect(result.userinorg).toBe(0);
|
|
});
|
|
|
|
it('should handle organization with many users', () => {
|
|
// Arrange
|
|
const orgWithManyUsers = createMockOrganization({
|
|
userinorg: 100
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDto(orgWithManyUsers);
|
|
|
|
// Assert
|
|
expect(result.userinorg).toBe(100);
|
|
});
|
|
});
|
|
|
|
describe('toDetailDto', () => {
|
|
it('should map OrganizationAggregate to DetailOrganizationDto correctly', () => {
|
|
// Arrange
|
|
const org = createMockOrganization();
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toDetailDto(org);
|
|
|
|
// Assert
|
|
expect(result).toEqual({
|
|
id: 'org-123',
|
|
name: 'Test Organization',
|
|
contactfname: 'John',
|
|
contactlname: 'Doe',
|
|
contactphone: '+1234567890',
|
|
contactemail: 'john@test.org',
|
|
state: OrganizationState.ACTIVE,
|
|
regdate: new Date('2024-01-01'),
|
|
updatedate: new Date('2024-01-02'),
|
|
url: 'https://test.org',
|
|
userinorg: 5,
|
|
users: ['user-1', 'user-2']
|
|
});
|
|
});
|
|
|
|
it('should handle organization without URL', () => {
|
|
// Arrange
|
|
const orgWithoutUrl = createMockOrganization({
|
|
url: null
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toDetailDto(orgWithoutUrl);
|
|
|
|
// Assert
|
|
expect(result.url).toBeNull();
|
|
});
|
|
|
|
it('should handle organization without users', () => {
|
|
// Arrange
|
|
const orgWithoutUsers = createMockOrganization({
|
|
users: null,
|
|
userinorg: 0
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toDetailDto(orgWithoutUsers);
|
|
|
|
// Assert
|
|
expect(result.users).toEqual([]);
|
|
expect(result.userinorg).toBe(0);
|
|
});
|
|
|
|
it('should handle empty users array', () => {
|
|
// Arrange
|
|
const orgWithEmptyUsers = createMockOrganization({
|
|
users: [],
|
|
userinorg: 0
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toDetailDto(orgWithEmptyUsers);
|
|
|
|
// Assert
|
|
expect(result.users).toEqual([]);
|
|
});
|
|
|
|
it('should handle soft deleted organization', () => {
|
|
// Arrange
|
|
const softDeletedOrg = createMockOrganization({
|
|
state: OrganizationState.SOFT_DELETE
|
|
});
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toDetailDto(softDeletedOrg);
|
|
|
|
// Assert
|
|
expect(result.state).toBe(OrganizationState.SOFT_DELETE);
|
|
});
|
|
});
|
|
|
|
describe('toShortDtoList', () => {
|
|
it('should map array of OrganizationAggregate to array of ShortOrganizationDto', () => {
|
|
// Arrange
|
|
const orgs = [
|
|
createMockOrganization({ id: 'org-1', name: 'First Org', userinorg: 10 }),
|
|
createMockOrganization({ id: 'org-2', name: 'Second Org', state: OrganizationState.REGISTERED }),
|
|
createMockOrganization({ id: 'org-3', name: 'Third Org', userinorg: 0 })
|
|
];
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDtoList(orgs);
|
|
|
|
// Assert
|
|
expect(result).toHaveLength(3);
|
|
expect(result[0]).toEqual({
|
|
id: 'org-1',
|
|
name: 'First Org',
|
|
state: OrganizationState.ACTIVE,
|
|
userinorg: 10
|
|
});
|
|
expect(result[1].state).toBe(OrganizationState.REGISTERED);
|
|
expect(result[2].userinorg).toBe(0);
|
|
});
|
|
|
|
it('should handle empty array', () => {
|
|
// Arrange
|
|
const orgs: any[] = [];
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDtoList(orgs);
|
|
|
|
// Assert
|
|
expect(result).toEqual([]);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('should handle large arrays', () => {
|
|
// Arrange
|
|
const orgs = Array.from({ length: 25 }, (_, i) =>
|
|
createMockOrganization({
|
|
id: `org-${i + 1}`,
|
|
name: `Organization ${i + 1}`,
|
|
userinorg: i * 2
|
|
})
|
|
);
|
|
|
|
// Act
|
|
const result = OrganizationMapper.toShortDtoList(orgs);
|
|
|
|
// Assert
|
|
expect(result).toHaveLength(25);
|
|
expect(result[0].userinorg).toBe(0);
|
|
expect(result[24].userinorg).toBe(48);
|
|
});
|
|
});
|
|
});
|