mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
move fetch mock helpers to separate file
This commit is contained in:
@@ -13,50 +13,16 @@ import {
|
|||||||
fetchShuttleDataSuccessfulResponse
|
fetchShuttleDataSuccessfulResponse
|
||||||
} from "../jsonSnapshots/fetchShuttleData/fetchShuttleDataSuccessfulResponse";
|
} from "../jsonSnapshots/fetchShuttleData/fetchShuttleDataSuccessfulResponse";
|
||||||
import { fetchEtaDataSuccessfulResponse } from "../jsonSnapshots/fetchEtaData/fetchEtaDataSuccessfulResponse";
|
import { fetchEtaDataSuccessfulResponse } from "../jsonSnapshots/fetchEtaData/fetchEtaDataSuccessfulResponse";
|
||||||
|
import {
|
||||||
/**
|
resetGlobalFetchMockJson,
|
||||||
* Function to update behavior of the global `fetch` function.
|
updateGlobalFetchMockJson,
|
||||||
* Note that the Passio GO API returns status code 200 for failed responses.
|
updateGlobalFetchMockJsonToThrowSyntaxError
|
||||||
* @param obj
|
} from "../mockHelpers/fetchMockHelpers";
|
||||||
* @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({});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise<any>) {
|
async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise<any>) {
|
||||||
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;
|
||||||
|
|
||||||
|
|||||||
13
test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts
Normal file
13
test/loaders/TimedApiBasedRepositoryLoaderTests.test.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { beforeEach, describe } from "@jest/globals";
|
||||||
|
import { TimedApiBasedRepositoryLoader } from "../../src/loaders/TimedApiBasedRepositoryLoader";
|
||||||
|
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
||||||
|
import { resetGlobalFetchMockJson } from "../mockHelpers/fetchMockHelpers";
|
||||||
|
|
||||||
|
describe("TimedApiBasedRepositoryLoader", () => {
|
||||||
|
let loader: TimedApiBasedRepositoryLoader;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
loader = new TimedApiBasedRepositoryLoader(new UnoptimizedInMemoryRepository());
|
||||||
|
resetGlobalFetchMockJson();
|
||||||
|
});
|
||||||
|
});
|
||||||
41
test/mockHelpers/fetchMockHelpers.ts
Normal file
41
test/mockHelpers/fetchMockHelpers.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { jest } from "@jest/globals";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
export 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
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the global fetch function mock's JSON to return an empty object.
|
||||||
|
* @param obj
|
||||||
|
*/
|
||||||
|
export function resetGlobalFetchMockJson() {
|
||||||
|
updateGlobalFetchMockJson({});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user