|
| 1 | +import { CohortsService } from './cohorts.service'; |
| 2 | +import { buildOffsetResponse, clampLimit } from '../common/utils/pagination.utils'; |
| 3 | + |
| 4 | +describe('CohortsService', () => { |
| 5 | + let service: CohortsService; |
| 6 | + let mockCohortRepo: any; |
| 7 | + let mockMemberRepo: any; |
| 8 | + let mockThreadRepo: any; |
| 9 | + let mockCommentRepo: any; |
| 10 | + let mockAssignmentRepo: any; |
| 11 | + |
| 12 | + const mockMembership = { id: 'mem-1', cohortId: 'cohort-1', userId: 'user-1', role: 'member' }; |
| 13 | + const mockMember = { id: 'm-1', cohortId: 'cohort-1', userId: 'user-2', role: 'member', createdAt: new Date() }; |
| 14 | + const mockThread = { id: 't-1', cohortId: 'cohort-1', authorId: 'user-2', title: 'Thread', content: 'Content', createdAt: new Date() }; |
| 15 | + const mockAssignment = { id: 'a-1', cohortId: 'cohort-1', title: 'Assignment', createdAt: new Date() }; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockCohortRepo = { |
| 19 | + findOne: jest.fn().mockResolvedValue(null), |
| 20 | + create: jest.fn((dto) => dto), |
| 21 | + save: jest.fn(async (data) => ({ id: 'cohort-1', ...data })), |
| 22 | + createQueryBuilder: jest.fn(() => ({ |
| 23 | + innerJoin: jest.fn().mockReturnThis(), |
| 24 | + where: jest.fn().mockReturnThis(), |
| 25 | + orderBy: jest.fn().mockReturnThis(), |
| 26 | + getOne: jest.fn().mockResolvedValue(null), |
| 27 | + getMany: jest.fn().mockResolvedValue([]), |
| 28 | + })), |
| 29 | + }; |
| 30 | + |
| 31 | + mockMemberRepo = { |
| 32 | + findOne: jest.fn().mockResolvedValue(mockMembership), |
| 33 | + create: jest.fn((dto) => dto), |
| 34 | + save: jest.fn(async (data) => ({ id: 'new-id', ...data })), |
| 35 | + findAndCount: jest.fn().mockResolvedValue([[mockMember], 1]), |
| 36 | + }; |
| 37 | + |
| 38 | + mockThreadRepo = { |
| 39 | + findOne: jest.fn().mockResolvedValue(null), |
| 40 | + create: jest.fn((dto) => dto), |
| 41 | + save: jest.fn(async (data) => ({ id: 'new-id', ...data })), |
| 42 | + findAndCount: jest.fn().mockResolvedValue([[mockThread], 1]), |
| 43 | + }; |
| 44 | + |
| 45 | + mockCommentRepo = { |
| 46 | + create: jest.fn((dto) => dto), |
| 47 | + save: jest.fn(async (data) => ({ id: 'new-id', ...data })), |
| 48 | + }; |
| 49 | + |
| 50 | + mockAssignmentRepo = { |
| 51 | + findOne: jest.fn().mockResolvedValue(null), |
| 52 | + create: jest.fn((dto) => dto), |
| 53 | + save: jest.fn(async (data) => ({ id: 'new-id', ...data })), |
| 54 | + findAndCount: jest.fn().mockResolvedValue([[mockAssignment], 1]), |
| 55 | + }; |
| 56 | + |
| 57 | + service = new CohortsService( |
| 58 | + mockCohortRepo, |
| 59 | + mockMemberRepo, |
| 60 | + mockThreadRepo, |
| 61 | + mockCommentRepo, |
| 62 | + mockAssignmentRepo, |
| 63 | + ); |
| 64 | + |
| 65 | + mockCohortRepo.findOne.mockResolvedValue({ id: 'cohort-1', ownerId: 'user-1' }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('listMembers', () => { |
| 69 | + it('should return paginated members with default page size', async () => { |
| 70 | + mockMemberRepo.findAndCount.mockResolvedValue([[mockMember], 1]); |
| 71 | + |
| 72 | + const result = await service.listMembers('cohort-1', 'user-1'); |
| 73 | + |
| 74 | + expect(result.data).toEqual([mockMember]); |
| 75 | + expect(result.total).toBe(1); |
| 76 | + expect(result.page).toBe(1); |
| 77 | + expect(result.limit).toBe(10); |
| 78 | + expect(result.totalPages).toBe(1); |
| 79 | + expect(mockMemberRepo.findAndCount).toHaveBeenCalledWith( |
| 80 | + expect.objectContaining({ skip: 0, take: 10 }), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should respect custom page and limit', async () => { |
| 85 | + const items = Array.from({ length: 5 }, (_, i) => ({ |
| 86 | + ...mockMember, |
| 87 | + id: `m-${i + 1}`, |
| 88 | + createdAt: new Date(), |
| 89 | + })); |
| 90 | + mockMemberRepo.findAndCount.mockResolvedValue([items, 25]); |
| 91 | + |
| 92 | + const result = await service.listMembers('cohort-1', 'user-1', { page: 2, limit: 5 }); |
| 93 | + |
| 94 | + expect(result.data).toHaveLength(5); |
| 95 | + expect(result.total).toBe(25); |
| 96 | + expect(result.page).toBe(2); |
| 97 | + expect(result.limit).toBe(5); |
| 98 | + expect(result.totalPages).toBe(5); |
| 99 | + expect(result.hasNextPage).toBe(true); |
| 100 | + expect(result.hasPrevPage).toBe(true); |
| 101 | + expect(mockMemberRepo.findAndCount).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ skip: 5, take: 5 }), |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should enforce max page size via clampLimit', async () => { |
| 107 | + mockMemberRepo.findAndCount.mockResolvedValue([[mockMember], 1]); |
| 108 | + |
| 109 | + await service.listMembers('cohort-1', 'user-1', { page: 1, limit: 999 }); |
| 110 | + |
| 111 | + expect(mockMemberRepo.findAndCount).toHaveBeenCalledWith( |
| 112 | + expect.objectContaining({ take: 100 }), |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return first page when no query is provided', async () => { |
| 117 | + mockMemberRepo.findAndCount.mockResolvedValue([[mockMember], 1]); |
| 118 | + |
| 119 | + const result = await service.listMembers('cohort-1', 'user-1', undefined); |
| 120 | + |
| 121 | + expect(result.page).toBe(1); |
| 122 | + expect(mockMemberRepo.findAndCount).toHaveBeenCalledWith( |
| 123 | + expect.objectContaining({ skip: 0, take: 10 }), |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should reject non-members with ForbiddenException', async () => { |
| 128 | + mockMemberRepo.findOne.mockResolvedValue(null); |
| 129 | + |
| 130 | + await expect(service.listMembers('cohort-1', 'user-2')).rejects.toThrow('Access denied'); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('listThreads', () => { |
| 135 | + it('should return paginated threads ordered by createdAt DESC', async () => { |
| 136 | + mockThreadRepo.findAndCount.mockResolvedValue([[mockThread], 1]); |
| 137 | + |
| 138 | + const result = await service.listThreads('cohort-1', 'user-1', { page: 1, limit: 10 }); |
| 139 | + |
| 140 | + expect(result.data).toEqual([mockThread]); |
| 141 | + expect(result.total).toBe(1); |
| 142 | + expect(result.page).toBe(1); |
| 143 | + expect(mockThreadRepo.findAndCount).toHaveBeenCalledWith( |
| 144 | + expect.objectContaining({ |
| 145 | + where: { cohortId: 'cohort-1' }, |
| 146 | + order: { createdAt: 'DESC' }, |
| 147 | + skip: 0, |
| 148 | + take: 10, |
| 149 | + }), |
| 150 | + ); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('listAssignments', () => { |
| 155 | + it('should return paginated assignments ordered by createdAt DESC', async () => { |
| 156 | + mockAssignmentRepo.findAndCount.mockResolvedValue([[mockAssignment], 1]); |
| 157 | + |
| 158 | + const result = await service.listAssignments('cohort-1', 'user-1', { page: 1, limit: 10 }); |
| 159 | + |
| 160 | + expect(result.data).toEqual([mockAssignment]); |
| 161 | + expect(result.total).toBe(1); |
| 162 | + expect(result.page).toBe(1); |
| 163 | + expect(mockAssignmentRepo.findAndCount).toHaveBeenCalledWith( |
| 164 | + expect.objectContaining({ |
| 165 | + where: { cohortId: 'cohort-1' }, |
| 166 | + order: { createdAt: 'DESC' }, |
| 167 | + skip: 0, |
| 168 | + take: 10, |
| 169 | + }), |
| 170 | + ); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('clampLimit', () => { |
| 175 | + it('should default to DEFAULT_PAGE_SIZE when limit is undefined', () => { |
| 176 | + expect(clampLimit(undefined)).toBe(10); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should cap at MAX_PAGE_SIZE', () => { |
| 180 | + expect(clampLimit(200)).toBe(100); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should enforce minimum of 1', () => { |
| 184 | + expect(clampLimit(0)).toBe(1); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments