Files
project-inter-server/test/loaders/ApiBasedRepositoryLoaderTests.test.ts

109 lines
3.4 KiB
TypeScript

import { beforeEach, describe, expect, it, jest, test } from "@jest/globals";
import { ApiBasedRepositoryLoader, ApiResponseError } from "../../src/loaders/ApiBasedRepositoryLoader";
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
import { fetchSystemDataSuccessfulResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataSuccessfulResponse";
import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse";
/**
* Function to update behavior of the global `fetch` function.
* Note that the Passio GO API returns status code 200 for failed responses.
* @param obj
* @param status
*/
function updateGlobalFetchMockJson(
obj: any,
status: number = 200
) {
// @ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve(obj),
status,
ok: status.toString().startsWith("2"), // 200-level codes are OK
})
}) as jest.Mock;
}
/**
* Reset the global fetch function mock's JSON to return an empty object.
* @param obj
*/
function resetGlobalFetchMockJson() {
updateGlobalFetchMockJson({})
}
describe("ApiBasedRepositoryLoader", () => {
let loader: ApiBasedRepositoryLoader;
beforeEach(() => {
loader = new ApiBasedRepositoryLoader(new UnoptimizedInMemoryRepository());
resetGlobalFetchMockJson();
});
describe("fetchAndUpdateSystemData", () => {
it("updates system data in repository if response received", async () => {
const numberOfSystemsInResponse = fetchSystemDataSuccessfulResponse.all.length;
updateGlobalFetchMockJson(fetchSystemDataSuccessfulResponse);
await loader.fetchAndUpdateSystemData();
const systems = await loader.repository.getSystems();
if (loader.supportedSystemIds.length < numberOfSystemsInResponse) {
expect(systems).toHaveLength(loader.supportedSystemIds.length);
} else {
expect(systems).toHaveLength(numberOfSystemsInResponse);
}
});
it("throws the correct error if the API response contains no data", async () => {
updateGlobalFetchMockJson(fetchSystemDataFailedResponse);
// Jest is so confusing
await expect(async () => {
await loader.fetchAndUpdateSystemData();
}).rejects.toThrow(ApiResponseError);
});
});
describe("fetchAndUpdateRouteDataForExistingSystems", () => {
it("updates route data in repository if there are systems and response received", async () => {
});
it("throws the correct error if the API response contains no data", async () => {
});
});
describe("fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems", () => {
it("updates stop and polyline data if there are systems and response received", async () => {
});
it("throws the correct error if the API response contains no data", async () => {
})
});
describe("fetchAndUpdateShuttleDataForExistingSystems", () => {
it("updates shuttle data in repository if there are systems and response received", async () => {
});
it("throws the correct error if the API response contains no data", async () => {
});
});
describe("fetchAndUpdateEtaDataForExistingSystems", () => {
it("updates shuttle data in repository if there are systems and response received", async () => {
});
it("throws the correct error if the API response contains no data", async () => {
});
});
});