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