test(frontend): Coverage verbeteren

This commit is contained in:
Tibo De Peuter 2025-04-24 16:54:52 +02:00
parent c47da0c826
commit 4a6b6ee061
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
10 changed files with 387 additions and 16 deletions

View file

@ -0,0 +1,65 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { AssignmentController } from '../../src/controllers/assignments';
import { AssignmentDTO } from '@dwengo-1/common/interfaces/assignment';
describe('AssignmentController Tests', () => {
let controller: AssignmentController;
beforeEach(() => {
controller = new AssignmentController('8764b861-90a6-42e5-9732-c0d9eb2f55f9'); // Example class ID
});
it('should fetch all assignments', async () => {
const result = await controller.getAll(true);
expect(result).toHaveProperty('assignments');
expect(Array.isArray(result.assignments)).toBe(true);
expect(result.assignments.length).toBeGreaterThan(0);
});
it('should fetch an assignment by number', async () => {
const assignmentNumber = 21000; // Example assignment ID
const result = await controller.getByNumber(assignmentNumber);
expect(result).toHaveProperty('assignment');
expect(result.assignment).toHaveProperty('id', assignmentNumber);
});
it('should update an existing assignment', async () => {
const assignmentNumber = 21000;
const updatedData = { title: 'Updated Assignment Title' };
const result = await controller.updateAssignment(assignmentNumber, updatedData);
expect(result).toHaveProperty('assignment');
expect(result.assignment).toHaveProperty('id', assignmentNumber);
expect(result.assignment).toHaveProperty('title', updatedData.title);
});
it('should fetch submissions for an assignment', async () => {
const assignmentNumber = 21000;
const result = await controller.getSubmissions(assignmentNumber, true);
expect(result).toHaveProperty('submissions');
expect(Array.isArray(result.submissions)).toBe(true);
});
it('should fetch questions for an assignment', async () => {
const assignmentNumber = 21000;
const result = await controller.getQuestions(assignmentNumber, true);
expect(result).toHaveProperty('questions');
expect(Array.isArray(result.questions)).toBe(true);
});
it('should fetch groups for an assignment', async () => {
const assignmentNumber = 21000;
const result = await controller.getGroups(assignmentNumber, true);
expect(result).toHaveProperty('groups');
expect(Array.isArray(result.groups)).toBe(true);
});
it('should handle fetching a non-existent assignment', async () => {
const assignmentNumber = 99999; // Non-existent assignment ID
await expect(controller.getByNumber(assignmentNumber)).rejects.toThrow();
});
it('should handle deleting a non-existent assignment', async () => {
const assignmentNumber = 99999; // Non-existent assignment ID
await expect(controller.deleteAssignment(assignmentNumber)).rejects.toThrow();
});
});

View file

@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest';
import { ClassController } from '../../src/controllers/classes';
describe('Test controller classes', () => {
it('Get classes', async () => {
const controller = new ClassController();
const data = await controller.getAll(true);
expect(data.classes).to.have.length.greaterThan(0);
});
});

View file

@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';
import { GroupController } from '../../src/controllers/groups';
describe('Test controller groups', () => {
it('Get groups', async () => {
const classId = '8764b861-90a6-42e5-9732-c0d9eb2f55f9';
const assignmentNumber = 21000;
const controller = new GroupController(classId, assignmentNumber);
const data = await controller.getAll(true);
expect(data.groups).to.have.length.greaterThan(0);
});
});

View file

@ -0,0 +1,21 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { LearningPathController } from '../../src/controllers/learning-paths';
import { Language } from '../../src/data-objects/language';
describe("Test controller learning paths", () => {
let controller: LearningPathController;
beforeEach(async () => {
controller = new LearningPathController();
});
it("Can search for learning paths", async () => {
const data = await controller.search("kiks", Language.Dutch);
expect(data).to.have.length.greaterThan(0);
});
it("Can get learning path by id", async () => {
const data = await controller.getAllByTheme("kiks");
expect(data).to.have.length.greaterThan(0);
});
});

