From 602ccf81398058ffaca5b0326396132bd2d41384 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 3 Feb 2025 23:20:42 -0800 Subject: [PATCH] update tests to wait for the publisher event to be sent --- .../services/NotificationServiceTests.test.ts | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index fb26785..980b0f7 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -19,19 +19,27 @@ SOcONbaf * Wait for a condition to become true until the timeout * is hit. * @param condition - * @param timeout - * @param interval + * @param timeoutMilliseconds + * @param intervalMilliseconds */ -async function waitForCondition(condition: () => boolean, timeout = 5000, interval = 500) { +async function waitForCondition(condition: () => boolean, timeoutMilliseconds = 5000, intervalMilliseconds = 500) { const startTime = Date.now(); while (!condition()) { - if (Date.now() - startTime > timeout) { + if (Date.now() - startTime > timeoutMilliseconds) { throw new Error("Timeout waiting for condition"); } - await new Promise((resolve) => setTimeout(resolve, interval)); + await new Promise((resolve) => setTimeout(resolve, intervalMilliseconds)); } } +/** + * Wait for a specified number of milliseconds. + * @param ms + */ +async function waitForMilliseconds(ms: number): Promise { + await new Promise((resolve) => setTimeout(resolve, ms)); +} + describe("NotificationService", () => { let repository: UnoptimizedInMemoryRepository let notificationService: NotificationService; @@ -130,11 +138,30 @@ describe("NotificationService", () => { expect(isSecondNotificationScheduled).toBe(false); }); + it("doesn't send notification if seconds threshold not exceeded", async () => { + // Arrange + const shuttle = await addMockShuttleToRepository(repository, "1"); + const stop = await addMockStopToRepository(repository, "1"); + const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop); + eta.secondsRemaining = notificationService.secondsThresholdForNotificationToFire + 100; + + updateGlobalFetchMockJson({}); + + // Act + await notificationService.scheduleNotification(notificationData1); + await repository.addOrUpdateEta(eta); + + // Assert + await waitForMilliseconds(500); + const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData1); + expect(isNotificationScheduled).toBe(true); + }); + it("leaves notification in array if delivery unsuccessful", async () => { // Arrange const shuttle = await addMockShuttleToRepository(repository, "1"); const stop = await addMockStopToRepository(repository, "1"); - const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop) + const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop) updateGlobalFetchMockJson({}, 400); @@ -145,7 +172,9 @@ describe("NotificationService", () => { // Assert // The notification should stay scheduled to be retried once // the ETA updates again - expect(notificationService.isNotificationScheduled(notificationData1)).toBe(true); + await waitForMilliseconds(500); + const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData1); + expect(isNotificationScheduled).toBe(true); }); });