mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add tests for in-memory notification repository
This commit is contained in:
@@ -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", () => {
|
describe("InMemoryNotificationRepository", () => {
|
||||||
|
let repo: InMemoryNotificationRepository;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
repo = new InMemoryNotificationRepository();
|
||||||
|
})
|
||||||
|
|
||||||
|
const notification = {
|
||||||
|
deviceId: "device1",
|
||||||
|
shuttleId: "shuttle1",
|
||||||
|
stopId: "stop1",
|
||||||
|
secondsThreshold: 180
|
||||||
|
};
|
||||||
|
|
||||||
describe("getAllNotificationsForShuttleAndStopId", () => {
|
describe("getAllNotificationsForShuttleAndStopId", () => {
|
||||||
it("gets notifications correctly", async () => {
|
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 () => {
|
it("returns empty array if no notifications", async () => {
|
||||||
|
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
|
||||||
|
expect(result).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getSecondsThresholdForNotificationIfExists", () => {
|
describe("getSecondsThresholdForNotificationIfExists", () => {
|
||||||
it("gets the seconds threshold if exists", async () => {
|
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 () => {
|
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", () => {
|
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 () => {
|
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", () => {
|
describe("deleteNotificationIfExists", () => {
|
||||||
it("deletes the notification", async () => {
|
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 () => {
|
it("does nothing if there's no notification", async () => {
|
||||||
|
await expect(repo.deleteNotificationIfExists({
|
||||||
|
deviceId: "device1",
|
||||||
|
shuttleId: "shuttle1",
|
||||||
|
stopId: "stop1"
|
||||||
|
})).resolves.not.toThrow();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user