From 764de079187f1eb1e00b41d66b569ca38a97993d Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:34:03 +0530 Subject: [PATCH 1/4] added testcases for tutorial --- test/routes/tutorial.test.js | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/routes/tutorial.test.js diff --git a/test/routes/tutorial.test.js b/test/routes/tutorial.test.js new file mode 100644 index 0000000..71ca168 --- /dev/null +++ b/test/routes/tutorial.test.js @@ -0,0 +1,77 @@ +import request from "supertest"; +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies +import app from "#app"; +import tutorialModel from "#models/tutorial"; +import connector from "#models/databaseUtil"; + +jest.mock("#util"); + +let server; +let agent; +beforeAll((done) => { + server = app.listen(null, () => { + agent = request.agent(server); + connector.set("debug", false); + done(); + }); +}); + +function cleanUp(callback) { + tutorialModel.remove({ no: "123git" }).then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database dissconnnect error: ", DBerr); + server.close((serverErr) => { + if (serverErr) console.log(serverErr); + callback(); + }); + }); + }); +} + +afterAll((done) => { + cleanUp(done); +}); + +describe("checking tutorial functions", () => { + it("create tutorial", async () => { + const response = await agent.post("/tutorial/add").send({ + no: "123", + title: "abc", + hours: "3", + cognitiveLevel: "abc", + }); + expect(response.headers["content-type"]).toMatch(/json/); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/added tutorial/); + }); + + beforeEach(async () => { + agent.post("/tutorial/add").send({ + no: "123", + title: "abc", + hours: "3", + cognitiveLevel: "abc", + }); + }); + + afterEach(async () => { + await tutorialModel.remove({ no: "123" }); + }); + + it("read tutorial", async () => { + const response = await agent + .get("/tutorial/list") + .send({ name: "xyz" }); + expect(response.status).toBe(200); + expect(response.body.res).toBeDefined(); + }); + + it("update tutorial", async () => { + const response = await agent + .post("/tutorial/update") + .send({ no: "123" }, { no: "123" }); + expect(response.headers["content-type"]).toMatch(/json/); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/tutorial updated/); + }); +}); From 18564933405bb42d0d65bd9b981fb5b0c285a67c Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:41:18 +0530 Subject: [PATCH 2/4] fixed enum values for tutorial testcase --- test/routes/tutorial.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/routes/tutorial.test.js b/test/routes/tutorial.test.js index 71ca168..ca6fe58 100644 --- a/test/routes/tutorial.test.js +++ b/test/routes/tutorial.test.js @@ -17,7 +17,7 @@ beforeAll((done) => { }); function cleanUp(callback) { - tutorialModel.remove({ no: "123git" }).then(() => { + tutorialModel.remove({ no: "123" }).then(() => { connector.disconnect((DBerr) => { if (DBerr) console.log("Database dissconnnect error: ", DBerr); server.close((serverErr) => { @@ -38,7 +38,7 @@ describe("checking tutorial functions", () => { no: "123", title: "abc", hours: "3", - cognitiveLevel: "abc", + cognitiveLevel: ["L1"], }); expect(response.headers["content-type"]).toMatch(/json/); expect(response.status).toBe(200); @@ -50,7 +50,7 @@ describe("checking tutorial functions", () => { no: "123", title: "abc", hours: "3", - cognitiveLevel: "abc", + cognitiveLevel: ["L1", "L2"], }); }); From ea816620a47d31a4b6087cb040476576f978116a Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:45:32 +0530 Subject: [PATCH 3/4] fixed update testcase --- test/routes/tutorial.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/routes/tutorial.test.js b/test/routes/tutorial.test.js index ca6fe58..e7241a4 100644 --- a/test/routes/tutorial.test.js +++ b/test/routes/tutorial.test.js @@ -45,13 +45,15 @@ describe("checking tutorial functions", () => { expect(response.body.res).toMatch(/added tutorial/); }); + let tutorialId; beforeEach(async () => { - agent.post("/tutorial/add").send({ + const id = agent.post("/tutorial/add").send({ no: "123", title: "abc", hours: "3", cognitiveLevel: ["L1", "L2"], }); + tutorialId = JSON.parse(id.res.text).id; }); afterEach(async () => { @@ -68,7 +70,7 @@ describe("checking tutorial functions", () => { it("update tutorial", async () => { const response = await agent - .post("/tutorial/update") + .post(`/tutorial/update+${tutorialId}`) .send({ no: "123" }, { no: "123" }); expect(response.headers["content-type"]).toMatch(/json/); expect(response.status).toBe(200); From f2b922eaeffa860f229c51abc8277f027e1ea045 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Sat, 23 Sep 2023 21:13:30 +0530 Subject: [PATCH 4/4] fixed testcase for tutorial, added timeout for testcases --- jest.config.js | 1 + test/routes/tutorial.test.js | 56 +++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/jest.config.js b/jest.config.js index a6f409c..8fc8075 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,7 @@ const config = { transform: { }, + "testTimeout": 15000, }; export default config; diff --git a/test/routes/tutorial.test.js b/test/routes/tutorial.test.js index e7241a4..1fa542c 100644 --- a/test/routes/tutorial.test.js +++ b/test/routes/tutorial.test.js @@ -44,36 +44,38 @@ describe("checking tutorial functions", () => { expect(response.status).toBe(200); expect(response.body.res).toMatch(/added tutorial/); }); - - let tutorialId; - beforeEach(async () => { - const id = agent.post("/tutorial/add").send({ - no: "123", - title: "abc", - hours: "3", - cognitiveLevel: ["L1", "L2"], + + describe("after creating a practical", () => { + let tutorialId; + beforeEach(async () => { + const id = await agent.post("/tutorial/add").send({ + no: "456", + title: "dfg", + hours: "3", + cognitiveLevel: ["L1", "L2"], + }); + tutorialId = JSON.parse(id.res.text).id; }); - tutorialId = JSON.parse(id.res.text).id; - }); - afterEach(async () => { - await tutorialModel.remove({ no: "123" }); - }); + afterEach(async () => { + await tutorialModel.remove(); + }); - it("read tutorial", async () => { - const response = await agent - .get("/tutorial/list") - .send({ name: "xyz" }); - expect(response.status).toBe(200); - expect(response.body.res).toBeDefined(); - }); + it("read tutorial", async () => { + const response = await agent + .get("/tutorial/list") + .send({ name: "dfg" }); + expect(response.status).toBe(200); + expect(response.body.res).toBeDefined(); + }); - it("update tutorial", async () => { - const response = await agent - .post(`/tutorial/update+${tutorialId}`) - .send({ no: "123" }, { no: "123" }); - expect(response.headers["content-type"]).toMatch(/json/); - expect(response.status).toBe(200); - expect(response.body.res).toMatch(/tutorial updated/); + it("update tutorial", async () => { + const response = await agent + .post(`/tutorial/update/${tutorialId}`) + .send({ no: "456" }); + expect(response.headers["content-type"]).toMatch(/json/); + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/tutorial updated/); + }); }); });