|
| 1 | +import request from "supertest"; |
| 2 | +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies |
| 3 | +import app from "#app"; |
| 4 | +import assignmentModel from "#models/assignment"; |
| 5 | +import connector from "#models/databaseUtil"; |
| 6 | + |
| 7 | +jest.mock("#util"); |
| 8 | + |
| 9 | +let server; |
| 10 | +let agent; |
| 11 | +beforeAll((done) => { |
| 12 | + server = app.listen(null, () => { |
| 13 | + agent = request.agent(server); |
| 14 | + connector.set("debug", false); |
| 15 | + done(); |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +function cleanUp(callback) { |
| 20 | + assignmentModel.remove({ no: "123" }).then(() => { |
| 21 | + connector.disconnect((DBerr) => { |
| 22 | + if (DBerr) console.log("Database dissconnnect error: ", DBerr); |
| 23 | + server.close((serverErr) => { |
| 24 | + if (serverErr) console.log(serverErr); |
| 25 | + callback(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +afterAll((done) => { |
| 32 | + cleanUp(done); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("checking assignment functions", () => { |
| 36 | + it("create assignment", async () => { |
| 37 | + const response = await agent.post("/assignment/add").send({ |
| 38 | + no: "123", |
| 39 | + agencyName: "abc", |
| 40 | + dateofAccreditation: "2023-06-18T14:11:30Z", |
| 41 | + dateofExpiry: "2023-05-28T14:10:30Z", |
| 42 | + }); |
| 43 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 44 | + expect(response.status).toBe(200); |
| 45 | + expect(response.body.res).toMatch(/added assignment/); |
| 46 | + }); |
| 47 | + let id; |
| 48 | + beforeEach(async () => { |
| 49 | + id = await agent.post("/assignment/add").send({ |
| 50 | + no: "123", |
| 51 | + title: "xyz", |
| 52 | + type: "abc", |
| 53 | + marks: 100, |
| 54 | + }); |
| 55 | + id = JSON.parse(id.res.text).id; |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(async () => { |
| 59 | + await assignmentModel.remove({ no: "123" }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("read assignment", async () => { |
| 63 | + const response = await agent |
| 64 | + .get("/assignment/list") |
| 65 | + .send({ no: "123" }); |
| 66 | + expect(response.status).toBe(200); |
| 67 | + expect(response.body.res).toBeDefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("update assignment", async () => { |
| 71 | + const response = await agent |
| 72 | + .post(`/assignment/update/${id}`) |
| 73 | + .send({ no: "123" }, { no: "345" }); |
| 74 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 75 | + expect(response.status).toBe(200); |
| 76 | + expect(response.body.res).toMatch(/assignment updated/); |
| 77 | + }); |
| 78 | +}); |
0 commit comments