mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { beforeEach, describe, expect, it } from "@jest/globals";
|
|
import { InMemoryNotificationRepository } from "../../src/repositories/InMemoryNotificationRepository";
|
|
|
|
describe("InMemoryNotificationRepository", () => {
|
|
let repo: InMemoryNotificationRepository;
|
|
|
|
beforeEach(() => {
|
|
repo = new InMemoryNotificationRepository();
|
|
})
|
|
|
|
const notification = {
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1",
|
|
secondsThreshold: 180
|
|
};
|
|
|
|
describe("getAllNotificationsForShuttleAndStopId", () => {
|
|
it("gets notifications correctly", async () => {
|
|
await repo.addOrUpdateNotification(notification);
|
|
|
|
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0]).toEqual(notification);
|
|
});
|
|
|
|
it("returns empty array if no notifications", async () => {
|
|
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
|
|
expect(result).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("getSecondsThresholdForNotificationIfExists", () => {
|
|
it("gets the seconds threshold if exists", async () => {
|
|
await repo.addOrUpdateNotification(notification);
|
|
|
|
const result = await repo.getSecondsThresholdForNotificationIfExists({
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1"
|
|
});
|
|
expect(result).toBe(180);
|
|
});
|
|
|
|
it("returns null if there is no seconds threshold", async () => {
|
|
const result = await repo.getSecondsThresholdForNotificationIfExists({
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1"
|
|
});
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("addOrUpdateNotification", () => {
|
|
// Add/get flow is covered in getAllNotificationsForShuttleAndStopId
|
|
|
|
it("updates the seconds threshold if the notification exists already", async () => {
|
|
await repo.addOrUpdateNotification(notification);
|
|
await repo.addOrUpdateNotification({...notification, secondsThreshold: 300});
|
|
|
|
const result = await repo.getSecondsThresholdForNotificationIfExists({
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1"
|
|
});
|
|
expect(result).toBe(300);
|
|
});
|
|
});
|
|
|
|
describe("deleteNotificationIfExists", () => {
|
|
it("deletes the notification", async () => {
|
|
await repo.addOrUpdateNotification(notification);
|
|
await repo.deleteNotificationIfExists({
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1"
|
|
});
|
|
|
|
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it("does nothing if there's no notification", async () => {
|
|
await expect(repo.deleteNotificationIfExists({
|
|
deviceId: "device1",
|
|
shuttleId: "shuttle1",
|
|
stopId: "stop1"
|
|
})).resolves.not.toThrow();
|
|
});
|
|
});
|
|
});
|