add tests and implementation for notification deletes

This commit is contained in:
2025-03-27 11:18:43 -07:00
parent b0f04a9256
commit 51d66d8886
2 changed files with 52 additions and 7 deletions

View File

@@ -19,7 +19,7 @@ export class InMemoryNotificationRepository implements NotificationRepository {
*/ */
private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {} private deviceIdsToDeliverTo: { [key: string]: DeviceIdSecondsThresholdAssociation } = {}
private subscribers: Listener[] = []; private listeners: Listener[] = [];
async getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string) { async getAllNotificationsForShuttleAndStopId(shuttleId: string, stopId: string) {
const tuple = new TupleKey(shuttleId, stopId); const tuple = new TupleKey(shuttleId, stopId);
@@ -81,12 +81,34 @@ export class InMemoryNotificationRepository implements NotificationRepository {
return; return;
} }
const secondsThreshold = this.deviceIdsToDeliverTo[tupleKey.toString()][deviceId];
delete this.deviceIdsToDeliverTo[tupleKey.toString()][deviceId]; delete this.deviceIdsToDeliverTo[tupleKey.toString()][deviceId];
this.listeners.forEach((listener) => {
const event: NotificationEvent = {
event: 'delete',
notification: {
deviceId,
shuttleId,
stopId,
secondsThreshold
}
}
listener(event);
})
} }
public subscribeToNotificationChanges(listener: Listener): void { public subscribeToNotificationChanges(listener: Listener): void {
const index = this.listeners.findIndex((existingListener) => existingListener == listener);
if (index < 0) {
this.listeners.push(listener);
}
} }
public unsubscribeFromNotificationChanges(listener: Listener): void { public unsubscribeFromNotificationChanges(listener: Listener): void {
const index = this.listeners.findIndex((existingListener) => existingListener == listener);
if (index >= 0) {
this.listeners.splice(index, 1);
}
} }
} }

View File

@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it } from "@jest/globals"; import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { InMemoryNotificationRepository } from "../../src/repositories/InMemoryNotificationRepository"; import { InMemoryNotificationRepository } from "../../src/repositories/InMemoryNotificationRepository";
import { NotificationEvent } from "../../src/repositories/NotificationRepository";
describe("InMemoryNotificationRepository", () => { describe("InMemoryNotificationRepository", () => {
let repo: InMemoryNotificationRepository; let repo: InMemoryNotificationRepository;
@@ -71,16 +72,13 @@ describe("InMemoryNotificationRepository", () => {
describe("deleteNotificationIfExists", () => { describe("deleteNotificationIfExists", () => {
it("deletes the notification", async () => { it("deletes the notification", async () => {
await repo.addOrUpdateNotification(notification); await repo.addOrUpdateNotification(notification);
await repo.deleteNotificationIfExists({ await repo.deleteNotificationIfExists(notification);
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1"
});
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1"); const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
expect(result).toHaveLength(0); expect(result).toHaveLength(0);
}); });
it("does nothing if there's no notification", async () => { it("does nothing if there's no notification", async () => {
await expect(repo.deleteNotificationIfExists({ await expect(repo.deleteNotificationIfExists({
deviceId: "device1", deviceId: "device1",
@@ -89,4 +87,29 @@ describe("InMemoryNotificationRepository", () => {
})).resolves.not.toThrow(); })).resolves.not.toThrow();
}); });
}); });
describe("subscribeToNotificationChanges", () => {
it("calls subscribers when something is added", async () => {
});
it("calls subscribers when something is deleted", async () => {
await repo.addOrUpdateNotification(notification);
const mockCallback = jest.fn();
repo.subscribeToNotificationChanges(mockCallback);
await repo.deleteNotificationIfExists(notification);
expect(mockCallback).toHaveBeenCalledTimes(1);
const expectedEvent: NotificationEvent = {
event: 'delete',
notification: {
...notification,
},
};
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
});
})
}); });