From b252adb5d4a079adaff6d2c05b074f397b6525b9 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 3 Feb 2025 23:06:28 -0800 Subject: [PATCH] use waitForCondition function to poll for data update --- test/services/NotificationServiceTests.test.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/services/NotificationServiceTests.test.ts b/test/services/NotificationServiceTests.test.ts index 271a003..ba6b969 100644 --- a/test/services/NotificationServiceTests.test.ts +++ b/test/services/NotificationServiceTests.test.ts @@ -28,7 +28,8 @@ describe("NotificationService", () => { ...process.env, APNS_KEY_ID: "1", APNS_TEAM_ID: "1", - APNS_KEY_PATH: "./dummy-path.p8" + APNS_KEY_PATH: "./dummy-path.p8", + APNS_BUNDLE_ID: "dev.bchen.ProjectInter" }; (fs.readFileSync as jest.Mock).mockReturnValue(sampleKey); @@ -95,6 +96,20 @@ describe("NotificationService", () => { await repository.addOrUpdateEta(eta); // Assert + // Because repository publisher calls subscriber without await + // wait for the change to occur first + async function waitForCondition(condition: () => boolean, timeout = 5000, interval = 500) { + const startTime = Date.now(); + while (!condition()) { + if (Date.now() - startTime > timeout) { + throw new Error("Timeout waiting for condition"); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } + } + + await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1)); + expect(fetch as jest.Mock).toHaveBeenCalledTimes(2); const isFirstNotificationScheduled = notificationService.isNotificationScheduled(notificationData1); const isSecondNotificationScheduled = notificationService.isNotificationScheduled(notificationData2);