From ecd30fdfabc9773f8aedd16836593fd9a4007717 Mon Sep 17 00:00:00 2001 From: Aryanzs Date: Sat, 9 Sep 2023 14:26:41 +0530 Subject: [PATCH 1/2] added testcases for practical model --- test/routes/practical.test.js | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/routes/practical.test.js diff --git a/test/routes/practical.test.js b/test/routes/practical.test.js new file mode 100644 index 00000000..6773a560 --- /dev/null +++ b/test/routes/practical.test.js @@ -0,0 +1,92 @@ +import { jest } from "@jest/globals"; +import request from "supertest"; +import app from "#app"; // Update this import based on your app's structure +import connector from "#models/databaseUtil"; // Update this import +import PracticalModel from "#models/practical"; // Update this import + +jest.mock("#util"); + +let server; +let agent; + +beforeAll((done) => { + server = app.listen(5000, () => { + agent = request.agent(server); + connector.set("debug", false); + done(); + }); +}); + +function cleanUp(callback) { + PracticalModel + .deleteMany({}) + .then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database disconnect error: ", DBerr); + server.close((serverErr) => { + if (serverErr) console.log(serverErr); + callback(); + }); + }); + }); +} + +afterAll((done) => { + cleanUp(done); +}); + +describe("Practical API", () => { + it("should create a new practical", async () => { + const response = await agent.post("/practical/create").send({ + no: 1, + title: "Sample Practical", + type: "Experiment", + hours: 2, + cognitiveLevels: ["L2", "L3"], + }); + + expect(response.status).toBe(201); + expect(response.body.res).toMatch(/added user/); + }); + + describe("after creating a practical", () => { + let practicalId; + + beforeEach(async () => { + const response = await agent.post("/practical/create").send({ + no: 1, + title: "Sample Practical", + type: "Experiment", + hours: 2, + cognitiveLevels: ["L2", "L3"], + }); + practicalId = response.body.res.match(/(\d+)/)[0]; + }); + + afterEach(async () => { + await PracticalModel.deleteOne({ _id: practicalId }); + }); + + it("should list practical entities", async () => { + const response = await agent.get("/practical/list"); + expect(response.status).toBe(200); + expect(response.body.res).toHaveLength(1); + }); + + it("should update a practical entity", async () => { + const response = await agent.post("/practical/update").send({ + id: practicalId, + hours: 3, + }); + + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/updated practical/); + }); + + it("should delete a practical entity", async () => { + const response = await agent.post(`/practical/delete/${practicalId}`); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/Deleted practical/); + }); + }); +}); From 4e788a65162b4fc1ea76ec14afe7d52f268dac98 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Wed, 13 Sep 2023 00:50:28 +0530 Subject: [PATCH 2/2] fixed issues with the testcase updated for making sure itll work with the current files --- test/routes/practical.test.js | 40 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/test/routes/practical.test.js b/test/routes/practical.test.js index 6773a560..0fd6124f 100644 --- a/test/routes/practical.test.js +++ b/test/routes/practical.test.js @@ -1,4 +1,4 @@ -import { jest } from "@jest/globals"; +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies import request from "supertest"; import app from "#app"; // Update this import based on your app's structure import connector from "#models/databaseUtil"; // Update this import @@ -10,7 +10,7 @@ let server; let agent; beforeAll((done) => { - server = app.listen(5000, () => { + server = app.listen(null, () => { agent = request.agent(server); connector.set("debug", false); done(); @@ -19,7 +19,7 @@ beforeAll((done) => { function cleanUp(callback) { PracticalModel - .deleteMany({}) + .remove({}) .then(() => { connector.disconnect((DBerr) => { if (DBerr) console.log("Database disconnect error: ", DBerr); @@ -45,48 +45,42 @@ describe("Practical API", () => { cognitiveLevels: ["L2", "L3"], }); - expect(response.status).toBe(201); - expect(response.body.res).toMatch(/added user/); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/Added Practical/); }); describe("after creating a practical", () => { let practicalId; beforeEach(async () => { - const response = await agent.post("/practical/create").send({ - no: 1, - title: "Sample Practical", - type: "Experiment", - hours: 2, - cognitiveLevels: ["L2", "L3"], + const id = await agent.post("/practical/create").send({ + no: 2, + title: "new practical", + type: "fun experiment", + hours: 5, + cognitiveLevels: ["L1", "L4"], }); - practicalId = response.body.res.match(/(\d+)/)[0]; + practicalId = JSON.parse(id.res.text).id; }); afterEach(async () => { - await PracticalModel.deleteOne({ _id: practicalId }); + await PracticalModel.remove(); }); it("should list practical entities", async () => { - const response = await agent.get("/practical/list"); + const response = await agent.get("/practical/list") + .send({ title: "new practical" }); expect(response.status).toBe(200); expect(response.body.res).toHaveLength(1); }); it("should update a practical entity", async () => { - const response = await agent.post("/practical/update").send({ - id: practicalId, + const response = await agent.post(`/practical/update/${practicalId}`).send({ hours: 3, }); expect(response.status).toBe(200); - expect(response.body.res).toMatch(/updated practical/); - }); - - it("should delete a practical entity", async () => { - const response = await agent.post(`/practical/delete/${practicalId}`); - expect(response.status).toBe(200); - expect(response.body.res).toMatch(/Deleted practical/); + expect(response.body.res).toMatch(/Updated Practical/); }); }); });