Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lennygir committed Jan 16, 2024
1 parent 8d36cf7 commit a2b24c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions server/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ router.put(
check("expiration_date").isISO8601().toDate(),
check("level").isString().isLength({ min: 3, max: 3 }),
check("cds").isString(),
(req, res) => {
async (req, res) => {
try {
if (!validationResult(req).isEmpty()) {
return res.status(400).json({ message: "Invalid proposal content" });
Expand Down Expand Up @@ -840,7 +840,7 @@ router.put(
level: level,
cds: cds,
};
notifyRemovedCosupervisors(proposal, newProposal);
await notifyRemovedCosupervisors(proposal, newProposal);
updateProposal(newProposal);
return res.status(200).json({ message: "Proposal updated successfully" });
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/theses-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,8 @@ exports.isAccepted = (proposal_id, student_id) => {
};

exports.notifyRemovedCosupervisors = async (oldProposal, newProposal) => {
const oldCosupervisors = oldProposal.co_supervisors?.split(", ");
const newCosupervisors = newProposal.co_supervisors?.split(", ");
const oldCosupervisors = (oldProposal.co_supervisors || "").split(", ");
const newCosupervisors = (newProposal.co_supervisors || []);
if(oldCosupervisors && newCosupervisors) {
const removedCosupervisors = oldCosupervisors.filter((cosupervisor) => {
return !newCosupervisors.includes(cosupervisor);
Expand Down
31 changes: 23 additions & 8 deletions server/tests/proposal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
getAcceptedApplicationsOfStudent,
getRequestsForClerk,
getProposalsThatExpireInXDays,
notifyRemovedCosupervisors,
} = require("../src/theses-dao");

const dayjs = require("dayjs");
Expand Down Expand Up @@ -963,8 +964,12 @@ describe("Cronjobs", () => {
});
});

describe("PATCH /api/proposals/:id", () => {
describe("PUT /api/proposals/:id", () => {
test("Remove a co supervisor", async () => {

const originalModule = jest.requireActual("../src/theses-dao");
notifyRemovedCosupervisors.mockImplementation(originalModule.notifyRemovedCosupervisors);

isLoggedIn.mockImplementation((req, res, next) => {
req.user = {
email: "[email protected]",
Expand All @@ -977,9 +982,7 @@ describe("PATCH /api/proposals/:id", () => {
title: "test",
description: "desc test",
supervisor: "s234567",
co_supervisors: [
"[email protected]"
],
co_supervisors: "[email protected]",
keywords: ["keyword1", "keyword2"],
groups: ["SOFTENG"],
types: ["EXPERIMENTAL"],
Expand All @@ -992,16 +995,28 @@ describe("PATCH /api/proposals/:id", () => {
deleted: 0
};

getProposal.mockReturnValue(proposal);
const modifiedProposal = {
id: 1,
title: "test",
description: "desc test",
co_supervisors: [],
keywords: ["keyword1", "keyword2"],
groups: [],
types: ["EXPERIMENTAL"],
required_knowledge: "required knowledge",
notes: "notes",
expiration_date: "2021-01-01",
level: "MSC",
cds: "LM-32 (DM270)"
};

const newProposal = { ...proposal };
newProposal.co_supervisors = [];
getProposal.mockReturnValue(proposal);

const requests = (
await request(app)
.put("/api/proposals/1")
.set("Content-Type", "application/json")
.send(newProposal)
.send(modifiedProposal)
.expect(200)
);
expect(requests.body).toEqual({ message: "Proposal updated successfully" });
Expand Down

0 comments on commit a2b24c2

Please sign in to comment.