diff --git a/test/jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse.ts b/test/jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse.ts index 98465b4..a19e96f 100644 --- a/test/jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse.ts +++ b/test/jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse.ts @@ -1,4 +1,4 @@ -export const fetchStopAndPolylineDataForRoutesWithSystemId = { +export const fetchStopAndPolylineDataSuccessfulResponse = { "stops": { "ID2032": { "routeId": "54529", diff --git a/test/loaders/ApiBasedRepositoryLoaderTests.test.ts b/test/loaders/ApiBasedRepositoryLoaderTests.test.ts index 41da8be..c891dc7 100644 --- a/test/loaders/ApiBasedRepositoryLoaderTests.test.ts +++ b/test/loaders/ApiBasedRepositoryLoaderTests.test.ts @@ -5,6 +5,9 @@ import { fetchSystemDataSuccessfulResponse } from "../jsonSnapshots/fetchSystemD import { fetchSystemDataFailedResponse } from "../jsonSnapshots/fetchSystemData/fetchSystemDataFailedResponse"; import { fetchRouteDataSuccessfulResponse } from "../jsonSnapshots/fetchRouteData/fetchRouteDataSuccessfulResponse"; import { ISystem } from "../../src/entities/entities"; +import { + fetchStopAndPolylineDataSuccessfulResponse +} from "../jsonSnapshots/fetchStopAndPolylineData/fetchStopAndPolylineDataSuccessfulResponse"; /** * 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 () => { + 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 () => { + // @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"); + }); }) });