refactor: tests class ids naar kleinere zonder hardcoding

This commit is contained in:
Gabriellvl 2025-05-04 13:52:23 +02:00
parent 81e0af28cc
commit 9643e25aed
15 changed files with 100 additions and 37 deletions

View file

@ -0,0 +1,49 @@
import { setupTestApp } from '../setup-tests.js';
import { describe, it, expect, beforeAll, beforeEach, vi, Mock } from 'vitest';
import { Request, Response } from 'express';
import {createClassHandler, deleteClassHandler} from "../../src/controllers/classes";
describe('Class controllers', () => {
let req: Partial<Request>;
let res: Partial<Response>;
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('create and delete class', async () => {
req = {
body: { displayName: 'coole_nieuwe_klas' },
};
await createClassHandler(req as Request, res as Response);
const result = jsonMock.mock.lastCall?.[0];
console.log('class', result.class);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() }));
req = {
params: { id: result.class.id },
};
await deleteClassHandler(req as Request, res as Response);
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ class: expect.anything() }));
});
});

View file

@ -21,6 +21,7 @@ import { BadRequestException } from '../../src/exceptions/bad-request-exception.
import { ConflictException } from '../../src/exceptions/conflict-exception.js';
import { EntityAlreadyExistsException } from '../../src/exceptions/entity-already-exists-exception.js';
import { StudentDTO } from '@dwengo-1/common/interfaces/student';
import {getClass02} from "../test_assets/classes/classes.testdata";
describe('Student controllers', () => {
let req: Partial<Request>;
@ -186,7 +187,7 @@ describe('Student controllers', () => {
it('Get join request by student and class', async () => {
req = {
params: { username: 'PinkFloyd', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
params: { username: 'PinkFloyd', classId: getClass02().classId },
};
await getStudentRequestHandler(req as Request, res as Response);
@ -201,7 +202,7 @@ describe('Student controllers', () => {
it('Create and delete join request', async () => {
req = {
params: { username: 'TheDoors' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
body: { classId: getClass02().classId },
};
await createStudentRequestHandler(req as Request, res as Response);
@ -209,7 +210,7 @@ describe('Student controllers', () => {
expect(jsonMock).toHaveBeenCalledWith(expect.objectContaining({ request: expect.anything() }));
req = {
params: { username: 'TheDoors', classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
params: { username: 'TheDoors', classId: getClass02().classId },
};
await deleteClassJoinRequestHandler(req as Request, res as Response);
@ -222,7 +223,7 @@ describe('Student controllers', () => {
it('Create join request student already in class error', async () => {
req = {
params: { username: 'Noordkaap' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
body: { classId: getClass02().classId },
};
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);
@ -231,7 +232,7 @@ describe('Student controllers', () => {
it('Create join request duplicate', async () => {
req = {
params: { username: 'Tool' },
body: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
body: { classId: getClass02().classId },
};
await expect(async () => createStudentRequestHandler(req as Request, res as Response)).rejects.toThrow(ConflictException);

View file

@ -12,6 +12,7 @@ import { TeacherInvitationData } from '@dwengo-1/common/interfaces/teacher-invit
import { getClassHandler } from '../../src/controllers/classes';
import { BadRequestException } from '../../src/exceptions/bad-request-exception';
import { ClassStatus } from '@dwengo-1/common/util/class-join-request';
import {getClass02} from "../test_assets/classes/classes.testdata";
describe('Teacher controllers', () => {
let req: Partial<Request>;
@ -57,7 +58,7 @@ describe('Teacher controllers', () => {
const body = {
sender: 'LimpBizkit',
receiver: 'testleerkracht1',
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
class: getClass02().classId,
} as TeacherInvitationData;
req = { body };
@ -67,7 +68,7 @@ describe('Teacher controllers', () => {
params: {
sender: 'LimpBizkit',
receiver: 'testleerkracht1',
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
classId: getClass02().classId,
},
body: { accepted: 'false' },
};
@ -80,7 +81,7 @@ describe('Teacher controllers', () => {
params: {
sender: 'LimpBizkit',
receiver: 'FooFighters',
classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
classId: getClass02().classId,
},
};
await getInvitationHandler(req as Request, res as Response);
@ -100,7 +101,7 @@ describe('Teacher controllers', () => {
const body = {
sender: 'LimpBizkit',
receiver: 'FooFighters',
class: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
class: getClass02().classId,
} as TeacherInvitationData;
req = { body };
@ -111,7 +112,7 @@ describe('Teacher controllers', () => {
req = {
params: {
id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89',
id: getClass02().classId,
},
};

View file

@ -17,6 +17,7 @@ import { EntityAlreadyExistsException } from '../../src/exceptions/entity-alread
import { getStudentRequestsHandler } from '../../src/controllers/students.js';
import { TeacherDTO } from '@dwengo-1/common/interfaces/teacher';
import { getClassHandler } from '../../src/controllers/classes';
import {getClass02} from "../test_assets/classes/classes.testdata";
describe('Teacher controllers', () => {
let req: Partial<Request>;
@ -169,7 +170,7 @@ describe('Teacher controllers', () => {
it('Get join requests by class', async () => {
req = {
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
params: { classId: getClass02().classId },
};
await getStudentJoinRequestHandler(req as Request, res as Response);
@ -183,7 +184,7 @@ describe('Teacher controllers', () => {
it('Update join request status', async () => {
req = {
params: { classId: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89', studentUsername: 'PinkFloyd' },
params: { classId: getClass02().classId, studentUsername: 'PinkFloyd' },
body: { accepted: 'true' },
};
@ -201,7 +202,7 @@ describe('Teacher controllers', () => {
expect(status).toBeTruthy();
req = {
params: { id: '34d484a1-295f-4e9f-bfdc-3e7a23d86a89' },
params: { id: getClass02().classId },
};
await getClassHandler(req as Request, res as Response);