Skip to content

Commit 4937987

Browse files
committed
fixed testcases for notification
1 parent 6970176 commit 4937987

File tree

4 files changed

+124
-128
lines changed

4 files changed

+124
-128
lines changed

controller/notification.js

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
11
import {
2-
createNotification,
3-
deleteNotificationById,
4-
listNotifications,
5-
updateNotificationById,
6-
} from "#services/notification";
7-
import { logger } from "#util";
8-
9-
async function addNotification(req, res) {
10-
const {
11-
data, title, type, from, filter,
12-
} = req.body;
13-
try {
14-
const newNotification = await createNotification({
15-
data, title, type, from, filter,
16-
});
17-
res.json({ res: `Added notification with ID: ${newNotification.id}`, id: newNotification.id });
18-
} catch (error) {
19-
logger.error("Error while inserting", error);
20-
res.status(500);
21-
res.json({ error: "Error while inserting in DB" });
22-
}
2+
createNotification,
3+
deleteNotificationById,
4+
listNotifications,
5+
updateNotificationById,
6+
} from "#services/notification";
7+
import { logger } from "#util";
8+
9+
async function addNotification(req, res) {
10+
const { data, title, type, from, filter } = req.body;
11+
try {
12+
const newNotification = await createNotification({
13+
data,
14+
title,
15+
type,
16+
from,
17+
filter,
18+
});
19+
res.json({
20+
res: `Added notification with ID: ${newNotification.id}`,
21+
id: newNotification.id,
22+
});
23+
} catch (error) {
24+
logger.error("Error while inserting", error);
25+
res.status(500);
26+
res.json({ error: "Error while inserting in DB" });
2327
}
24-
25-
async function updateNotification(req, res) {
26-
const { id } = req.params;
27-
const{
28-
...data
29-
} = req.body;
30-
try {
31-
await updateNotificationById(id, data);
32-
res.json({ res: `Updated notification with ID: ${id}` });
33-
} catch (error) {
34-
logger.error("Error while updating", error);
35-
res.status(500)
36-
res.json({ error: "Error while updating in DB" });
37-
}
28+
}
29+
30+
async function updateNotification(req, res) {
31+
const { id } = req.params;
32+
const { ...data } = req.body;
33+
try {
34+
await updateNotificationById(id, data);
35+
res.json({ res: `Updated notification with ID: ${id}` });
36+
} catch (error) {
37+
logger.error("Error while updating", error);
38+
res.status(500);
39+
res.json({ error: "Error while updating in DB" });
3840
}
39-
40-
async function getNotifications(req, res) {
41-
const filter = req.query;
42-
const notificationList = await listNotifications(filter);
43-
res.json({ res: notificationList });
41+
}
42+
43+
async function getNotifications(req, res) {
44+
const filter = req.query;
45+
const notificationList = await listNotifications(filter);
46+
res.json({ res: notificationList });
47+
}
48+
49+
async function deleteNotification(req, res) {
50+
const { id } = req.params;
51+
try {
52+
await deleteNotificationById(id);
53+
res.json({ res: `Deleted notification with ID: ${id}` });
54+
} catch (error) {
55+
logger.error("Error while deleting", error);
56+
res.status(500).json({ error: "Error while deleting from DB" });
4457
}
45-
46-
async function deleteNotification(req, res) {
47-
const { id } = req.params;
48-
try {
49-
await deleteNotificationById(id);
50-
res.json({ res: `Deleted notification with ID: ${id}` });
51-
} catch (error) {
52-
logger.error("Error while deleting", error);
53-
res.status(500).json({ error: "Error while deleting from DB" });
54-
}
55-
}
56-
57-
export default {
58-
addNotification,
59-
deleteNotification,
60-
getNotifications,
61-
updateNotification,
62-
};
63-
58+
}
59+
60+
export default {
61+
addNotification,
62+
deleteNotification,
63+
getNotifications,
64+
updateNotification,
65+
};

services/notification.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import Notification from "#models/notification";
33
import databaseError from "#error/database";
44

55
// Service function to create a new Notification entity
6-
export async function createNotification({
7-
data, title, type, from, filter,
8-
}) {
6+
export async function createNotification({ data, title, type, from, filter }) {
97
try {
108
const newNotification = await Notification.create({
11-
data, title, type, from, filter,
9+
data,
10+
title,
11+
type,
12+
from,
13+
filter,
1214
});
1315
return newNotification;
1416
} catch (error) {
@@ -19,7 +21,7 @@ export async function createNotification({
1921
// Service function to update a Notification entity by ID
2022
export async function updateNotificationById(id, data) {
2123
try {
22-
const updated = await Notification.findByIdAndUpdate({_id : id} , data);
24+
const updated = await Notification.update({ _id: id }, data);
2325
if (updated) {
2426
return updated;
2527
}

services/topic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export async function addNewTopic(title) {
55
const newTopic = await Topic.create({
66
title,
77
});
8-
if (newTopic.name === name) {
9-
return newAopic;
8+
if (newTopic.title === title) {
9+
return newTopic;
1010
}
1111
throw new databaseError.DataEntryError("Add Topic");
1212
}

test/routes/notification.test.js

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,93 @@
1-
import { jest } from "@jest/globals";
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
22
import notificationModel from "#models/notification";
33
import connector from "#models/databaseUtil"; // Import your Express app instance
44

55
jest.mock("#util");
6-
const {agent}= global;
7-
6+
const { agent } = global;
87

98
function cleanUp(callback) {
10-
notificationModel.remove({
11-
data: "Sample Notification",
12-
title: "Test Title",
13-
from: "YOUR_FACULTY_ID",
14-
type: "Student",
15-
filter: ["TARGETED_USER_ID"],
9+
notificationModel
10+
.remove({
11+
data: "Sample Notification",
12+
title: "Test Title",
13+
from: "64fc3c8bde9fa947ea1f412f",
14+
type: "Student",
15+
filter: ["64fc3c8bde9fa947ea1f412f"],
1616
})
1717
.then(() => {
18-
connector.disconnect((DBerr) => {
19-
if (DBerr) console.log("database disconnect error: ", DBerr);
20-
callback();
21-
});
22-
})
23-
18+
connector.disconnect((DBerr) => {
19+
if (DBerr) console.log("database disconnect error: ", DBerr);
20+
callback();
21+
});
22+
});
2423
}
2524

2625
afterAll((done) => {
2726
cleanUp(done);
2827
});
2928

3029
describe("Notification API", () => {
31-
let notificationId;
32-
3330
it("should create a new notification", async () => {
3431
const response = await agent.post("/notification/add").send({
3532
data: "Sample Notification",
3633
title: "Test Title",
37-
from: "YOUR_FACULTY_ID", // Use a valid Faculty ID
34+
from: "64fc3c8bde9fa947ea1f412f", // Use a valid Faculty ID
3835
type: "Student",
39-
filter: ["TARGETED_USER_ID"], // Use a valid User ID
36+
filter: ["64fc3c8bde9fa947ea1f412f"], // Use a valid User ID
4037
});
41-
4238
expect(response.status).toBe(200);
43-
expect(response.body.res).toMatch(/added notification/);
44-
45-
39+
expect(response.body.res).toMatch(/Added notification/);
40+
const notificationId = JSON.parse(response.res.text).id;
41+
await notificationModel.remove({ _id: notificationId });
4642
});
4743

4844
describe("after adding notification", () => {
4945
let notificationId;
5046
beforeEach(async () => {
51-
notificationId=await agent.post("notification/add").send({
52-
data: "Sample Notification",
53-
title: "Test Title",
54-
from: "YOUR_FACULTY_ID",
55-
type: "Student",
56-
filter: ["TARGETED_USER_ID"],
57-
});
58-
notificationId=JSON.parse(id.res.text).id;
47+
const id = await agent.post("/notification/add").send({
48+
data: "Sample Notification",
49+
title: "Test Title",
50+
from: "64fc3c8bde9fa947ea1f412f",
51+
type: "Student",
52+
filter: ["64fc3c8bde9fa947ea1f412f"],
53+
});
54+
notificationId = JSON.parse(id.res.text).id;
5955
});
6056
afterEach(async () => {
61-
await notificationModel.remove({
62-
data: "Sample Notification",
63-
title: "Test Title",
64-
from: "YOUR_FACULTY_ID",
65-
type: "Student",
66-
filter: ["TARGETED_USER_ID"],
67-
68-
});
57+
await notificationModel.remove({
58+
data: "Sample Notification",
59+
title: "Test Title",
60+
from: "64fc3c8bde9fa947ea1f412f",
61+
type: "Student",
62+
filter: ["64fc3c8bde9fa947ea1f412f"],
63+
});
6964
});
7065

7166
it("should update a notification entity", async () => {
72-
const response = await agent.post(`/notification/update/${notificationId}`).send({
67+
const response = await agent
68+
.post(`/notification/update/${notificationId}`)
69+
.send({
7370
data: "Updated Notification Data",
7471
title: "Updated Title",
75-
from: "YOUR_FACULTY_ID",
72+
from: "64fc3c8bde9fa947ea1f412f",
7673
type: "Faculty",
77-
filter: ["TARGETED_USER_ID"],
74+
filter: ["64fc3c8bde9fa947ea1f412f"],
7875
});
79-
80-
expect(response.status).toBe(200);
81-
expect(response.body.res).toMatch(/updated notification/);
82-
});
83-
84-
it("should list notification entities", async () => {
85-
const response = await agent.get("/notification/list").send({
86-
data: "Sample Notification",
87-
title: "Test Title",
88-
from: "YOUR_FACULTY_ID",
89-
type: "Student",
90-
filter: ["TARGETED_USER_ID"],
9176

92-
});
93-
expect(response.status).toBe(200);
94-
expect(response.body.res).toBeDefined();
95-
});
96-
97-
98-
})
77+
expect(response.status).toBe(200);
78+
expect(response.body.res).toMatch(/Updated notification/);
79+
});
9980

100-
81+
it("should list notification entities", async () => {
82+
const response = await agent.get("/notification/list").send({
83+
data: "Sample Notification",
84+
title: "Test Title",
85+
from: "64fc3c8bde9fa947ea1f412f",
86+
type: "Student",
87+
filter: ["64fc3c8bde9fa947ea1f412f"],
88+
});
89+
expect(response.status).toBe(200);
90+
expect(response.body.res).toBeDefined();
91+
});
92+
});
10193
});

0 commit comments

Comments
 (0)