mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update tests to wait for the publisher event to be sent
This commit is contained in:
@@ -19,19 +19,27 @@ SOcONbaf
|
|||||||
* Wait for a condition to become true until the timeout
|
* Wait for a condition to become true until the timeout
|
||||||
* is hit.
|
* is hit.
|
||||||
* @param condition
|
* @param condition
|
||||||
* @param timeout
|
* @param timeoutMilliseconds
|
||||||
* @param interval
|
* @param intervalMilliseconds
|
||||||
*/
|
*/
|
||||||
async function waitForCondition(condition: () => boolean, timeout = 5000, interval = 500) {
|
async function waitForCondition(condition: () => boolean, timeoutMilliseconds = 5000, intervalMilliseconds = 500) {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
while (!condition()) {
|
while (!condition()) {
|
||||||
if (Date.now() - startTime > timeout) {
|
if (Date.now() - startTime > timeoutMilliseconds) {
|
||||||
throw new Error("Timeout waiting for condition");
|
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<void> {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
describe("NotificationService", () => {
|
describe("NotificationService", () => {
|
||||||
let repository: UnoptimizedInMemoryRepository
|
let repository: UnoptimizedInMemoryRepository
|
||||||
let notificationService: NotificationService;
|
let notificationService: NotificationService;
|
||||||
@@ -130,11 +138,30 @@ describe("NotificationService", () => {
|
|||||||
expect(isSecondNotificationScheduled).toBe(false);
|
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 () => {
|
it("leaves notification in array if delivery unsuccessful", async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
const shuttle = await addMockShuttleToRepository(repository, "1");
|
const shuttle = await addMockShuttleToRepository(repository, "1");
|
||||||
const stop = await addMockStopToRepository(repository, "1");
|
const stop = await addMockStopToRepository(repository, "1");
|
||||||
const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop)
|
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop)
|
||||||
|
|
||||||
updateGlobalFetchMockJson({}, 400);
|
updateGlobalFetchMockJson({}, 400);
|
||||||
|
|
||||||
@@ -145,7 +172,9 @@ describe("NotificationService", () => {
|
|||||||
// Assert
|
// Assert
|
||||||
// The notification should stay scheduled to be retried once
|
// The notification should stay scheduled to be retried once
|
||||||
// the ETA updates again
|
// the ETA updates again
|
||||||
expect(notificationService.isNotificationScheduled(notificationData1)).toBe(true);
|
await waitForMilliseconds(500);
|
||||||
|
const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
|
||||||
|
expect(isNotificationScheduled).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user