mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add test and implementation for addOrUpdate listeners
This commit is contained in:
@@ -66,6 +66,19 @@ export class InMemoryNotificationRepository implements NotificationRepository {
|
||||
}
|
||||
|
||||
this.deviceIdsToDeliverTo[tuple.toString()][deviceId] = secondsThreshold;
|
||||
this.listeners.forEach((listener: Listener) => {
|
||||
const event: NotificationEvent = {
|
||||
event: 'addOrUpdate',
|
||||
notification: {
|
||||
shuttleId,
|
||||
stopId,
|
||||
deviceId,
|
||||
secondsThreshold
|
||||
},
|
||||
}
|
||||
|
||||
listener(event);
|
||||
})
|
||||
}
|
||||
|
||||
async deleteNotificationIfExists({
|
||||
|
||||
@@ -90,7 +90,38 @@ describe("InMemoryNotificationRepository", () => {
|
||||
|
||||
describe("subscribeToNotificationChanges", () => {
|
||||
it("calls subscribers when something is added", async () => {
|
||||
const mockCallback = jest.fn();
|
||||
repo.subscribeToNotificationChanges(mockCallback);
|
||||
|
||||
await repo.addOrUpdateNotification(notification);
|
||||
|
||||
const expectedEvent: NotificationEvent = {
|
||||
event: 'addOrUpdate',
|
||||
notification,
|
||||
}
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
|
||||
});
|
||||
|
||||
it("calls subscribers when something is updated", async () => {
|
||||
const mockCallback = jest.fn();
|
||||
repo.subscribeToNotificationChanges(mockCallback);
|
||||
|
||||
await repo.addOrUpdateNotification(notification);
|
||||
|
||||
const updatedNotification = {
|
||||
...notification,
|
||||
secondsThreshold: notification.secondsThreshold + 60,
|
||||
};
|
||||
|
||||
await repo.addOrUpdateNotification(updatedNotification);
|
||||
|
||||
const expectedEvent: NotificationEvent = {
|
||||
event: 'addOrUpdate',
|
||||
notification,
|
||||
}
|
||||
expect(mockCallback).toHaveBeenCalledTimes(2);
|
||||
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
|
||||
});
|
||||
|
||||
it("calls subscribers when something is deleted", async () => {
|
||||
@@ -105,9 +136,7 @@ describe("InMemoryNotificationRepository", () => {
|
||||
|
||||
const expectedEvent: NotificationEvent = {
|
||||
event: 'delete',
|
||||
notification: {
|
||||
...notification,
|
||||
},
|
||||
notification,
|
||||
};
|
||||
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user