import { classes } from '@automapper/classes'; import { AutomapperModule } from '@automapper/nestjs'; import { Test, TestingModule } from '@nestjs/testing'; import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository'; import { LoggingMessager } from '../../adapters/secondaries/logging.messager'; import { UsernameRepository } from '../../adapters/secondaries/username.repository'; import { DeleteAuthenticationCommand } from '../../commands/delete-authentication.command'; import { DeleteAuthenticationRequest } from '../../domain/dtos/delete-authentication.request'; import { Type } from '../../domain/dtos/type.enum'; import { DeleteAuthenticationUseCase } from '../../domain/usecases/delete-authentication.usecase'; const usernames = { data: [ { uuid: 'cf76af29-f75d-4f6e-bb40-4ecbcfa3356e', username: 'john.doe@email.com', type: Type.EMAIL, }, { uuid: 'cf76af29-f75d-4f6e-bb40-4ecbcfa3356e', username: '0611223344', type: Type.PHONE, }, ], total: 2, }; const deleteAuthenticationRequest: DeleteAuthenticationRequest = new DeleteAuthenticationRequest(); deleteAuthenticationRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91'; const deleteAuthenticationCommand: DeleteAuthenticationCommand = new DeleteAuthenticationCommand(deleteAuthenticationRequest); const mockAuthenticationRepository = { delete: jest .fn() .mockResolvedValueOnce(undefined) .mockImplementation(() => { throw new Error('Error'); }), }; const mockUsernameRepository = { // eslint-disable-next-line @typescript-eslint/no-unused-vars findAll: jest.fn().mockImplementation((page, perPage, query) => { return Promise.resolve(usernames); }), delete: jest.fn().mockResolvedValue(undefined), deleteMany: jest.fn().mockResolvedValue(undefined), }; const mockMessager = { publish: jest.fn().mockImplementation(), }; describe('DeleteAuthenticationUseCase', () => { let deleteAuthenticationUseCase: DeleteAuthenticationUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })], providers: [ { provide: AuthenticationRepository, useValue: mockAuthenticationRepository, }, { provide: UsernameRepository, useValue: mockUsernameRepository, }, { provide: LoggingMessager, useValue: mockMessager, }, DeleteAuthenticationUseCase, ], }).compile(); deleteAuthenticationUseCase = module.get( DeleteAuthenticationUseCase, ); }); it('should be defined', () => { expect(deleteAuthenticationUseCase).toBeDefined(); }); describe('execute', () => { it('should delete an authentication and its usernames', async () => { const deletedAuthentication = await deleteAuthenticationUseCase.execute( deleteAuthenticationCommand, ); expect(deletedAuthentication).toBe(undefined); }); it('should throw an error if authentication does not exist', async () => { await expect( deleteAuthenticationUseCase.execute(deleteAuthenticationCommand), ).rejects.toBeInstanceOf(Error); }); }); });