|
| 1 | +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies |
| 2 | +import connector from "#models/databaseUtil"; |
| 3 | +import course from "#models/course"; |
| 4 | +import semesterModel from "#models/semester"; |
| 5 | +import departmentModel from "#models/department"; |
| 6 | +import moduleModel from "#models/module"; |
| 7 | +import practicalModel from "#models/practical"; |
| 8 | +import tutorialModel from "#models/tutorial"; |
| 9 | + |
| 10 | +jest.mock("#util"); |
| 11 | +const { agent } = global; |
| 12 | +let semesterId; |
| 13 | +let departmentId; |
| 14 | +let moduleIds; |
| 15 | +let practicalIds; |
| 16 | +let tutorialIds; |
| 17 | + |
| 18 | +function cleanUp(callback) { |
| 19 | + course |
| 20 | + .remove({ |
| 21 | + semester: semesterId, |
| 22 | + }) |
| 23 | + .then(() => { |
| 24 | + connector.disconnect((DBerr) => { |
| 25 | + if (DBerr) console.log("Database disconnect error: ", DBerr); |
| 26 | + }); |
| 27 | + callback(); |
| 28 | + }); |
| 29 | +} |
| 30 | +/* eslint-disable no-underscore-dangle */ |
| 31 | +async function getIds(callback) { |
| 32 | + semesterId = await semesterModel.read({}, 1); |
| 33 | + semesterId = semesterId.data[0]._id; |
| 34 | + departmentId = await departmentModel.read({}, 1); |
| 35 | + departmentId = departmentId.data[0]._id; |
| 36 | + moduleIds = await moduleModel.read({}, 3); |
| 37 | + moduleIds = moduleIds.data.flatMap((obj) => obj._id); |
| 38 | + practicalIds = await practicalModel.read({}, 3); |
| 39 | + practicalIds = practicalIds.data.flatMap((obj) => obj._id); |
| 40 | + tutorialIds = await tutorialModel.read({}, 3); |
| 41 | + tutorialIds = tutorialIds.data.flatMap((obj) => obj._id); |
| 42 | + callback(); |
| 43 | +} |
| 44 | + |
| 45 | +beforeAll((done) => { |
| 46 | + getIds(done); |
| 47 | +}); |
| 48 | + |
| 49 | +afterAll((done) => { |
| 50 | + cleanUp(done); |
| 51 | +}); |
| 52 | + |
| 53 | +describe("Course API", () => { |
| 54 | + it("should create course", async () => { |
| 55 | + const response = await agent.post("/course/create").send({ |
| 56 | + name: "my favourite course", |
| 57 | + code: "DDSABUB123", |
| 58 | + theoryHours: 12, |
| 59 | + tutorialHours: 4, |
| 60 | + practicalHours: 3, |
| 61 | + ISAMarks: 60, |
| 62 | + ESEMarks: 60, |
| 63 | + tutorialMarks: 20, |
| 64 | + practicalMarks: 20, |
| 65 | + semester: semesterId, |
| 66 | + department: departmentId, |
| 67 | + subType: "open", |
| 68 | + prerequisites: ["prereq"], // array of strings |
| 69 | + objective: "objective", |
| 70 | + outcomes: [ |
| 71 | + { |
| 72 | + outcome: "outcome 1", |
| 73 | + RBTLevel: ["L1", "L2"], |
| 74 | + }, |
| 75 | + ], // this is the modules from syllabus |
| 76 | + modules: moduleIds, |
| 77 | + practicals: practicalIds, |
| 78 | + tutorials: tutorialIds, |
| 79 | + reccTextbooks: ["random book"], |
| 80 | + refBooks: ["random book"], |
| 81 | + }); |
| 82 | + |
| 83 | + expect(response.status).toBe(200); |
| 84 | + expect(response.body.res).toMatch(/added course/); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("after adding course", () => { |
| 88 | + let id; |
| 89 | + beforeEach(async () => { |
| 90 | + id = await agent.post("/course/create").send({ |
| 91 | + name: "my second favourite course", |
| 92 | + code: "EEEABUB123", |
| 93 | + theoryHours: 12, |
| 94 | + tutorialHours: 4, |
| 95 | + practicalHours: 3, |
| 96 | + ISAMarks: 60, |
| 97 | + ESEMarks: 60, |
| 98 | + tutorialMarks: 20, |
| 99 | + practicalMarks: 20, |
| 100 | + semester: semesterId, |
| 101 | + department: departmentId, |
| 102 | + subType: "open", |
| 103 | + prerequisites: ["prereq"], // array of strings |
| 104 | + objective: "objective", |
| 105 | + outcomes: [ |
| 106 | + { |
| 107 | + outcome: "outcome 1", |
| 108 | + RBTLevel: ["L1", "L2"], |
| 109 | + }, |
| 110 | + ], // this is the modules from syllabus |
| 111 | + modules: moduleIds, |
| 112 | + practicals: practicalIds, |
| 113 | + tutorials: tutorialIds, |
| 114 | + reccTextbooks: ["random book"], |
| 115 | + refBooks: ["random book"], |
| 116 | + }); |
| 117 | + id = JSON.parse(id.res.text).id; |
| 118 | + }); |
| 119 | + |
| 120 | + afterEach(async () => { |
| 121 | + await course.remove({ |
| 122 | + code: "EEEABUB123", |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should read course", async () => { |
| 127 | + const response = await agent.get("/course/list"); |
| 128 | + expect(response.status).toBe(200); |
| 129 | + expect(response.body.res).toBeDefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should update course", async () => { |
| 133 | + const response = await agent |
| 134 | + .post(`/course/update/${id}`) |
| 135 | + .send({ subType: "professional" }); |
| 136 | + |
| 137 | + expect(response.status).toBe(200); |
| 138 | + expect(response.body.res).toMatch(/updated course with id/); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments