From dbe7807f4aff78f730668787f8ee235301b1968a Mon Sep 17 00:00:00 2001 From: Adriaan Jacquet Date: Sun, 23 Mar 2025 20:49:09 +0100 Subject: [PATCH] feat: testen voor assignment controller geimplementeerd --- backend/tests/controllers/assignments.test.ts | 96 +++++++++++++++++++ backend/tests/controllers/classes.test.ts | 7 +- backend/tests/controllers/group.test.ts | 13 --- 3 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 backend/tests/controllers/assignments.test.ts diff --git a/backend/tests/controllers/assignments.test.ts b/backend/tests/controllers/assignments.test.ts new file mode 100644 index 00000000..bb9d80ed --- /dev/null +++ b/backend/tests/controllers/assignments.test.ts @@ -0,0 +1,96 @@ +import { setupTestApp } from '../setup-tests.js'; +import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest'; +import { Request, Response } from 'express'; +import { getAssignmentHandler, getAllAssignmentsHandler, getAssignmentsSubmissionsHandler } from '../../src/controllers/assignments.js'; + +function createRequestObject(classid: string, assignmentid: string) { + return { + params: { + classid: classid, + id: assignmentid, + }, + query: {}, + }; +} + +function checkReturnList(jsonMock: Mock, listName: string) { + expect(jsonMock).toHaveBeenCalled(); + + const result = jsonMock.mock.lastCall![0]; + + expect(listName in result).toBeTruthy(); +} + +function checkReturn404(jsonMock: Mock, statusMock: Mock) { + expect(statusMock).toHaveBeenCalledWith(404); + expect(jsonMock).toHaveBeenCalled(); +} + +describe('Assignment 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 a 404 when trying to find a non-existent assignment', async () => { + req = createRequestObject('id01', '43000'); // should not exist + + await getAssignmentHandler(req as Request, res as Response); + + checkReturn404(jsonMock, statusMock); + }); + + it('should return a 404 when trying to find an assignment on a non-existing class', async () => { + req = createRequestObject('doesnotexist', '1'); // should not exist + + await getAssignmentHandler(req as Request, res as Response); + + checkReturn404(jsonMock, statusMock); + }); + + it('should return an assignment', async () => { + req = createRequestObject('id01', '1'); + + await getAssignmentHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith({ + id: 1, + class: 'id01', + title: 'dire straits', + description: 'reading', + learningPath: 'id02', + language: 'en' + }); + }); + + it('should return a list of assignments', async () => { + req = createRequestObject('id01', 'irrelevant'); + + await getAllAssignmentsHandler(req as Request, res as Response); + + checkReturnList(jsonMock, "assignments"); + }); + + it('should return a list of submissions for an assignment', async () => { + req = createRequestObject('id01', '1'); + + await getAssignmentsSubmissionsHandler(req as Request, res as Response); + + checkReturnList(jsonMock, "submissions"); + }) +}) diff --git a/backend/tests/controllers/classes.test.ts b/backend/tests/controllers/classes.test.ts index b20abae5..f91c5e67 100644 --- a/backend/tests/controllers/classes.test.ts +++ b/backend/tests/controllers/classes.test.ts @@ -4,11 +4,6 @@ import { createClassHandler, getAllClassesHandler, getClassHandler, getClassStud import { Request, Response } from 'express'; import { getAllClasses } from '../../src/services/class.js'; -async function fetchClass(id: string) { - const data = await fetch(`localhost:3000/class/${id}`); - return data; -} - describe('Class controllers', () => { let req: Partial; let res: Partial; @@ -156,4 +151,4 @@ describe('Class controllers', () => { 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 index 8cfbd0d3..5d3b3542 100644 --- a/backend/tests/controllers/group.test.ts +++ b/backend/tests/controllers/group.test.ts @@ -2,19 +2,6 @@ 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 {