add json snapshot and test cases for method

This commit is contained in:
2025-01-22 14:56:02 -08:00
parent b2be3ae583
commit acc8d562c3
2 changed files with 33 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
export const fetchStopAndPolylineDataForRoutesWithSystemId = { export const fetchStopAndPolylineDataSuccessfulResponse = {
"stops": { "stops": {
"ID2032": { "ID2032": {
"routeId": "54529", "routeId": "54529",

View File

@@ -5,6 +5,9 @@ import { fetchSystemDataSuccessfulResponse } from "../jsonSnapshots/fetchSystemD
import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse"; import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse";
import { fetchRouteDataSuccessfulResponse } from "../jsonSnapshots/fetchRouteData/fetchRouteDataSuccessfulResponse"; import { fetchRouteDataSuccessfulResponse } from "../jsonSnapshots/fetchRouteData/fetchRouteDataSuccessfulResponse";
import { ISystem } from "../../src/entities/entities"; import { ISystem } from "../../src/entities/entities";
import {
fetchStopAndPolylineDataSuccessfulResponse
} from "../jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse";
/** /**
* Function to update behavior of the global `fetch` function. * Function to update behavior of the global `fetch` function.
@@ -134,13 +137,41 @@ describe("ApiBasedRepositoryLoader", () => {
}); });
}); });
describe("fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystems", () => { describe("fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId", () => {
it("updates stop and polyline data if there are systems and response received", async () => { it("updates stop and polyline data if there are systems and response received", async () => {
updateGlobalFetchMockJson(fetchStopAndPolylineDataSuccessfulResponse);
const stopsArray = Object.values(fetchStopAndPolylineDataSuccessfulResponse.stops);
await loader.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId("263");
const stops = await loader.repository.getStopsBySystemId("263");
expect(stops.length).toEqual(stopsArray.length);
await Promise.all(stops.map(async (stop) => {
const orderedStops = await loader.repository.getOrderedStopsByStopId(stop.id)
expect(orderedStops.length).toBeGreaterThan(0);
}));
const routes = await loader.repository.getRoutesBySystemId("263");
routes.forEach((route) => {
expect(route.polylineCoordinates.length).toBeGreaterThan(0);
});
}); });
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 () => {
// @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.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId("263");
});
}) })
}); });