Skip to content

Commit bfa8f42

Browse files
committed
test: update tests for new route structure and added endpoints
1 parent f6fb305 commit bfa8f42

1 file changed

Lines changed: 56 additions & 6 deletions

File tree

tests/enrollment.test.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Authentication Middleware', () => {
6464

6565
it('allows GET /enrollments without token (public)', async () => {
6666
Enrollment.find = jest.fn().mockResolvedValue([]);
67-
const res = await request(app).get('/enrollments/S001');
67+
const res = await request(app).get('/enrollments/student/S001');
6868
// Should not be 401 — even though no enrollments, it should be 404 (not unauthorized)
6969
expect(res.status).not.toBe(401);
7070
});
@@ -145,11 +145,11 @@ describe('POST /enroll', () => {
145145
});
146146
});
147147

148-
// ── GET /enrollments/:studentId ───────────────────────────────
149-
describe('GET /enrollments/:studentId', () => {
148+
// ── GET /enrollments/student/:studentId ───────────────────────────────
149+
describe('GET /enrollments/student/:studentId', () => {
150150
it('returns 404 when no enrollments found', async () => {
151151
Enrollment.find = jest.fn().mockResolvedValue([]);
152-
const res = await request(app).get('/enrollments/S999');
152+
const res = await request(app).get('/enrollments/student/S999');
153153
expect(res.status).toBe(404);
154154
expect(res.body.message).toMatch(/no enrollments found/i);
155155
});
@@ -161,20 +161,70 @@ describe('GET /enrollments/:studentId', () => {
161161
];
162162
Enrollment.find = jest.fn().mockResolvedValue(mockEnrollments);
163163

164-
const res = await request(app).get('/enrollments/S001');
164+
const res = await request(app).get('/enrollments/student/S001');
165165
expect(res.status).toBe(200);
166166
expect(Array.isArray(res.body)).toBe(true);
167167
expect(res.body.length).toBe(2);
168168
});
169169

170170
it('returns 500 on database error', async () => {
171171
Enrollment.find = jest.fn().mockRejectedValue(new Error('DB error'));
172-
const res = await request(app).get('/enrollments/S001');
172+
const res = await request(app).get('/enrollments/student/S001');
173173
expect(res.status).toBe(500);
174174
expect(res.body.message).toMatch(/error fetching/i);
175175
});
176176
});
177177

178+
// ── GET /enrollments/course/:courseId ───────────────────────────────
179+
describe('GET /enrollments/course/:courseId', () => {
180+
it('returns roster when found', async () => {
181+
Enrollment.find = jest.fn().mockResolvedValue([{ student_id: 'S001' }]);
182+
const res = await request(app).get('/enrollments/course/C202');
183+
expect(res.status).toBe(200);
184+
expect(res.body[0].student_id).toBe('S001');
185+
});
186+
});
187+
188+
// ── GET /enrollments/check ──────────────────────────────────────────
189+
describe('GET /enrollments/check', () => {
190+
it('returns isEnrolled: true for valid enrollment', async () => {
191+
Enrollment.findOne = jest.fn().mockResolvedValue({ status: 'ACTIVE' });
192+
const res = await request(app).get('/enrollments/check?studentId=S101&courseId=C202');
193+
expect(res.status).toBe(200);
194+
expect(res.body.isEnrolled).toBe(true);
195+
});
196+
197+
it('returns isEnrolled: false for missing enrollment', async () => {
198+
Enrollment.findOne = jest.fn().mockResolvedValue(null);
199+
const res = await request(app).get('/enrollments/check?studentId=S999&courseId=C999');
200+
expect(res.status).toBe(200);
201+
expect(res.body.isEnrolled).toBe(false);
202+
});
203+
});
204+
205+
// ── PATCH /enrollments/:id/status ──────────────────────────────────
206+
describe('PATCH /enrollments/:id/status', () => {
207+
const authHeader = { Authorization: 'Bearer fake-jwt-token-for-testing' };
208+
209+
it('updates status successfully', async () => {
210+
Enrollment.findByIdAndUpdate = jest.fn().mockResolvedValue({ status: 'COMPLETED' });
211+
const res = await request(app)
212+
.patch('/enrollments/507f1f77bcf86cd799439011/status')
213+
.set(authHeader)
214+
.send({ status: 'COMPLETED' });
215+
expect(res.status).toBe(200);
216+
expect(res.body.enrollment.status).toBe('COMPLETED');
217+
});
218+
219+
it('returns 400 for invalid status', async () => {
220+
const res = await request(app)
221+
.patch('/enrollments/507f1f77bcf86cd799439011/status')
222+
.set(authHeader)
223+
.send({ status: 'INVALID' });
224+
expect(res.status).toBe(400);
225+
});
226+
});
227+
178228
// ── DELETE /enroll/:id ────────────────────────────────────────
179229
describe('DELETE /enroll/:id', () => {
180230
const authHeader = { Authorization: 'Bearer fake-jwt-token-for-testing' };

0 commit comments

Comments
 (0)