import { NotFoundException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { FindUserByUuidRequest } from '../../domain/dtos/find-user-by-uuid.request'; import { FindUserByUuidUseCase } from '../../domain/usecases/find-user-by-uuid.usecase'; import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query'; import { MESSAGE_PUBLISHER } from '../../../../app.constants'; const mockUser = { uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', firstName: 'John', lastName: 'Doe', email: 'john.doe@email.com', phone: '0601020304', }; const mockUserRepository = { findOneByUuid: jest .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((query?: FindUserByUuidQuery) => { return Promise.resolve(mockUser); }) .mockImplementation(() => { return Promise.resolve(undefined); }), }; const mockMessagePublisher = { publish: jest.fn().mockImplementation(), }; describe('FindUserByUuidUseCase', () => { let findUserByUuidUseCase: FindUserByUuidUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [], providers: [ { provide: UsersRepository, useValue: mockUserRepository, }, { provide: MESSAGE_PUBLISHER, useValue: mockMessagePublisher, }, FindUserByUuidUseCase, ], }).compile(); findUserByUuidUseCase = module.get( FindUserByUuidUseCase, ); }); it('should be defined', () => { expect(findUserByUuidUseCase).toBeDefined(); }); describe('execute', () => { it('should return a user', async () => { const findUserByUuidRequest: FindUserByUuidRequest = new FindUserByUuidRequest(); findUserByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91'; const user = await findUserByUuidUseCase.execute( new FindUserByUuidQuery(findUserByUuidRequest), ); expect(user).toBe(mockUser); }); it('should throw an error if user does not exist', async () => { const findUserByUuidRequest: FindUserByUuidRequest = new FindUserByUuidRequest(); findUserByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90'; await expect( findUserByUuidUseCase.execute( new FindUserByUuidQuery(findUserByUuidRequest), ), ).rejects.toBeInstanceOf(NotFoundException); }); }); });