|
1 | | -import { jest } from "@jest/globals"; |
| 1 | +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies |
2 | 2 | import notificationModel from "#models/notification"; |
3 | 3 | import connector from "#models/databaseUtil"; // Import your Express app instance |
4 | 4 |
|
5 | 5 | jest.mock("#util"); |
6 | | -const {agent}= global; |
7 | | - |
| 6 | +const { agent } = global; |
8 | 7 |
|
9 | 8 | 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"], |
16 | 16 | }) |
17 | 17 | .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 | + }); |
24 | 23 | } |
25 | 24 |
|
26 | 25 | afterAll((done) => { |
27 | 26 | cleanUp(done); |
28 | 27 | }); |
29 | 28 |
|
30 | 29 | describe("Notification API", () => { |
31 | | - let notificationId; |
32 | | - |
33 | 30 | it("should create a new notification", async () => { |
34 | 31 | const response = await agent.post("/notification/add").send({ |
35 | 32 | data: "Sample Notification", |
36 | 33 | title: "Test Title", |
37 | | - from: "YOUR_FACULTY_ID", // Use a valid Faculty ID |
| 34 | + from: "64fc3c8bde9fa947ea1f412f", // Use a valid Faculty ID |
38 | 35 | type: "Student", |
39 | | - filter: ["TARGETED_USER_ID"], // Use a valid User ID |
| 36 | + filter: ["64fc3c8bde9fa947ea1f412f"], // Use a valid User ID |
40 | 37 | }); |
41 | | - |
42 | 38 | 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 }); |
46 | 42 | }); |
47 | 43 |
|
48 | 44 | describe("after adding notification", () => { |
49 | 45 | let notificationId; |
50 | 46 | 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; |
59 | 55 | }); |
60 | 56 | 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 | + }); |
69 | 64 | }); |
70 | 65 |
|
71 | 66 | 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({ |
73 | 70 | data: "Updated Notification Data", |
74 | 71 | title: "Updated Title", |
75 | | - from: "YOUR_FACULTY_ID", |
| 72 | + from: "64fc3c8bde9fa947ea1f412f", |
76 | 73 | type: "Faculty", |
77 | | - filter: ["TARGETED_USER_ID"], |
| 74 | + filter: ["64fc3c8bde9fa947ea1f412f"], |
78 | 75 | }); |
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"], |
91 | 76 |
|
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 | + }); |
99 | 80 |
|
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 | + }); |
101 | 93 | }); |
0 commit comments