extract notification/eta data into function

This commit is contained in:
2025-02-03 23:13:27 -08:00
parent 0cd7c2fe47
commit 11ea0518fe

View File

@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { NotificationService } from "../../src/services/NotificationService";
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
import fs from "fs";
import { IEta, IShuttle } from "../../src/entities/entities";
import { IEta, IShuttle, IStop } from "../../src/entities/entities";
import { updateGlobalFetchMockJson } from "../testHelpers/fetchMockHelpers";
import { addMockShuttleToRepository, addMockStopToRepository } from "../testHelpers/repositorySetupHelpers";
@@ -68,6 +68,25 @@ describe("NotificationService", () => {
});
})
function generateNotificationDataAndEta(shuttle: IShuttle, stop: IStop) {
const eta: IEta = {
shuttleId: shuttle.id,
stopId: stop.id,
secondsRemaining: 120,
};
const notificationData1 = {
deviceId: "1",
shuttleId: eta.shuttleId,
stopId: eta.stopId,
}
const notificationData2 = {
...notificationData1,
deviceId: "2",
}
return { eta, notificationData1, notificationData2 };
}
describe("scheduleNotification", () => {
it("schedules the notification", async () => {
// arrange
@@ -88,21 +107,7 @@ describe("NotificationService", () => {
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const eta: IEta = {
shuttleId: shuttle.id,
stopId: stop.id,
secondsRemaining: 120,
};
const notificationData1 = {
deviceId: "1",
shuttleId: eta.shuttleId,
stopId: eta.stopId,
}
const notificationData2 = {
...notificationData1,
deviceId: "2",
}
const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop);
// Simulate 200 + empty object for successful push notification
updateGlobalFetchMockJson({});
@@ -115,7 +120,6 @@ describe("NotificationService", () => {
// Assert
// Because repository publisher calls subscriber without await
// wait for the change to occur first
await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1));
expect(fetch as jest.Mock).toHaveBeenCalledTimes(2);
@@ -128,27 +132,20 @@ describe("NotificationService", () => {
it("leaves notification in array if delivery unsuccessful", async () => {
// Arrange
const eta: IEta = {
shuttleId: "1",
stopId: "1",
secondsRemaining: 120,
};
const notificationData = {
deviceId: "1",
shuttleId: eta.shuttleId,
stopId: eta.stopId,
}
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop)
updateGlobalFetchMockJson({}, 400);
// Act
await notificationService.scheduleNotification(notificationData);
await notificationService.scheduleNotification(notificationData1);
await repository.addOrUpdateEta(eta);
// Assert
// The notification should stay scheduled to be retried once
// the ETA updates again
expect(notificationService.isNotificationScheduled(notificationData)).toBe(true);
expect(notificationService.isNotificationScheduled(notificationData1)).toBe(true);
});
});