From 378dba0b5756d0167cd4d1df0d9ada50ba8d5dc2 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 28 Oct 2024 14:20:22 +0800 Subject: [PATCH] Add tests for notificationService --- .../services/notificationService.test.js | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Server/tests/services/notificationService.test.js diff --git a/Server/tests/services/notificationService.test.js b/Server/tests/services/notificationService.test.js new file mode 100644 index 000000000..b33bd21ed --- /dev/null +++ b/Server/tests/services/notificationService.test.js @@ -0,0 +1,95 @@ +import sinon from "sinon"; +import NotificationService from "../../service/notificationService.js"; +import { expect } from "chai"; + +describe("NotificationService", () => { + let emailService, db, logger, notificationService; + beforeEach(() => { + db = { + getNotificationsByMonitorId: sinon.stub(), + }; + emailService = { + buildAndSendEmail: sinon.stub(), + }; + logger = { + warn: sinon.stub(), + }; + + notificationService = new NotificationService(emailService, db, logger); + }); + afterEach(() => { + sinon.restore(); + }); + + describe("constructor", () => { + it("should create a new instance of NotificationService", () => { + expect(notificationService).to.be.an.instanceOf(NotificationService); + }); + }); + + describe("sendEmail", async () => { + it("should send an email notification with Up Template", async () => { + const networkResponse = { + monitor: { + name: "Test Monitor", + url: "http://test.com", + }, + status: true, + prevStatus: false, + }; + const address = "test@test.com"; + await notificationService.sendEmail(networkResponse, address); + expect(notificationService.emailService.buildAndSendEmail.calledOnce).to.be.true; + expect( + notificationService.emailService.buildAndSendEmail.calledWith( + "serverIsUpTemplate", + { monitor: "Test Monitor", url: "http://test.com" } + ) + ); + }); + it("should send an email notification with Down Template", async () => { + const networkResponse = { + monitor: { + name: "Test Monitor", + url: "http://test.com", + }, + status: false, + prevStatus: true, + }; + const address = "test@test.com"; + await notificationService.sendEmail(networkResponse, address); + expect(notificationService.emailService.buildAndSendEmail.calledOnce).to.be.true; + }); + it("should send an email notification with Up Template", async () => { + const networkResponse = { + monitor: { + name: "Test Monitor", + url: "http://test.com", + }, + status: true, + prevStatus: false, + }; + const address = "test@test.com"; + await notificationService.sendEmail(networkResponse, address); + expect(notificationService.emailService.buildAndSendEmail.calledOnce).to.be.true; + }); + }); + + describe("handleNotifications", async () => { + it("should handle notifications based on the network response", async () => { + notificationService.sendEmail = sinon.stub(); + notificationService.db.getNotificationsByMonitorId.resolves([ + { type: "email", address: "www.google.com" }, + ]); + await notificationService.handleNotifications({ monitorId: "123" }); + expect(notificationService.sendEmail.calledOnce).to.be.true; + }); + + it("should handle an error when getting notifications", async () => { + const testError = new Error("Test Error"); + notificationService.db.getNotificationsByMonitorId.rejects(testError); + await notificationService.handleNotifications({ monitorId: "123" }); + expect(notificationService.logger.warn.calledOnce).to.be.true; + }); + }); +});