diff --git a/src/modules/authentication/tests/unit/authentication.mapper.spec.ts b/src/modules/authentication/tests/unit/authentication.mapper.spec.ts new file mode 100644 index 0000000..02046c8 --- /dev/null +++ b/src/modules/authentication/tests/unit/authentication.mapper.spec.ts @@ -0,0 +1,82 @@ +import { AuthenticationMapper } from '@modules/authentication/authentication.mapper'; +import { AuthenticationEntity } from '@modules/authentication/core/domain/authentication.entity'; +import { Type } from '@modules/authentication/core/domain/username.types'; +import { + AuthenticationReadModel, + AuthenticationWriteModel, +} from '@modules/authentication/infrastructure/authentication.repository'; +import { AuthenticationResponseDto } from '@modules/authentication/interface/dtos/authentication.response.dto'; +import { Test } from '@nestjs/testing'; + +const now = new Date('2023-06-21 06:00:00'); +const authenticationEntity: AuthenticationEntity = new AuthenticationEntity({ + id: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + props: { + userId: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + password: 'somePassword', + usernames: [ + { + name: 'john.doe@email.com', + type: Type.EMAIL, + }, + { + name: '+33611223344', + type: Type.PHONE, + }, + ], + }, + createdAt: now, + updatedAt: now, +}); +const authenticationReadModel: AuthenticationReadModel = { + uuid: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + password: '$2b$10$mpkLb3LGyQGJoPaWNBAGReSQHflXPWMjw5RXpebz8EVIMfHJYZ0Fu', + usernames: [ + { + username: 'john.doe@email.com', + type: Type.EMAIL, + }, + { + username: '+33611223344', + type: Type.PHONE, + }, + ], + createdAt: now, + updatedAt: now, +}; + +describe('Authentication Mapper', () => { + let authenticationMapper: AuthenticationMapper; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + providers: [AuthenticationMapper], + }).compile(); + authenticationMapper = + module.get(AuthenticationMapper); + }); + + it('should be defined', () => { + expect(authenticationMapper).toBeDefined(); + }); + + it('should map domain entity to persistence data', async () => { + const mapped: AuthenticationWriteModel = + authenticationMapper.toPersistence(authenticationEntity); + expect(mapped.usernames.create[0].username).toBe('john.doe@email.com'); + expect(mapped.usernames.create[1].username).toBe('+33611223344'); + }); + + it('should map persisted data to domain entity', async () => { + const mapped: AuthenticationEntity = authenticationMapper.toDomain( + authenticationReadModel, + ); + expect(mapped.getProps().usernames[1].name).toBe('+33611223344'); + }); + + it('should map domain entity to response', async () => { + const mapped: AuthenticationResponseDto = + authenticationMapper.toResponse(authenticationEntity); + expect(mapped.id).toBe('7ca2490b-d04d-4ac5-8d6c-5c416fab922e'); + }); +}); diff --git a/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts b/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts index e4e2655..5f3de63 100644 --- a/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts +++ b/src/modules/authentication/tests/unit/core/authentication.entity.spec.ts @@ -36,6 +36,7 @@ describe('Authentication entity create', () => { expect(authenticationEntity.id).toBe( '165192d4-398a-4469-a16b-98c02cc6f531', ); + expect(authenticationEntity.getProps().password.length).toBe(60); expect(authenticationEntity.domainEvents.length).toBe(1); }); it('should create a new authentication entity with 2 usernames', async () => {