mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
use waitForCondition function to poll for data update
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user