auth/src/modules/authentication/tests/unit/authentication.mapper.spec.ts

88 lines
2.7 KiB
TypeScript

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,
UsernameModel,
} 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 as { create: UsernameModel[] }).create[0].username,
).toBe('john.doe@email.com');
expect(
(mapped.usernames as { create: UsernameModel[] }).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');
});
});