mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
move json syntax error setting to top-level method
This commit is contained in:
@@ -41,6 +41,17 @@ async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise
|
|||||||
await expect(callback).rejects.toThrow(ApiResponseError);
|
await expect(callback).rejects.toThrow(ApiResponseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateGlobalFetchMockJsonToThrowSyntaxError() {
|
||||||
|
// @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;
|
||||||
|
}
|
||||||
|
|
||||||
describe("ApiBasedRepositoryLoader", () => {
|
describe("ApiBasedRepositoryLoader", () => {
|
||||||
let loader: ApiBasedRepositoryLoader;
|
let loader: ApiBasedRepositoryLoader;
|
||||||
|
|
||||||
@@ -122,14 +133,7 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
// The Passio API returns some invalid JSON if there is no data,
|
// The Passio API returns some invalid JSON if there is no data,
|
||||||
// so simulate a JSON parsing error
|
// so simulate a JSON parsing error
|
||||||
|
|
||||||
// @ts-ignore
|
updateGlobalFetchMockJsonToThrowSyntaxError();
|
||||||
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 assertAsyncCallbackThrowsApiResponseError(async () => {
|
||||||
await loader.fetchAndUpdateRouteDataForSystemId("263");
|
await loader.fetchAndUpdateRouteDataForSystemId("263");
|
||||||
@@ -160,14 +164,7 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
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
|
updateGlobalFetchMockJsonToThrowSyntaxError();
|
||||||
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 assertAsyncCallbackThrowsApiResponseError(async () => {
|
||||||
await loader.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId("263");
|
await loader.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId("263");
|
||||||
|
|||||||
Reference in New Issue
Block a user