|
| 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 tutorialModel from "#models/tutorial"; |
| 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 | + tutorialModel.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 tutorial functions", () => { |
| 36 | + it("create tutorial", async () => { |
| 37 | + const response = await agent.post("/tutorial/add").send({ |
| 38 | + no: "123", |
| 39 | + title: "abc", |
| 40 | + hours: "3", |
| 41 | + cognitiveLevel: ["L1"], |
| 42 | + }); |
| 43 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 44 | + expect(response.status).toBe(200); |
| 45 | + expect(response.body.res).toMatch(/added tutorial/); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("after creating a practical", () => { |
| 49 | + let tutorialId; |
| 50 | + beforeEach(async () => { |
| 51 | + const id = await agent.post("/tutorial/add").send({ |
| 52 | + no: "456", |
| 53 | + title: "dfg", |
| 54 | + hours: "3", |
| 55 | + cognitiveLevel: ["L1", "L2"], |
| 56 | + }); |
| 57 | + tutorialId = JSON.parse(id.res.text).id; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async () => { |
| 61 | + await tutorialModel.remove(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("read tutorial", async () => { |
| 65 | + const response = await agent |
| 66 | + .get("/tutorial/list") |
| 67 | + .send({ name: "dfg" }); |
| 68 | + expect(response.status).toBe(200); |
| 69 | + expect(response.body.res).toBeDefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("update tutorial", async () => { |
| 73 | + const response = await agent |
| 74 | + .post(`/tutorial/update/${tutorialId}`) |
| 75 | + .send({ no: "456" }); |
| 76 | + expect(response.headers["content-type"]).toMatch(/json/); |
| 77 | + expect(response.status).toBe(200); |
| 78 | + expect(response.body.res).toMatch(/tutorial updated/); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments