|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import request from "supertest"; |
| 3 | +import app from "#app"; // Update this import based on your app's structure |
| 4 | +import connector from "#models/databaseUtil"; // Update this import |
| 5 | +import PracticalModel from "#models/practical"; // Update this import |
| 6 | + |
| 7 | +jest.mock("#util"); |
| 8 | + |
| 9 | +let server; |
| 10 | +let agent; |
| 11 | + |
| 12 | +beforeAll((done) => { |
| 13 | + server = app.listen(5000, () => { |
| 14 | + agent = request.agent(server); |
| 15 | + connector.set("debug", false); |
| 16 | + done(); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +function cleanUp(callback) { |
| 21 | + PracticalModel |
| 22 | + .deleteMany({}) |
| 23 | + .then(() => { |
| 24 | + connector.disconnect((DBerr) => { |
| 25 | + if (DBerr) console.log("Database disconnect error: ", DBerr); |
| 26 | + server.close((serverErr) => { |
| 27 | + if (serverErr) console.log(serverErr); |
| 28 | + callback(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +afterAll((done) => { |
| 35 | + cleanUp(done); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("Practical API", () => { |
| 39 | + it("should create a new practical", async () => { |
| 40 | + const response = await agent.post("/practical/create").send({ |
| 41 | + no: 1, |
| 42 | + title: "Sample Practical", |
| 43 | + type: "Experiment", |
| 44 | + hours: 2, |
| 45 | + cognitiveLevels: ["L2", "L3"], |
| 46 | + }); |
| 47 | + |
| 48 | + expect(response.status).toBe(201); |
| 49 | + expect(response.body.res).toMatch(/added user/); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("after creating a practical", () => { |
| 53 | + let practicalId; |
| 54 | + |
| 55 | + beforeEach(async () => { |
| 56 | + const response = await agent.post("/practical/create").send({ |
| 57 | + no: 1, |
| 58 | + title: "Sample Practical", |
| 59 | + type: "Experiment", |
| 60 | + hours: 2, |
| 61 | + cognitiveLevels: ["L2", "L3"], |
| 62 | + }); |
| 63 | + practicalId = response.body.res.match(/(\d+)/)[0]; |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(async () => { |
| 67 | + await PracticalModel.deleteOne({ _id: practicalId }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should list practical entities", async () => { |
| 71 | + const response = await agent.get("/practical/list"); |
| 72 | + expect(response.status).toBe(200); |
| 73 | + expect(response.body.res).toHaveLength(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should update a practical entity", async () => { |
| 77 | + const response = await agent.post("/practical/update").send({ |
| 78 | + id: practicalId, |
| 79 | + hours: 3, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(response.status).toBe(200); |
| 83 | + expect(response.body.res).toMatch(/updated practical/); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should delete a practical entity", async () => { |
| 87 | + const response = await agent.post(`/practical/delete/${practicalId}`); |
| 88 | + expect(response.status).toBe(200); |
| 89 | + expect(response.body.res).toMatch(/Deleted practical/); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments