Skip to content

Commit 2cb8ff9

Browse files
authored
Merge pull request #343 from tcet-opensource/278-testcases-for-assignment
added test
2 parents c76251a + ec07e81 commit 2cb8ff9

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

controller/assignment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function addAssignment(req, res) {
99
} = req.body;
1010
try {
1111
const newAssignment = await createAssignment(no, title, type, marks);
12-
res.json({ res: `added user ${newAssignment.id}`, id: newAssignment.id });
12+
res.json({ res: `added assignment ${newAssignment.id}`, id: newAssignment.id });
1313
} catch (error) {
1414
logger.error("Error while inserting", error);
1515
res.status(500);
@@ -24,7 +24,7 @@ async function updateAssignment(req, res) {
2424
} = req.body;
2525
try {
2626
await updateAssignmentById(id, data);
27-
res.json({ res: `updated assignment with id ${id}` });
27+
res.json({ res: `updated assignment ${id}` });
2828
} catch (error) {
2929
logger.error("Error while updating", error);
3030
res.status(500);

test/routes/assignment.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 assignmentModel from "#models/assignment";
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+
assignmentModel.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 assignment functions", () => {
36+
it("create assignment", async () => {
37+
const response = await agent.post("/assignment/add").send({
38+
no: 123,
39+
title: "xyz",
40+
type: "FA",
41+
marks: 100,
42+
});
43+
expect(response.headers["content-type"]).toMatch(/json/);
44+
expect(response.status).toBe(200);
45+
expect(response.body.res).toMatch(/added assignment/);
46+
});
47+
let id;
48+
beforeEach(async () => {
49+
id = await agent.post("/assignment/add").send({
50+
no: "123",
51+
title: "xyz",
52+
type: "FA",
53+
marks: 100,
54+
});
55+
id = JSON.parse(id.res.text).id;
56+
});
57+
58+
afterEach(async () => {
59+
await assignmentModel.remove({ no: "123" });
60+
});
61+
62+
it("read assignment", async () => {
63+
const response = await agent
64+
.get("/assignment/list")
65+
.send({ no: "123" });
66+
expect(response.status).toBe(200);
67+
expect(response.body.res).toBeDefined();
68+
});
69+
70+
it("update assignment", async () => {
71+
const response = await agent
72+
.post(`/assignment/update/${id}`)
73+
.send({ no: "123" }, { no: "345" });
74+
expect(response.headers["content-type"]).toMatch(/json/);
75+
expect(response.status).toBe(200);
76+
expect(response.body.res).toMatch(/updated assignment/);
77+
});
78+
});

0 commit comments

Comments
 (0)