mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add test cases for the new functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { beforeEach, describe, expect, test } from "@jest/globals";
|
import { beforeEach, describe, expect, jest, test } from "@jest/globals";
|
||||||
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
||||||
import {
|
import {
|
||||||
generateMockEtas,
|
generateMockEtas,
|
||||||
@@ -226,14 +226,36 @@ describe("UnoptimizedInMemoryRepository", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("subscribeToEtaChanges", () => {
|
describe("subscribeToEtaChanges", () => {
|
||||||
test("notifies listeners if etas has changed", async () => {
|
test("notifies listeners if etas have been added or changed", async () => {
|
||||||
|
const mockCallback = jest.fn(); // Jest mock function to simulate a listener
|
||||||
|
repository.subscribeToEtaChanges(mockCallback);
|
||||||
|
|
||||||
|
const mockEtas = generateMockEtas();
|
||||||
|
for (const eta of mockEtas) {
|
||||||
|
await repository.addOrUpdateEta(eta); // Trigger changes in ETAs
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(mockCallback).toHaveBeenCalledTimes(mockEtas.length);
|
||||||
|
expect(mockCallback).toHaveBeenCalledWith(mockEtas[0]); // First notification
|
||||||
|
expect(mockCallback).toHaveBeenCalledWith(mockEtas[mockEtas.length - 1]); // Last notification
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("unsubscribeFromEtaChanges", () => {
|
describe("unsubscribeFromEtaChanges", () => {
|
||||||
test("stops notifying listeners after etas have stopped changing", async () => {
|
test("stops notifying listeners after etas have stopped changing", async () => {
|
||||||
|
const mockCallback = jest.fn(); // Jest mock function to simulate a listener
|
||||||
|
repository.subscribeToEtaChanges(mockCallback);
|
||||||
|
|
||||||
|
const mockEtas = generateMockEtas();
|
||||||
|
await repository.addOrUpdateEta(mockEtas[0]);
|
||||||
|
|
||||||
|
repository.unsubscribeFromEtaChanges(mockCallback);
|
||||||
|
|
||||||
|
await repository.addOrUpdateEta(mockEtas[mockEtas.length - 1]);
|
||||||
|
|
||||||
|
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockCallback).toHaveBeenCalledWith(mockEtas[0]); // First notification
|
||||||
|
expect(mockCallback).not.toHaveBeenCalledWith(mockEtas[mockEtas.length - 1]); // Last notification
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user