mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
update tests and index with updated scheduler interface
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
||||
import { ETANotificationScheduler } from "../../../src/notifications/schedulers/ETANotificationScheduler";
|
||||
import { UnoptimizedInMemoryShuttleRepository } from "../../../src/repositories/UnoptimizedInMemoryShuttleRepository";
|
||||
import http2 from "http2";
|
||||
import { IEta, IShuttle, IStop } from "../../../src/entities/entities";
|
||||
import { addMockShuttleToRepository, addMockStopToRepository } from "../../testHelpers/repositorySetupHelpers";
|
||||
import { AppleNotificationSender } from "../../../src/notifications/senders/AppleNotificationSender";
|
||||
@@ -17,23 +16,6 @@ function mockNotificationSenderMethods(shouldSimulateNotificationSend: boolean)
|
||||
MockAppleNotificationSender.prototype.sendNotificationImmediately = jest.fn(async () => shouldSimulateNotificationSend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a condition to become true until the timeout
|
||||
* is hit.
|
||||
* @param condition
|
||||
* @param timeoutMilliseconds
|
||||
* @param intervalMilliseconds
|
||||
*/
|
||||
async function waitForCondition(condition: () => boolean, timeoutMilliseconds = 5000, intervalMilliseconds = 500) {
|
||||
const startTime = Date.now();
|
||||
while (!condition()) {
|
||||
if (Date.now() - startTime > timeoutMilliseconds) {
|
||||
throw new Error("Timeout waiting for condition");
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMilliseconds));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a specified number of milliseconds.
|
||||
* @param ms
|
||||
@@ -60,6 +42,7 @@ describe("ETANotificationScheduler", () => {
|
||||
notificationRepository,
|
||||
appleNotificationSender
|
||||
);
|
||||
notificationService.startListeningForUpdates();
|
||||
});
|
||||
|
||||
function generateNotificationDataAndEta(shuttle: IShuttle, stop: IStop) {
|
||||
@@ -97,9 +80,8 @@ describe("ETANotificationScheduler", () => {
|
||||
await shuttleRepository.addOrUpdateEta(eta);
|
||||
|
||||
// Assert
|
||||
// Because repository publisher calls subscriber without await
|
||||
// wait for the change to occur first
|
||||
await waitForCondition(() => !notificationRepository.isNotificationScheduled(notificationData1));
|
||||
// Wait for the callback to actually be called
|
||||
await waitForMilliseconds(1000);
|
||||
|
||||
const isFirstNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1);
|
||||
const isSecondNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData2);
|
||||
@@ -132,12 +114,18 @@ describe("ETANotificationScheduler", () => {
|
||||
const stop = await addMockStopToRepository(shuttleRepository, "1");
|
||||
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop)
|
||||
|
||||
// replace the old notification scheduler with a new one
|
||||
// detach the old callback method from the shuttle repo
|
||||
notificationService.stopListeningForUpdates();
|
||||
|
||||
mockNotificationSenderMethods(false);
|
||||
const updatedNotificationSender = new MockAppleNotificationSender(false);
|
||||
notificationService = new ETANotificationScheduler(
|
||||
shuttleRepository,
|
||||
new InMemoryNotificationRepository(),
|
||||
new MockAppleNotificationSender(),
|
||||
)
|
||||
notificationRepository,
|
||||
updatedNotificationSender
|
||||
);
|
||||
notificationService.startListeningForUpdates();
|
||||
|
||||
// Act
|
||||
await notificationRepository.addOrUpdateNotification(notificationData1);
|
||||
|
||||
Reference in New Issue
Block a user