Skip to content

Commit

Permalink
chore: add tests + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francisvaut committed May 13, 2024
1 parent bbf55fb commit 522ff18
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 25 deletions.
5 changes: 5 additions & 0 deletions frontend/src/test/unit/services/setup/delete_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ export const deleteHandlers = [
assistants.splice(index, 1);
return HttpResponse.json(assistants);
}),
http.delete(baseUrl + endpoints.structureChecks.retrieve.replace('{id}', ':id'), async ({ params }) => {
const index = structureChecks.findIndex((x) => x.id === params.id);
structureChecks.splice(index, 1);
return HttpResponse.json(structureChecks);
}),
];
17 changes: 0 additions & 17 deletions frontend/src/test/unit/services/setup/get_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,10 @@ export const getHandlers = [
const project = projects.find((x) => x.id === params.id);
const groupIds = project !== null && project !== undefined ? project.groups : [];
const submissionIds = project !== null && project !== undefined ? project.submissions : [];
const subGroupIds = Array.from(
new Set(submissions.filter((x) => submissionIds.includes(x.id)).map((x) => x.group)),
);

// Filter submissions for each subgroup and get the submission with the highest number
const subgroupSubmissions = subGroupIds.map((groupId) => {
const submissionsForGroup = submissions.filter((submission) => submission.group === groupId);
if (submissionsForGroup.length > 0) {
return submissionsForGroup.reduce((maxSubmission, currentSubmission) => {
return currentSubmission.submission_number > maxSubmission.submission_number
? currentSubmission
: maxSubmission;
});
} else {
return null;
}
});
return HttpResponse.json({
groups_submitted: new Set(submissions.filter((x) => submissionIds.includes(x.id)).map((x) => x.group)).size,
non_empty_groups: groups.filter((x) => groupIds.includes(x.id) && x.students.length > 0).length,
submissions_passed: subgroupSubmissions.filter((x) => x?.structureChecks_passed).length,
});
}),
http.get(baseUrl + endpoints.structureChecks.byProject.replace('{projectId}', ':id'), ({ params }) => {
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/test/unit/services/setup/post_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,29 @@ export const postHandlers = [
faculties.push(newFaculty);
return HttpResponse.json(faculties);
}),
http.post(baseUrl + endpoints.groups.byProject.replace('{projectId}', ':id'), async ({ request, params }) => {
http.post(baseUrl + endpoints.groups.byProject.replace('{projectId}', ':id'), async ({ request }) => {
const buffer = await request.arrayBuffer();
const requestBody = new TextDecoder().decode(buffer);
const newGroup = JSON.parse(requestBody);
groups.push(newGroup);
return HttpResponse.json(groups);
}),
http.post(baseUrl + endpoints.projects.byCourse.replace('{courseId}', ':id'), async ({ request, params }) => {
http.post(baseUrl + endpoints.projects.byCourse.replace('{courseId}', ':id'), async ({ request }) => {
const buffer = await request.arrayBuffer();
const requestBody = new TextDecoder().decode(buffer);
const newProject = JSON.parse(requestBody);
projects.push(newProject);
return HttpResponse.json(projects);
}),
http.post(
baseUrl + endpoints.structureChecks.byProject.replace('{projectId}', ':id'),
async ({ request, params }) => {
const buffer = await request.arrayBuffer();
const requestBody = new TextDecoder().decode(buffer);
const newStructureCheck = JSON.parse(requestBody);
newStructureCheck.project = params.id;
structureChecks.push(newStructureCheck);
return HttpResponse.json(structureChecks);
},
),
];
50 changes: 50 additions & 0 deletions frontend/src/test/unit/services/structure_check.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { describe, it, expect } from 'vitest';
import { useStructureCheck } from '@/composables/services/structure_check.service.ts';
import { StructureCheck } from '@/types/StructureCheck';

const {
structureChecks,
Expand Down Expand Up @@ -59,3 +60,52 @@ describe('structureCheck', (): void => {
expect(structureChecks.value?.[3]?.blocked_extensions).toBeNull();
});
});

it('create structureCheck', async () => {
resetService();

const exampleStructureCheck = new StructureCheck(
'', // id
'structure_check_name', // name
[], // blocked extensions
[], // obligated extensions
null, // project
);

await getStructureCheckByProject('123456');
expect(structureChecks).not.toBeNull();
expect(Array.isArray(structureChecks.value)).toBe(true);
const prevLength = structureChecks.value?.length ?? 0;

await createStructureCheck(exampleStructureCheck, '123456');
await getStructureCheckByProject('123456');

expect(structureChecks).not.toBeNull();
expect(Array.isArray(structureChecks.value)).toBe(true);
expect(structureChecks.value?.length).toBe(prevLength + 1);

// Only check for fields that are sent to the backend
expect(structureChecks.value?.[prevLength]?.name).toBe('structure_check_name');
});

it('delete structureCheck', async () => {
resetService();

await getStructureCheckByProject('123456');
expect(structureChecks.value).not.toBeNull();
expect(Array.isArray(structureChecks.value)).toBe(true);
const prevLength = structureChecks.value?.length ?? 0;

let structureCheckId = '';
if (structureChecks.value?.[2]?.id !== undefined && structureChecks.value?.[2].id !== null) {
structureCheckId = structureChecks.value?.[2]?.id;
}

await deleteStructureCheck(structureCheckId);
await getStructureCheckByProject('123456');

expect(structureChecks).not.toBeNull();
expect(Array.isArray(structureChecks.value)).toBe(true);
expect(structureChecks.value?.length).toBe(prevLength - 1);
expect(structureChecks.value?.[2]?.id).not.toBe(structureCheckId);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe('submision_status', (): void => {
expect(submissionStatus.value).not.toBeNull();
expect(submissionStatus.value?.groups_submitted).toBe(1);
expect(submissionStatus.value?.non_empty_groups).toBe(2);
expect(submissionStatus.value?.submissions_passed).toBe(1);
// No need to check for structure_check_passed and extra_checks_passed since those queries are not implemented in the frontend
});
});
5 changes: 3 additions & 2 deletions frontend/src/test/unit/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ export const structureCheckData = {

export const submissionStatusData = {
non_empty_groups: 5,
groups_submitted: 2,
submissions_passed: 1,
groups_submitted: 4,
structure_checks_passed: 3,
extra_checks_passed: 1,
};

export const submissionData = {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/test/unit/types/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export function createSubmissionStatus(submissionStatusData: any): SubmissionSta
return new SubmissionStatus(
submissionStatusData.non_empty_groups,
submissionStatusData.groups_submitted,
submissionStatusData.submissions_passed,
submissionStatusData.structure_checks_passed,
submissionStatusData.extra_checks_passed,
);
}

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/test/unit/types/submissionStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('submissionStatus type', () => {
expect(submissionStatus).toBeInstanceOf(SubmissionStatus);
expect(submissionStatus.non_empty_groups).toBe(submissionStatusData.non_empty_groups);
expect(submissionStatus.groups_submitted).toBe(submissionStatusData.groups_submitted);
expect(submissionStatus.submissions_passed).toBe(submissionStatusData.submissions_passed);
expect(submissionStatus.structure_checks_passed).toBe(submissionStatusData.structure_checks_passed);
expect(submissionStatus.extra_checks_passed).toBe(submissionStatusData.extra_checks_passed);
});

it('create a submissionStatus instance from JSON data', () => {
Expand All @@ -21,6 +22,7 @@ describe('submissionStatus type', () => {
expect(submissionStatus).toBeInstanceOf(SubmissionStatus);
expect(submissionStatus.non_empty_groups).toBe(submissionStatusData.non_empty_groups);
expect(submissionStatus.groups_submitted).toBe(submissionStatusData.groups_submitted);
expect(submissionStatus.submissions_passed).toBe(submissionStatusData.submissions_passed);
expect(submissionStatus.structure_checks_passed).toBe(submissionStatusData.structure_checks_passed);
expect(submissionStatus.extra_checks_passed).toBe(submissionStatusData.extra_checks_passed);
});
});

0 comments on commit 522ff18

Please sign in to comment.