authentication mapper tests

This commit is contained in:
sbriat 2023-07-07 10:32:35 +02:00
parent e123f190c6
commit 8e1bfe68d5
2 changed files with 83 additions and 0 deletions

View File

@ -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>(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');
});
});

View File

@ -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 () => {