Skip to content

Commit 8c4eb43

Browse files
committed
created testcases for all endpoints
1 parent 730ad7f commit 8c4eb43

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

test/routes/group.test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2+
import request from "supertest";
3+
import app from "#app";
4+
import connector from "#models/databaseUtil";
5+
import groupModel from "#models/group";
6+
7+
jest.mock("#util");
8+
9+
let server;
10+
let agent;
11+
12+
beforeAll((done) => {
13+
server = app.listen(null, () => {
14+
agent = request.agent(server);
15+
connector.set("debug", false);
16+
done();
17+
});
18+
});
19+
20+
function cleanUp(callback) {
21+
groupModel
22+
.remove({
23+
title: "Group 1",
24+
student: "64fdc67feca8a69f01b33614",
25+
})
26+
.then(() => {
27+
connector.disconnect((DBerr) => {
28+
if (DBerr) console.log("Database disconnect error: ", DBerr);
29+
server.close((serverErr) => {
30+
if (serverErr) console.log(serverErr);
31+
callback();
32+
});
33+
});
34+
});
35+
}
36+
37+
afterAll((done) => {
38+
cleanUp(done);
39+
});
40+
41+
describe("group API", () => {
42+
it("should create group", async () => {
43+
const response = await agent.post("/group/add").send({
44+
title: "Group 1",
45+
student: "64fdc67feca8a69f01b33614",
46+
});
47+
48+
expect(response.status).toBe(200);
49+
expect(response.body.res).toMatch(/added group/);
50+
});
51+
52+
describe("after adding group", () => {
53+
let id;
54+
beforeEach(async () => {
55+
id = await agent.post("/group/add").send({
56+
title: "Group 1",
57+
student: "64fdc67feca8a69f01b33614",
58+
});
59+
id = JSON.parse(id.res.text).id;
60+
});
61+
62+
afterEach(async () => {
63+
await groupModel.remove({
64+
title: "Group 1",
65+
student: "64fdc67feca8a69f01b33614",
66+
});
67+
});
68+
69+
it("should read group", async () => {
70+
const response = await agent
71+
.get("/group/list")
72+
.send({ name: "Building A" });
73+
expect(response.status).toBe(200);
74+
expect(response.body.res).toBeDefined();
75+
});
76+
77+
it("should update group", async () => {
78+
const response = await agent
79+
.post(`/group/update/${id}`)
80+
.send({ title: "Group 1" }, { title: "Group 2" });
81+
82+
expect(response.status).toBe(200);
83+
expect(response.body.res).toMatch(/updated group/);
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)