| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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';
- 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>(CreateUserUseCase);
- userRepository = module.get<IUserRepository>(IUserRepository);
- hasherService = module.get<IHasherService>(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 };
- 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,
- });
- });
- });
|