From 5624f3bbfe9b93e05ba08ca0af9d465cab5da501 Mon Sep 17 00:00:00 2001 From: Gabriellvl Date: Sun, 13 Apr 2025 09:48:34 +0200 Subject: [PATCH] feat: tests --- .../src/controllers/teacher-invitations.ts | 6 +- .../controllers/teacher-invitations.test.ts | 97 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 backend/tests/controllers/teacher-invitations.test.ts diff --git a/backend/src/controllers/teacher-invitations.ts b/backend/src/controllers/teacher-invitations.ts index 3292b7bf..15bfc936 100644 --- a/backend/src/controllers/teacher-invitations.ts +++ b/backend/src/controllers/teacher-invitations.ts @@ -8,7 +8,7 @@ export async function getAllInvitationsHandler(req: Request, res: Response): Pro const by = req.query.by === 'true'; requireFields({ username }); - const invitations = getAllInvitations(username, by); + const invitations = await getAllInvitations(username, by); res.json({ invitations }); } @@ -28,11 +28,11 @@ export async function createInvitationHandler(req: Request, res: Response): Prom export async function deleteInvitationForHandler(req: Request, res: Response): Promise { const sender = req.params.sender; const receiver = req.params.receiver; - const classId = req.params.class; + const classId = req.params.classId; const accepted = req.body.accepted !== 'false'; requireFields({ sender, receiver, classId }); - const invitation = deleteInvitationFor(sender, receiver, classId, accepted); + const invitation = await deleteInvitationFor(sender, receiver, classId, accepted); res.json({ invitation }); } diff --git a/backend/tests/controllers/teacher-invitations.test.ts b/backend/tests/controllers/teacher-invitations.test.ts new file mode 100644 index 00000000..8135281a --- /dev/null +++ b/backend/tests/controllers/teacher-invitations.test.ts @@ -0,0 +1,97 @@ +import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest'; +import { Request, Response } from 'express'; +import { setupTestApp } from '../setup-tests.js'; +import { + createInvitationHandler, + deleteInvitationForHandler, + getAllInvitationsHandler +} from "../../src/controllers/teacher-invitations"; +import {TeacherInvitationData} from "@dwengo-1/common/interfaces/teacher-invitation"; +import {getClassHandler} from "../../src/controllers/classes"; + +describe('Teacher controllers', () => { + let req: Partial; + let res: Partial; + + let jsonMock: Mock; + + beforeAll(async () => { + await setupTestApp(); + }); + + beforeEach(() => { + jsonMock = vi.fn(); + res = { + json: jsonMock, + }; + }); + + it('Get teacher invitations by', async () => { + req = {params: {username: 'LimpBizkit'}, query: {by: 'true' }}; + + await getAllInvitationsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({invitations: expect.anything()})); + + const result = jsonMock.mock.lastCall?.[0]; + expect(result.invitations).to.have.length.greaterThan(0); + }); + + it('Get teacher invitations for', async () => { + req = {params: {username: 'FooFighters'}, query: {by: 'false' }}; + + await getAllInvitationsHandler(req as Request, res as Response); + + expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({invitations: expect.anything()})); + + const result = jsonMock.mock.lastCall?.[0]; + expect(result.invitations).to.have.length.greaterThan(0); + }); + + it('Create and delete invitation', async () => { + const body = { + sender: 'LimpBizkit', receiver: 'testleerkracht1', + class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' + } as TeacherInvitationData; + req = { body }; + + await createInvitationHandler(req as Request, res as Response); + + req = { + params: { + sender: 'LimpBizkit', receiver: 'testleerkracht1', + classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' + }, body: { accepted: 'false' } + }; + + await deleteInvitationForHandler(req as Request, res as Response); + }); + + it('Create and accept invitation', async () => { + const body = { + sender: 'LimpBizkit', receiver: 'testleerkracht1', + class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' + } as TeacherInvitationData; + req = { body }; + + await createInvitationHandler(req as Request, res as Response); + + req = { + params: { + sender: 'LimpBizkit', receiver: 'testleerkracht1', + classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' + }, body: { accepted: 'true' } + }; + + await deleteInvitationForHandler(req as Request, res as Response); + + req = {params: { + id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' + }}; + + await getClassHandler(req as Request, res as Response); + + const result = jsonMock.mock.lastCall?.[0]; + expect(result.class.teachers).toContain('testleerkracht1'); + }); +});