import { classes } from '@automapper/classes'; import { AutomapperModule } from '@automapper/nestjs'; import { UnauthorizedException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { Messager } from '../../adapters/secondaries/messager'; import { UsernameRepository } from '../../adapters/secondaries/username.repository'; import { DeleteUsernameCommand } from '../../commands/delete-username.command'; import { DeleteUsernameRequest } from '../../domain/dtos/delete-username.request'; import { Type } from '../../domain/dtos/type.enum'; import { DeleteUsernameUseCase } from '../../domain/usecases/delete-username.usecase'; const usernamesEmail = { 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 usernamesPhone = { data: [ { uuid: 'a7fa221f-dd77-481c-bb77-ae89da662c87', username: '0611223344', type: Type.PHONE, }, ], total: 1, }; const deleteUsernameEmailRequest: DeleteUsernameRequest = new DeleteUsernameRequest(); deleteUsernameEmailRequest.username = 'john.doe@email.com'; const deleteUsernamePhoneRequest: DeleteUsernameRequest = new DeleteUsernameRequest(); deleteUsernamePhoneRequest.username = '0611223344'; const deleteUsernameEmailCommand: DeleteUsernameCommand = new DeleteUsernameCommand(deleteUsernameEmailRequest); const deleteUsernamePhoneCommand: DeleteUsernameCommand = new DeleteUsernameCommand(deleteUsernamePhoneRequest); const mockUsernameRepository = { findOne: jest.fn().mockImplementation((where) => { if (where.username == 'john.doe@email.com') { return { uuid: 'cf76af29-f75d-4f6e-bb40-4ecbcfa3356e' }; } return { uuid: 'a7fa221f-dd77-481c-bb77-ae89da662c87' }; }), findAll: jest.fn().mockImplementation((page, perPage, query) => { if (query.uuid == 'cf76af29-f75d-4f6e-bb40-4ecbcfa3356e') { return Promise.resolve(usernamesEmail); } return Promise.resolve(usernamesPhone); }), deleteMany: jest.fn().mockResolvedValue(undefined), }; const mockMessager = { publish: jest.fn().mockImplementation(), }; describe('DeleteUsernameUseCase', () => { let deleteUsernameUseCase: DeleteUsernameUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })], providers: [ { provide: UsernameRepository, useValue: mockUsernameRepository, }, { provide: Messager, useValue: mockMessager, }, DeleteUsernameUseCase, ], }).compile(); deleteUsernameUseCase = module.get( DeleteUsernameUseCase, ); }); it('should be defined', () => { expect(deleteUsernameUseCase).toBeDefined(); }); describe('execute', () => { it('should delete a username', async () => { const deletedEmailUsername = await deleteUsernameUseCase.execute( deleteUsernameEmailCommand, ); expect(deletedEmailUsername).toBe(undefined); }); it('should throw an exception if auth has only one username', async () => { await expect( deleteUsernameUseCase.execute(deleteUsernamePhoneCommand), ).rejects.toBeInstanceOf(UnauthorizedException); }); }); });