user.controller.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import { UserController } from '../src/presentation/controllers/user.controller';
  3. import { CreateUserUseCase } from '../src/application/use-cases/create-user.use-case';
  4. describe('UserController', () => {
  5. let controller: UserController;
  6. let createUserUseCase: CreateUserUseCase;
  7. beforeEach(async () => {
  8. const module: TestingModule = await Test.createTestingModule({
  9. controllers: [UserController],
  10. providers: [
  11. {
  12. provide: CreateUserUseCase,
  13. useValue: {
  14. execute: jest.fn(),
  15. },
  16. },
  17. ],
  18. }).compile();
  19. controller = module.get<UserController>(UserController);
  20. createUserUseCase = module.get<CreateUserUseCase>(CreateUserUseCase);
  21. });
  22. it('should be defined', () => {
  23. expect(controller).toBeDefined();
  24. });
  25. describe('create', () => {
  26. it('should create a new user', async () => {
  27. const createUserDto = {
  28. email: 'test@test.com',
  29. password: 'password123',
  30. name: 'Test User',
  31. otherName: 'Other Name',
  32. address: 'Test Address',
  33. telephone: '1234567890',
  34. };
  35. const expectedResult = {
  36. id: 1,
  37. ...createUserDto,
  38. };
  39. jest.spyOn(createUserUseCase, 'execute').mockResolvedValue(expectedResult);
  40. const result = await controller.create(createUserDto);
  41. expect(result).toEqual(expectedResult);
  42. expect(createUserUseCase.execute).toHaveBeenCalledWith(createUserDto);
  43. });
  44. });
  45. });