mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { afterEach, beforeAll, beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
import { TimedApiBasedRepositoryLoader } from "../../src/loaders/TimedApiBasedRepositoryLoader";
|
|
import { resetGlobalFetchMockJson } from "../testHelpers/fetchMockHelpers";
|
|
import { UnoptimizedInMemoryShuttleRepository } from "../../src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository";
|
|
import { ApiBasedShuttleRepositoryLoader } from "../../src/loaders/shuttle/ApiBasedShuttleRepositoryLoader";
|
|
|
|
describe("TimedApiBasedRepositoryLoader", () => {
|
|
let timedLoader: TimedApiBasedRepositoryLoader;
|
|
let spies: any;
|
|
|
|
beforeEach(() => {
|
|
jest.useFakeTimers();
|
|
jest.spyOn(global, "setTimeout");
|
|
resetGlobalFetchMockJson();
|
|
|
|
const mockLoader = new ApiBasedShuttleRepositoryLoader(
|
|
"1",
|
|
"1",
|
|
new UnoptimizedInMemoryShuttleRepository(),
|
|
);
|
|
timedLoader = new TimedApiBasedRepositoryLoader(
|
|
mockLoader,
|
|
);
|
|
|
|
spies = {
|
|
fetchAndUpdateAll: jest.spyOn(mockLoader, 'fetchAndUpdateAll'),
|
|
};
|
|
|
|
Object.values(spies).forEach((spy: any) => {
|
|
spy.mockResolvedValue(undefined);
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
jest.clearAllTimers();
|
|
})
|
|
|
|
describe("start", () => {
|
|
it("should update internal state, call data fetching methods, and start a timer", async () => {
|
|
await timedLoader.start();
|
|
expect(timedLoader["shouldBeRunning"]).toBe(true);
|
|
|
|
Object.values(spies).forEach((spy: any) => {
|
|
expect(spy).toHaveBeenCalled();
|
|
});
|
|
|
|
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), timedLoader.timeoutMs);
|
|
expect(timedLoader.timeoutMs).not.toBeUndefined();
|
|
});
|
|
|
|
it("does nothing if timer is already running", async () => {
|
|
await timedLoader.start();
|
|
await timedLoader.start();
|
|
|
|
Object.values(spies).forEach((spy: any) => {
|
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("stop", () => {
|
|
it("should update internal state", async () => {
|
|
timedLoader.stop();
|
|
expect(timedLoader['shouldBeRunning']).toBe(false);
|
|
});
|
|
});
|
|
});
|