View file

@ -1,19 +1,30 @@
import { StudentController } from "../../src/controllers/students";
import { expect, it, describe, afterAll, beforeAll } from "vitest";
import { setup, teardown } from "../setup-backend.js";
import { StudentController } from '../../src/controllers/students';
import { beforeEach, describe, expect, it } from 'vitest';
describe("Test controller students", () => {
beforeAll(async () => {
await setup();
describe('Test controller students', () => {
let controller: StudentController;
beforeEach(async () => {
controller = new StudentController();
});
afterAll(async () => {
await teardown();
});
it("Get students", async () => {
const controller = new StudentController();
it('Get students', async () => {
const data = await controller.getAll(true);
expect(data.students).to.have.length.greaterThan(0);
});
it('Get student by username', async () => {
const username = 'testleerling1';
const data = await controller.getByUsername(username);
expect(data.student.username).to.equal(username);
});
it('Get classes of student', async () => {
const students = await controller.getAll(true);
for (const student of students.students) {
const data = await controller.getClasses(student.username, true);
expect(data.classes).to.have.length.greaterThan(0);
}
});
});

View file

@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest';
import { SubmissionController } from '../../src/controllers/submissions';
import { Language } from '../../src/data-objects/language';
describe("Test controller submissions", () => {
it("Get submission by number", async () => {
const hruid = "id03";
const classId = "8764b861-90a6-42e5-9732-c0d9eb2f55f9";
const controller = new SubmissionController(hruid);
const data = await controller.getByNumber(Language.English, 1, classId, 1, 1, 1);
expect(data.submission).to.have.property("submissionNumber");
});
});

View file

@ -0,0 +1,71 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { TeacherController } from '../../src/controllers/teachers';
describe("Test controller teachers", () => {
let controller: TeacherController;
beforeEach(async () => {
controller = new TeacherController();
});
it("Get all teachers", async () => {
const data = await controller.getAll(true);
expect(data.teachers).to.have.length.greaterThan(0);
expect(data.teachers[0]).to.have.property("username");
expect(data.teachers[0]).to.have.property("firstName");
expect(data.teachers[0]).to.have.property("lastName");
});
it("Get teacher by username", async () => {
const username = "testleerkracht1";
const data = await controller.getByUsername(username);
expect(data.teacher.username).to.equal(username);
expect(data.teacher).to.have.property("firstName");
expect(data.teacher).to.have.property("lastName");
});
it("Get teacher by non-existent username", async () => {
const username = "nonexistentuser";
await expect(controller.getByUsername(username)).rejects.toThrow();
});
it("Create a new teacher", async () => {
const newTeacher = {
username: "newteacher",
firstName: "New",
lastName: "Teacher",
};
const data = await controller.createTeacher(newTeacher);
expect(data.teacher.username).to.equal(newTeacher.username);
expect(data.teacher.firstName).to.equal(newTeacher.firstName);
expect(data.teacher.lastName).to.equal(newTeacher.lastName);
});
it("Delete a teacher", async () => {
const username = "newteacher";
const data = await controller.deleteTeacher(username);
expect(data).toBeTruthy();
});
it("Handle deletion of non-existent teacher", async () => {
const username = "nonexistentuser";
await expect(controller.deleteTeacher(username)).rejects.toThrow();
});
it("Get classes for a teacher", async () => {
const username = "testleerkracht1";
const data = await controller.getClasses(username, true);
expect(data.classes).to.have.length.greaterThan(0);
expect(data.classes[0]).to.have.property("id");
expect(data.classes[0]).to.have.property("displayName");
});
it("Get students for a teacher", async () => {
const username = "testleerkracht1";
const data = await controller.getStudents(username, true);
expect(data.students).to.have.length.greaterThan(0);
expect(data.students[0]).to.have.property("username");
expect(data.students[0]).to.have.property("firstName");
expect(data.students[0]).to.have.property("lastName");
});
});