|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import notificationModel from "#models/notification"; |
| 3 | +import connector from "#models/databaseUtil"; // Import your Express app instance |
| 4 | + |
| 5 | +jest.mock("#util"); |
| 6 | +const {agent}= global; |
| 7 | + |
| 8 | + |
| 9 | +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"], |
| 16 | + }) |
| 17 | + .then(() => { |
| 18 | + connector.disconnect((DBerr) => { |
| 19 | + if (DBerr) console.log("database disconnect error: ", DBerr); |
| 20 | + callback(); |
| 21 | + }); |
| 22 | + }) |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +afterAll((done) => { |
| 27 | + cleanUp(done); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("Notification API", () => { |
| 31 | + let notificationId; |
| 32 | + |
| 33 | + it("should create a new notification", async () => { |
| 34 | + const response = await agent.post("/notification/add").send({ |
| 35 | + data: "Sample Notification", |
| 36 | + title: "Test Title", |
| 37 | + from: "YOUR_FACULTY_ID", // Use a valid Faculty ID |
| 38 | + type: "Student", |
| 39 | + filter: ["TARGETED_USER_ID"], // Use a valid User ID |
| 40 | + }); |
| 41 | + |
| 42 | + expect(response.status).toBe(200); |
| 43 | + expect(response.body.res).toMatch(/added notification/); |
| 44 | + |
| 45 | + |
| 46 | + }); |
| 47 | + |
| 48 | + describe("after adding notification", () => { |
| 49 | + let notificationId; |
| 50 | + 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; |
| 59 | + }); |
| 60 | + 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 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should update a notification entity", async () => { |
| 72 | + const response = await agent.post(`/notification/update/${notificationId}`).send({ |
| 73 | + data: "Updated Notification Data", |
| 74 | + title: "Updated Title", |
| 75 | + from: "YOUR_FACULTY_ID", |
| 76 | + type: "Faculty", |
| 77 | + filter: ["TARGETED_USER_ID"], |
| 78 | + }); |
| 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 | + |
| 92 | + }); |
| 93 | + expect(response.status).toBe(200); |
| 94 | + expect(response.body.res).toBeDefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + |
| 98 | + }) |
| 99 | + |
| 100 | + |
| 101 | +}); |
0 commit comments