add test cases for fetchANdUpdateRouteDataForSystemId

This commit is contained in:
2025-01-22 14:35:11 -08:00
parent f1fd774821
commit 420b71258e

View File

@@ -3,6 +3,7 @@ import { ApiBasedRepositoryLoader, ApiResponseError } from "../../src/loaders/Ap
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
import { fetchSystemDataSuccessfulResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataSuccessfulResponse"; import { fetchSystemDataSuccessfulResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataSuccessfulResponse";
import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse"; import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse";
import { fetchRouteDataSuccessfulResponse } from "../jsonSnapshots/fetchRouteData/fetchRouteDataSuccessfulResponse";
/** /**
* Function to update behavior of the global `fetch` function. * Function to update behavior of the global `fetch` function.
@@ -76,13 +77,40 @@ describe("ApiBasedRepositoryLoader", () => {
}); });
}); });
describe("fetchAndUpdateRouteDataForExistingSystems", () => {
it("updates route data in repository if there are systems and response received", async () => {
describe("fetchAndUpdateRouteDataForExistingSystemsInRepository", () => {
// TODO
// Add a test case to ensure that fetchAndUpdateRouteDataForSystemId
// was called for every available system ID
});
describe("fetchAndUpdateRouteDataForSystemId", () => {
it("updates route data in repository if there are systems and response received", async () => {
updateGlobalFetchMockJson(fetchRouteDataSuccessfulResponse);
await loader.fetchAndUpdateRouteDataForSystemId("263");
const routes = await loader.repository.getRoutesBySystemId("263");
expect(routes.length).toEqual(fetchRouteDataSuccessfulResponse.all.length)
}); });
it("throws the correct error if the API response contains no data", async () => { it("throws the correct error if the API response contains no data", async () => {
// The Passio API returns some invalid JSON if there is no data,
// so simulate a JSON parsing error
// @ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.reject(new SyntaxError("Unable to parse JSON")),
status: 200,
ok: true,
})
}) as jest.Mock;
await assertAsyncCallbackThrowsApiResponseError(async () => {
await loader.fetchAndUpdateRouteDataForSystemId("263");
});
}); });
}); });