|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { NotificationController } from '../../src/notification/presentation/controllers/notification.controller'; |
| 3 | +import { NotificationFactory } from '../../src/notification/application/factories/notification.factory'; |
| 4 | +import { INotificationRepository } from '../../src/notification/domain/interfaces/notification-repository.interface'; |
| 5 | +import { LoggerServiceFile } from '../../src/logger/services/logger.service.file'; |
| 6 | +import { LoggerServiceDb } from '../../src/logger/services/logger.service.db'; |
| 7 | +import { SendNotificationDto } from '../../src/notification/presentation/dtos/send-notification.dto'; |
| 8 | +import { Notification } from '../../src/notification/domain/entities/notification.entity'; |
| 9 | +import { |
| 10 | + NotificationChannel, |
| 11 | + NotificationType, |
| 12 | +} from '../../src/config/notification.config'; |
| 13 | + |
| 14 | +describe('NotificationController', () => { |
| 15 | + let controller: NotificationController; |
| 16 | + let factory: NotificationFactory; |
| 17 | + let repository: INotificationRepository; |
| 18 | + |
| 19 | + const mockNotification = new Notification( |
| 20 | + 'test@example.com', |
| 21 | + 'Test Subject', |
| 22 | + 'Test Body', |
| 23 | + NotificationChannel.EMAIL, |
| 24 | + NotificationType.ADMISSION_ID, |
| 25 | + new Date(), |
| 26 | + ); |
| 27 | + |
| 28 | + const mockSendDto: SendNotificationDto = { |
| 29 | + recipient: 'test@example.com', |
| 30 | + subject: 'Test Subject', |
| 31 | + body: 'Test Body', |
| 32 | + mediaType: NotificationChannel.EMAIL, |
| 33 | + notificationType: NotificationType.ADMISSION_ID, |
| 34 | + createdAt: new Date(), |
| 35 | + }; |
| 36 | + |
| 37 | + const mockStrategy = { |
| 38 | + send: jest.fn().mockResolvedValue('EMAIL'), |
| 39 | + }; |
| 40 | + |
| 41 | + beforeEach(async () => { |
| 42 | + const module: TestingModule = await Test.createTestingModule({ |
| 43 | + controllers: [NotificationController], |
| 44 | + providers: [ |
| 45 | + { |
| 46 | + provide: NotificationFactory, |
| 47 | + useValue: { |
| 48 | + createStrategy: jest.fn().mockReturnValue(mockStrategy), |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + provide: 'INotificationRepository', |
| 53 | + useValue: { |
| 54 | + save: jest.fn().mockResolvedValue(mockNotification), |
| 55 | + findAll: jest.fn().mockResolvedValue([mockNotification]), |
| 56 | + count: jest.fn().mockResolvedValue(1), |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + provide: LoggerServiceFile, |
| 61 | + useValue: { |
| 62 | + log: jest.fn(), |
| 63 | + error: jest.fn(), |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + provide: LoggerServiceDb, |
| 68 | + useValue: { |
| 69 | + log: jest.fn(), |
| 70 | + error: jest.fn().mockResolvedValue(undefined), |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }).compile(); |
| 75 | + |
| 76 | + controller = module.get<NotificationController>(NotificationController); |
| 77 | + factory = module.get<NotificationFactory>(NotificationFactory); |
| 78 | + repository = module.get<INotificationRepository>('INotificationRepository'); |
| 79 | + |
| 80 | + // Use spies to avoid unbound method errors |
| 81 | + jest.spyOn(factory, 'createStrategy'); |
| 82 | + jest.spyOn(repository, 'save'); |
| 83 | + jest.spyOn(mockStrategy, 'send'); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('send', () => { |
| 87 | + it('should send notification and log successfully', async () => { |
| 88 | + await controller.send(mockSendDto); |
| 89 | + |
| 90 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 91 | + expect(factory.createStrategy).toHaveBeenCalledWith( |
| 92 | + mockSendDto.mediaType, |
| 93 | + ); |
| 94 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 95 | + expect(repository.save).toHaveBeenCalledWith(expect.any(Notification)); |
| 96 | + expect(mockStrategy.send).toHaveBeenCalledWith(expect.any(Notification)); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw error if strategy send fails', async () => { |
| 100 | + jest.spyOn(factory, 'createStrategy').mockReturnValue({ |
| 101 | + send: jest.fn().mockRejectedValue(new Error('Send failed')), |
| 102 | + }); |
| 103 | + |
| 104 | + await expect(controller.send(mockSendDto)).rejects.toThrow('Send failed'); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('getNotificationHistory', () => { |
| 109 | + it('should return notification history with default pagination', async () => { |
| 110 | + jest.spyOn(repository, 'findAll'); |
| 111 | + const result = await controller.getNotificationHistory(1, 10); |
| 112 | + |
| 113 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 114 | + expect(repository.findAll).toHaveBeenCalledWith(0, 10); |
| 115 | + expect(result).toEqual({ |
| 116 | + data: [mockNotification], |
| 117 | + total: 1, |
| 118 | + page: 1, |
| 119 | + totalPages: 1, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle custom pagination', async () => { |
| 124 | + jest |
| 125 | + .spyOn(repository, 'findAll') |
| 126 | + .mockResolvedValue([mockNotification, mockNotification]); |
| 127 | + const result = await controller.getNotificationHistory(2, 5); |
| 128 | + |
| 129 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 130 | + expect(repository.findAll).toHaveBeenCalledWith(5, 5); |
| 131 | + expect(result).toEqual({ |
| 132 | + data: [mockNotification, mockNotification], |
| 133 | + total: 2, |
| 134 | + page: 2, |
| 135 | + totalPages: 1, |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments