|
| 1 | +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies |
| 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 | +function cleanUp(callback) { |
| 9 | + notificationModel |
| 10 | + .remove({ |
| 11 | + data: "Sample Notification", |
| 12 | + title: "Test Title", |
| 13 | + from: "64fc3c8bde9fa947ea1f412f", |
| 14 | + type: "Student", |
| 15 | + filter: ["64fc3c8bde9fa947ea1f412f"], |
| 16 | + }) |
| 17 | + .then(() => { |
| 18 | + connector.disconnect((DBerr) => { |
| 19 | + if (DBerr) console.log("database disconnect error: ", DBerr); |
| 20 | + callback(); |
| 21 | + }); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +afterAll((done) => { |
| 26 | + cleanUp(done); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("Notification API", () => { |
| 30 | + it("should create a new notification", async () => { |
| 31 | + const response = await agent.post("/notification/add").send({ |
| 32 | + data: "Sample Notification", |
| 33 | + title: "Test Title", |
| 34 | + from: "64fc3c8bde9fa947ea1f412f", // Use a valid Faculty ID |
| 35 | + type: "Student", |
| 36 | + filter: ["64fc3c8bde9fa947ea1f412f"], // Use a valid User ID |
| 37 | + }); |
| 38 | + expect(response.status).toBe(200); |
| 39 | + expect(response.body.res).toMatch(/Added notification/); |
| 40 | + const notificationId = JSON.parse(response.res.text).id; |
| 41 | + await notificationModel.remove({ _id: notificationId }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("after adding notification", () => { |
| 45 | + let notificationId; |
| 46 | + beforeEach(async () => { |
| 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; |
| 55 | + }); |
| 56 | + afterEach(async () => { |
| 57 | + await notificationModel.remove({ |
| 58 | + data: "Sample Notification", |
| 59 | + title: "Test Title", |
| 60 | + from: "64fc3c8bde9fa947ea1f412f", |
| 61 | + type: "Student", |
| 62 | + filter: ["64fc3c8bde9fa947ea1f412f"], |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should update a notification entity", async () => { |
| 67 | + const response = await agent |
| 68 | + .post(`/notification/update/${notificationId}`) |
| 69 | + .send({ |
| 70 | + data: "Updated Notification Data", |
| 71 | + title: "Updated Title", |
| 72 | + from: "64fc3c8bde9fa947ea1f412f", |
| 73 | + type: "Faculty", |
| 74 | + filter: ["64fc3c8bde9fa947ea1f412f"], |
| 75 | + }); |
| 76 | + |
| 77 | + expect(response.status).toBe(200); |
| 78 | + expect(response.body.res).toMatch(/Updated notification/); |
| 79 | + }); |
| 80 | + |
| 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 | + }); |
| 93 | +}); |
0 commit comments