import { Test, TestingModule } from '@nestjs/testing'; import { CreateUserUseCase } from '../src/application/use-cases/create-user.use-case'; import { IUserRepository } from '../src/application/interfaces/i-user.repository'; import { IHasherService } from '../src/application/interfaces/i-hasher.service'; import { UserRole } from '../src/domain/entities/user.entity'; describe('CreateUserUseCase', () => { let useCase: CreateUserUseCase; let userRepository: IUserRepository; let hasherService: IHasherService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ CreateUserUseCase, { provide: 'IUserRepository', useValue: { create: jest.fn(), findByEmail: jest.fn(), }, }, { provide: 'IHasherService', useValue: { hash: jest.fn(), }, }, ], }).compile(); useCase = module.get(CreateUserUseCase); userRepository = module.get('IUserRepository'); hasherService = module.get('IHasherService'); }); it('should be defined', () => { expect(useCase).toBeDefined(); }); it('should create a user successfully', async () => { const createUserDto = { email: 'test@test.com', password: 'password123', name: 'Test User', otherName: 'Other Name', address: 'Test Address', telephone: '1234567890', }; const hashedPassword = 'hashedPassword123'; const createdUser = { ...createUserDto, id: 1, password: hashedPassword, role: UserRole.USER }; jest.spyOn(hasherService, 'hash').mockResolvedValue(hashedPassword); jest.spyOn(userRepository, 'create').mockResolvedValue(createdUser); const result = await useCase.execute(createUserDto); expect(result).toEqual(createdUser); expect(hasherService.hash).toHaveBeenCalledWith(createUserDto.password); expect(userRepository.create).toHaveBeenCalledWith({ ...createUserDto, password: hashedPassword, }); }); });