|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing';
|
2 | 2 | import { AuthEmailsController } from './auth-emails.controller';
|
| 3 | +import { AuthEmailsService } from './auth-emails.service'; |
| 4 | +import { mockUserRequest } from '../mocks/mock-user-request'; |
| 5 | +import { Types } from 'mongoose'; |
| 6 | +import { IResetPasswordDto } from 'shared-types'; |
3 | 7 |
|
4 | 8 | describe('AuthEmailsController', () => {
|
5 | 9 | let controller: AuthEmailsController;
|
6 | 10 |
|
| 11 | + const mockAuthEmailsService = { |
| 12 | + sendEmailConfirmation: jest.fn(), |
| 13 | + sendPasswordResetEmail: jest.fn(), |
| 14 | + confirmEmailWithToken: jest.fn(), |
| 15 | + resetPasswordWithToken: jest.fn(), |
| 16 | + }; |
| 17 | + |
| 18 | + const sendEmailConfirmationSpy = jest.spyOn(mockAuthEmailsService, 'sendEmailConfirmation'); |
| 19 | + const sendPasswordResetEmailSpy = jest.spyOn(mockAuthEmailsService, 'sendPasswordResetEmail'); |
| 20 | + const confirmEmailWithTokenSpy = jest.spyOn(mockAuthEmailsService, 'confirmEmailWithToken'); |
| 21 | + const resetPasswordWithTokenSpy = jest.spyOn(mockAuthEmailsService, 'resetPasswordWithToken'); |
| 22 | + |
7 | 23 | beforeEach(async () => {
|
8 | 24 | const module: TestingModule = await Test.createTestingModule({
|
9 | 25 | controllers: [AuthEmailsController],
|
10 |
| - }).compile(); |
| 26 | + providers: [AuthEmailsService], |
| 27 | + }) |
| 28 | + .overrideProvider(AuthEmailsService) |
| 29 | + .useValue(mockAuthEmailsService) |
| 30 | + .compile(); |
11 | 31 |
|
12 | 32 | controller = module.get<AuthEmailsController>(AuthEmailsController);
|
13 | 33 | });
|
14 | 34 |
|
| 35 | + afterEach(() => { |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
15 | 39 | it('should be defined', () => {
|
16 | 40 | expect(controller).toBeDefined();
|
17 | 41 | });
|
| 42 | + |
| 43 | + it('should start email confirmation', async () => { |
| 44 | + await controller.startEmailConfirmation(mockUserRequest); |
| 45 | + expect(sendEmailConfirmationSpy).toBeCalledWith(expect.any(Types.ObjectId)); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should confirm email', async () => { |
| 49 | + const userId = new Types.ObjectId(); |
| 50 | + await controller.confirmEmail(userId, 'TOKEN'); |
| 51 | + expect(confirmEmailWithTokenSpy).toBeCalledWith(userId, 'TOKEN'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should password reset', async () => { |
| 55 | + await controller.startPasswordReset('[email protected]'); |
| 56 | + expect(sendPasswordResetEmailSpy).toBeCalledWith('[email protected]'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should reset password', async () => { |
| 60 | + const userId = new Types.ObjectId(); |
| 61 | + const dto: IResetPasswordDto = { |
| 62 | + user: userId.toString(), |
| 63 | + token: 'TOKEN', |
| 64 | + password: 'password', |
| 65 | + }; |
| 66 | + |
| 67 | + await controller.resetPassword(dto); |
| 68 | + |
| 69 | + expect(resetPasswordWithTokenSpy).toBeCalledWith(userId, 'TOKEN', 'password'); |
| 70 | + }); |
18 | 71 | });
|
0 commit comments