From a84cedd05aba689da951f21ff4f318f857678ded Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 27 Mar 2025 11:23:07 -0700 Subject: [PATCH] add test and implementation for addOrUpdate listeners --- .../InMemoryNotificationRepository.ts | 13 +++++++ ...nMemoryNotificationRepositoryTests.test.ts | 35 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/repositories/InMemoryNotificationRepository.ts b/src/repositories/InMemoryNotificationRepository.ts index e543e8c..d93af1f 100644 --- a/src/repositories/InMemoryNotificationRepository.ts +++ b/src/repositories/InMemoryNotificationRepository.ts @@ -66,6 +66,19 @@ export class InMemoryNotificationRepository implements NotificationRepository { } this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold; + this.listeners.forEach((listener: Listener) => { + const event: NotificationEvent = { + event: 'addOrUpdate', + notification: { + shuttleId, + stopId, + deviceId, + secondsThreshold + }, + } + + listener(event); + }) } async deleteNotificationIfExists({ diff --git a/test/repositories/InMemoryNotificationRepositoryTests.test.ts b/test/repositories/InMemoryNotificationRepositoryTests.test.ts index 1feb9c3..3419d96 100644 --- a/test/repositories/InMemoryNotificationRepositoryTests.test.ts +++ b/test/repositories/InMemoryNotificationRepositoryTests.test.ts @@ -90,7 +90,38 @@ describe("InMemoryNotificationRepository", () => { describe("subscribeToNotificationChanges", () => { it("calls subscribers when something is added", async () => { + const mockCallback = jest.fn(); + repo.subscribeToNotificationChanges(mockCallback); + await repo.addOrUpdateNotification(notification); + + const expectedEvent: NotificationEvent = { + event: 'addOrUpdate', + notification, + } + expect(mockCallback).toHaveBeenCalledTimes(1); + expect(mockCallback).toHaveBeenCalledWith(expectedEvent); + }); + + it("calls subscribers when something is updated", async () => { + const mockCallback = jest.fn(); + repo.subscribeToNotificationChanges(mockCallback); + + await repo.addOrUpdateNotification(notification); + + const updatedNotification = { + ...notification, + secondsThreshold: notification.secondsThreshold + 60, + }; + + await repo.addOrUpdateNotification(updatedNotification); + + const expectedEvent: NotificationEvent = { + event: 'addOrUpdate', + notification, + } + expect(mockCallback).toHaveBeenCalledTimes(2); + expect(mockCallback).toHaveBeenCalledWith(expectedEvent); }); it("calls subscribers when something is deleted", async () => { @@ -105,9 +136,7 @@ describe("InMemoryNotificationRepository", () => { const expectedEvent: NotificationEvent = { event: 'delete', - notification: { - ...notification, - }, + notification, }; expect(mockCallback).toHaveBeenCalledWith(expectedEvent); });