use waitForCondition function to poll for data update

This commit is contained in:
2025-02-03 23:06:28 -08:00
parent 64931e7546
commit b252adb5d4

View File

@@ -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);