|
| 1 | +// src/notifications/tests/notifications.controller.spec.ts |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { NotificationsController } from '../notifications.controller'; |
| 4 | +import { NotificationsService } from '../notifications.service'; |
| 5 | +import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; |
| 6 | +import { NotificationType } from '../entities/notification.entity'; |
| 7 | +import { GetNotificationsQueryDto } from '../dto/get-notifications-query.dto'; |
| 8 | +import { |
| 9 | + PaginatedNotificationsResponseDto, |
| 10 | + PaginationMetaDto, |
| 11 | +} from '../dto/paginated-notifications-response.dto'; |
| 12 | + |
| 13 | +// ─── Helpers ───────────────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +const mockUser = { sub: 'user-uuid-1', email: 'user@example.com' }; |
| 16 | + |
| 17 | +const makePaginatedResponse = ( |
| 18 | + overrides: Partial<PaginatedNotificationsResponseDto> = {}, |
| 19 | +): PaginatedNotificationsResponseDto => ({ |
| 20 | + data: [ |
| 21 | + { |
| 22 | + id: 'notif-uuid-1', |
| 23 | + userId: 'user-uuid-1', |
| 24 | + type: NotificationType.SYSTEM, |
| 25 | + title: 'Test', |
| 26 | + content: 'Test content', |
| 27 | + isRead: false, |
| 28 | + createdAt: new Date('2024-01-01T00:00:00.000Z'), |
| 29 | + }, |
| 30 | + ], |
| 31 | + meta: { |
| 32 | + page: 1, |
| 33 | + limit: 20, |
| 34 | + total: 1, |
| 35 | + totalPages: 1, |
| 36 | + hasNextPage: false, |
| 37 | + hasPreviousPage: false, |
| 38 | + }, |
| 39 | + ...overrides, |
| 40 | +}); |
| 41 | + |
| 42 | +// ─── Mock Service ───────────────────────────────────────────────────────────── |
| 43 | + |
| 44 | +const mockNotificationsService = { |
| 45 | + findAllForUser: jest.fn(), |
| 46 | + getUnreadCount: jest.fn(), |
| 47 | +}; |
| 48 | + |
| 49 | +// ─── Tests ─────────────────────────────────────────────────────────────────── |
| 50 | + |
| 51 | +describe('NotificationsController', () => { |
| 52 | + let controller: NotificationsController; |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + jest.clearAllMocks(); |
| 56 | + |
| 57 | + const module: TestingModule = await Test.createTestingModule({ |
| 58 | + controllers: [NotificationsController], |
| 59 | + providers: [ |
| 60 | + { provide: NotificationsService, useValue: mockNotificationsService }, |
| 61 | + ], |
| 62 | + }) |
| 63 | + // Bypass JwtAuthGuard in unit tests — integration tests cover auth |
| 64 | + .overrideGuard(JwtAuthGuard) |
| 65 | + .useValue({ canActivate: () => true }) |
| 66 | + .compile(); |
| 67 | + |
| 68 | + controller = module.get<NotificationsController>(NotificationsController); |
| 69 | + }); |
| 70 | + |
| 71 | + // ── GET /api/notifications ────────────────────────────────────────────────── |
| 72 | + |
| 73 | + describe('getNotifications', () => { |
| 74 | + it('should call service.findAllForUser with user id and query params', async () => { |
| 75 | + const expected = makePaginatedResponse(); |
| 76 | + mockNotificationsService.findAllForUser.mockResolvedValue(expected); |
| 77 | + |
| 78 | + const query: GetNotificationsQueryDto = { page: 1, limit: 20 }; |
| 79 | + const result = await controller.getNotifications(mockUser as any, query); |
| 80 | + |
| 81 | + expect(mockNotificationsService.findAllForUser).toHaveBeenCalledWith( |
| 82 | + mockUser.sub, |
| 83 | + query, |
| 84 | + ); |
| 85 | + expect(result).toBe(expected); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should pass isRead filter to service', async () => { |
| 89 | + mockNotificationsService.findAllForUser.mockResolvedValue( |
| 90 | + makePaginatedResponse({ data: [] }), |
| 91 | + ); |
| 92 | + |
| 93 | + const query: GetNotificationsQueryDto = { page: 1, limit: 20, isRead: false }; |
| 94 | + await controller.getNotifications(mockUser as any, query); |
| 95 | + |
| 96 | + expect(mockNotificationsService.findAllForUser).toHaveBeenCalledWith( |
| 97 | + mockUser.sub, |
| 98 | + expect.objectContaining({ isRead: false }), |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should pass type filter to service', async () => { |
| 103 | + mockNotificationsService.findAllForUser.mockResolvedValue( |
| 104 | + makePaginatedResponse({ data: [] }), |
| 105 | + ); |
| 106 | + |
| 107 | + const query: GetNotificationsQueryDto = { |
| 108 | + page: 1, |
| 109 | + limit: 20, |
| 110 | + type: NotificationType.MENTION, |
| 111 | + }; |
| 112 | + await controller.getNotifications(mockUser as any, query); |
| 113 | + |
| 114 | + expect(mockNotificationsService.findAllForUser).toHaveBeenCalledWith( |
| 115 | + mockUser.sub, |
| 116 | + expect.objectContaining({ type: NotificationType.MENTION }), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return the paginated response from service', async () => { |
| 121 | + const expected = makePaginatedResponse(); |
| 122 | + mockNotificationsService.findAllForUser.mockResolvedValue(expected); |
| 123 | + |
| 124 | + const result = await controller.getNotifications( |
| 125 | + mockUser as any, |
| 126 | + { page: 1, limit: 20 }, |
| 127 | + ); |
| 128 | + |
| 129 | + expect(result.data).toHaveLength(1); |
| 130 | + expect(result.meta.total).toBe(1); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + // ── GET /api/notifications/count ──────────────────────────────────────────── |
| 135 | + |
| 136 | + describe('getUnreadCount', () => { |
| 137 | + it('should call service.getUnreadCount with user id', async () => { |
| 138 | + mockNotificationsService.getUnreadCount.mockResolvedValue(5); |
| 139 | + |
| 140 | + await controller.getUnreadCount(mockUser as any); |
| 141 | + |
| 142 | + expect(mockNotificationsService.getUnreadCount).toHaveBeenCalledWith( |
| 143 | + mockUser.sub, |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should return { count: number } shape', async () => { |
| 148 | + mockNotificationsService.getUnreadCount.mockResolvedValue(5); |
| 149 | + |
| 150 | + const result = await controller.getUnreadCount(mockUser as any); |
| 151 | + |
| 152 | + expect(result).toEqual({ count: 5 }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return count of 0 when no unread notifications', async () => { |
| 156 | + mockNotificationsService.getUnreadCount.mockResolvedValue(0); |
| 157 | + |
| 158 | + const result = await controller.getUnreadCount(mockUser as any); |
| 159 | + |
| 160 | + expect(result).toEqual({ count: 0 }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should always wrap the number in a count object regardless of value', async () => { |
| 164 | + mockNotificationsService.getUnreadCount.mockResolvedValue(99); |
| 165 | + |
| 166 | + const result = await controller.getUnreadCount(mockUser as any); |
| 167 | + |
| 168 | + expect(result).toHaveProperty('count'); |
| 169 | + expect(typeof result.count).toBe('number'); |
| 170 | + expect(result.count).toBe(99); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments