repository tests
This commit is contained in:
parent
805a7fe24d
commit
55c4367702
|
@ -2,13 +2,37 @@ import { UsernameMapper } from '@modules/authentication/username.mapper';
|
||||||
import { PrismaService } from '@modules/authentication/infrastructure/prisma.service';
|
import { PrismaService } from '@modules/authentication/infrastructure/prisma.service';
|
||||||
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
|
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { UsernameRepository } from '@modules/authentication/infrastructure/username.repository';
|
import {
|
||||||
|
UsernameModel,
|
||||||
|
UsernameRepository,
|
||||||
|
} from '@modules/authentication/infrastructure/username.repository';
|
||||||
|
import { Type } from '@modules/authentication/core/domain/username.types';
|
||||||
|
import { UsernameEntity } from '@modules/authentication/core/domain/username.entity';
|
||||||
|
|
||||||
|
const mockPrismaService = {
|
||||||
|
username: {
|
||||||
|
findFirst: jest.fn().mockImplementation(async () => {
|
||||||
|
const now = new Date('2023-06-21 06:00:00');
|
||||||
|
const record: UsernameModel = {
|
||||||
|
authUuid: '330bd6de-1eb8-450b-8674-0e3c9209f048',
|
||||||
|
type: Type.EMAIL,
|
||||||
|
username: 'john.doe@email.com',
|
||||||
|
createdAt: now,
|
||||||
|
updatedAt: now,
|
||||||
|
};
|
||||||
|
return record;
|
||||||
|
}),
|
||||||
|
|
||||||
|
update: jest.fn().mockImplementation(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const mockMessagePublisher = {
|
const mockMessagePublisher = {
|
||||||
publish: jest.fn().mockImplementation(),
|
publish: jest.fn().mockImplementation(),
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('Username repository', () => {
|
describe('Username repository', () => {
|
||||||
|
let usernameRepository: UsernameRepository;
|
||||||
let prismaService: PrismaService;
|
let prismaService: PrismaService;
|
||||||
let usernameMapper: UsernameMapper;
|
let usernameMapper: UsernameMapper;
|
||||||
let eventEmitter: EventEmitter2;
|
let eventEmitter: EventEmitter2;
|
||||||
|
@ -16,21 +40,58 @@ describe('Username repository', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
imports: [EventEmitterModule.forRoot()],
|
imports: [EventEmitterModule.forRoot()],
|
||||||
providers: [PrismaService, UsernameMapper],
|
providers: [
|
||||||
|
{
|
||||||
|
provide: PrismaService,
|
||||||
|
useValue: mockPrismaService,
|
||||||
|
},
|
||||||
|
UsernameMapper,
|
||||||
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
prismaService = module.get<PrismaService>(PrismaService);
|
prismaService = module.get<PrismaService>(PrismaService);
|
||||||
usernameMapper = module.get<UsernameMapper>(UsernameMapper);
|
usernameMapper = module.get<UsernameMapper>(UsernameMapper);
|
||||||
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
|
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
|
||||||
});
|
usernameRepository = new UsernameRepository(
|
||||||
it('should be defined', () => {
|
|
||||||
expect(
|
|
||||||
new UsernameRepository(
|
|
||||||
prismaService,
|
prismaService,
|
||||||
usernameMapper,
|
usernameMapper,
|
||||||
eventEmitter,
|
eventEmitter,
|
||||||
mockMessagePublisher,
|
mockMessagePublisher,
|
||||||
),
|
);
|
||||||
).toBeDefined();
|
});
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(usernameRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
it('should find a username by its userId and Type', async () => {
|
||||||
|
jest.spyOn(usernameRepository, 'findOne');
|
||||||
|
const username: UsernameEntity = await usernameRepository.findByType(
|
||||||
|
'330bd6de-1eb8-450b-8674-0e3c9209f048',
|
||||||
|
Type.EMAIL,
|
||||||
|
);
|
||||||
|
expect(usernameRepository.findOne).toHaveBeenCalledTimes(1);
|
||||||
|
expect(usernameRepository.findOne).toHaveBeenCalledWith({
|
||||||
|
authUuid: '330bd6de-1eb8-450b-8674-0e3c9209f048',
|
||||||
|
type: Type.EMAIL,
|
||||||
|
});
|
||||||
|
expect(username.getProps().name).toBe('john.doe@email.com');
|
||||||
|
});
|
||||||
|
it('should update a username', async () => {
|
||||||
|
jest.spyOn(usernameRepository, 'updateWhere');
|
||||||
|
const usernameToUpdate: UsernameEntity = await UsernameEntity.create({
|
||||||
|
userId: '165192d4-398a-4469-a16b-98c02cc6f531',
|
||||||
|
type: Type.EMAIL,
|
||||||
|
name: 'john.doe@new-email.com',
|
||||||
|
});
|
||||||
|
await usernameRepository.updateUsername(
|
||||||
|
'john.doe@email.com',
|
||||||
|
usernameToUpdate,
|
||||||
|
);
|
||||||
|
expect(usernameRepository.updateWhere).toHaveBeenCalledTimes(1);
|
||||||
|
expect(usernameRepository.updateWhere).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
username: 'john.doe@email.com',
|
||||||
|
},
|
||||||
|
usernameToUpdate,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue