From ead401a3b12f44218a2f0ce8422ec573bce8d1fc Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 27 Mar 2025 10:11:41 -0700 Subject: [PATCH] add tests for in-memory notification repository --- ...nMemoryNotificationRepositoryTests.test.ts | 63 ++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/test/repositories/InMemoryNotificationRepositoryTests.test.ts b/test/repositories/InMemoryNotificationRepositoryTests.test.ts index 3d4f18b..20ce245 100644 --- a/test/repositories/InMemoryNotificationRepositoryTests.test.ts +++ b/test/repositories/InMemoryNotificationRepositoryTests.test.ts @@ -1,43 +1,92 @@ -import { describe, it } from "@jest/globals"; +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", () => { - it("adds the notification", async () => { - - }); + // 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(); }); }); });