From 69525a643daefd445930b282b192431e0fb633df Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 3 Feb 2025 21:26:59 -0800 Subject: [PATCH] add another test to see if the data actually gets scheduled --- .../services/NotificationServiceTests.test.ts | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index a89dfa4..53d7423 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -3,6 +3,7 @@ import { NotificationService } from "../../src/services/NotificationService"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; import fs from "fs"; import { IEta } from "../../src/entities/entities"; +import { updateGlobalFetchMockJson } from "../testHelpers/fetchMockHelpers"; jest.mock("fs"); @@ -49,7 +50,21 @@ describe("NotificationService", () => { }) describe("scheduleNotification", () => { - it("sends a notification to given shuttle/stop ID when changed", async () => { + it("schedules the notification", async () => { + // arrange + const notificationData = { + deviceId: "1", + shuttleId: "1", + stopId: "1" + }; + + await notificationService.scheduleNotification(notificationData); + + const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData); + expect(isNotificationScheduled).toEqual(true); + }); + + it("sends and clears correct notification after ETA changed", async () => { // Arrange const eta: IEta = { shuttleId: "1", @@ -57,20 +72,26 @@ describe("NotificationService", () => { secondsRemaining: 120, }; + // Simulate 200 + empty object for successful push notification + updateGlobalFetchMockJson({}); + // Act - await notificationService.scheduleNotification({ + const notificationData = { deviceId: "1", shuttleId: eta.shuttleId, stopId: eta.stopId, - }); + } + await notificationService.scheduleNotification(notificationData); await repository.addOrUpdateEta(eta); // Assert - // ...that notification (fetch request) was sent + expect(fetch as jest.Mock).toHaveBeenCalledTimes(1); + const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData); + // No longer scheduled after being sent + expect(isNotificationScheduled).toBe(false); }); - it("clears the notification after delivering successfully", async () => { - + it("leaves notification in array if delivery unsuccessful", async () => { }); });