From 9648f31cfdcff65f17d2a8d38cfbc630743d050b Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Sun, 23 Mar 2025 18:55:08 +0100 Subject: [PATCH] feat: tests voor backend/controllers/groups.ts geimplementeerd (4 FAILS) --- backend/src/controllers/groups.ts | 9 +- backend/tests/controllers/classes.test.ts | 22 ++- backend/tests/controllers/group.test.ts | 157 ++++++++++++++++++++++ 3 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 backend/tests/controllers/group.test.ts diff --git a/backend/src/controllers/groups.ts b/backend/src/controllers/groups.ts index b7bfd212..da1cde39 100644 --- a/backend/src/controllers/groups.ts +++ b/backend/src/controllers/groups.ts @@ -2,14 +2,7 @@ import { Request, Response } from 'express'; import { createGroup, getAllGroups, getGroup, getGroupSubmissions } from '../services/groups.js'; import { GroupDTO } from '../interfaces/group.js'; -// Typescript is annoywith with parameter forwarding from class.ts -interface GroupParams { - classid: string; - assignmentid: string; - groupid?: string; -} - -export async function getGroupHandler(req: Request, res: Response): Promise { +export async function getGroupHandler(req: Request, res: Response): Promise { const classId = req.params.classid; const full = req.query.full === 'true'; const assignmentId = +req.params.assignmentid; diff --git a/backend/tests/controllers/classes.test.ts b/backend/tests/controllers/classes.test.ts index 03952e53..b20abae5 100644 --- a/backend/tests/controllers/classes.test.ts +++ b/backend/tests/controllers/classes.test.ts @@ -1,18 +1,14 @@ import { setupTestApp } from '../setup-tests.js'; import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; -import { createClassHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; +import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStudentsHandler, getTeacherInvitationsHandler } from '../../src/controllers/classes.js'; import { Request, Response } from 'express'; -import { getClassTeacherInvitations } from '../../src/services/class.js'; +import { getAllClasses } from '../../src/services/class.js'; async function fetchClass(id: string) { const data = await fetch(`localhost:3000/class/${id}`); return data; } -vi.mock('./getClass', () => ({ - getClass: vi.fn(), -})); - describe('Class controllers', () => { let req: Partial; let res: Partial; @@ -146,4 +142,18 @@ describe('Class controllers', () => { expect(statusMock).toHaveBeenCalledWith(404); expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); }); + + it('should return a list of classes', async () => { + req = { + query: {}, + }; + + await getAllClassesHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalled(); + + const result = jsonMock.mock.lastCall![0]; + + expect("classes" in result).toBeTruthy(); + }) }) \ No newline at end of file diff --git a/backend/tests/controllers/group.test.ts b/backend/tests/controllers/group.test.ts new file mode 100644 index 00000000..6b4b95a4 --- /dev/null +++ b/backend/tests/controllers/group.test.ts @@ -0,0 +1,157 @@ +import { setupTestApp } from '../setup-tests.js'; +import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; +import { Request, Response } from 'express'; +import { createGroupHandler, getAllGroupsHandler, getGroupHandler, getGroupSubmissionsHandler } from '../../src/controllers/groups.js'; +import { G } from 'vitest/dist/chunks/reporters.66aFHiyX.js'; + +async function test404( + req: Partial> , + res: Partial, + handler: (req: Request, res: Response) => Promise, + expectedMessage: string +) { + await handler(req as Request, res as Response); + + expect(res.status).toHaveBeenCalledWith(404); + expect(res.json).toHaveBeenCalledWith({ error: expectedMessage }); +} + +function createRequestObject(classid: string, assignmentid: string, groupNumber: string) { + return { + params: { + classid: classid, + assignmentid: assignmentid, // should not exist + groupid: groupNumber, // should not exist + }, + query: {}, + }; +} + +describe('Group controllers', () => { + let req: Partial; + let res: Partial; + + let jsonMock: Mock; + let statusMock: Mock; + + beforeAll(async () => { + await setupTestApp(); + }); + + beforeEach(async () => { + jsonMock = vi.fn(); + statusMock = vi.fn().mockReturnThis(); + + res = { + json: jsonMock, + status: statusMock, + }; + }); + + it('should return 404 not found on a non-existing group', async () => { + req = { + params: { + classid: 'id01', + assignmentid: '1', + groupid: '42000', // should not exist + }, + query: {}, + }; + + await getGroupHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalledWith({ error: 'Group not found' }); + }); + + it('should return 404 not found on a non-existing assignment', async () => { + req = { + params: { + classid: 'id01', + assignmentid: '1000', // should not exist + groupid: '42000', // should not exist + }, + query: {}, + }; + + await getGroupHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalledWith({ error: 'Assignment not found' }); + }); + + it('should return 404 not found ont a non-existing class', async () => { + req = { + params: { + classid: 'doesnotexist', // should not exist + assignmentid: '1000', // should not exist + groupid: '42000', // should not exist + }, + query: {}, + }; + + await getGroupHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalledWith({ error: 'Class not found' }); + }); + + it('should return an existing group', async () => { + req = createRequestObject('id01', '1', '1'); + + await getGroupHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith({ + assignment: 1, + groupNumber: 1, + members: [ 'DireStraits', 'Noordkaap' ] + }); + }); + + + it('should return a 201 when creating a group', async () => { + req = createRequestObject('id01', '1', 'irrelevant'); + req.body = { + members: [ + 'NoordKaap', + 'DireStraits', + ] + }; + + await createGroupHandler(req as Request, res as Response); + + expect(statusMock).toHaveBeenCalledWith(201); + expect(jsonMock).toHaveBeenCalled(); + + const result = jsonMock.mock.lastCall![0]; + + expect("assignment" in result).toBeTruthy(); + expect("groupNumber" in result).toBeTruthy(); + expect("members" in result).toBeTruthy(); + }); + + it('should return the submissions for a group', async () => { + req = createRequestObject('id01', '1', '1'); + + await getGroupSubmissionsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalled(); + + const result = jsonMock.mock.lastCall![0]; + + expect("submissions" in result).toBeTruthy(); + expect(typeof(result.submissions)).toBe(typeof([])); + }); + + it('should return a list of groups for an assignment', async () => { + req = createRequestObject('id01', '1', '1'); + + await getAllGroupsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalled(); + + const result = jsonMock.mock.lastCall![0]; + + expect("groups" in result).toBeTruthy(); + }); +});