mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
42 lines
977 B
TypeScript
42 lines
977 B
TypeScript
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;
|
|
}
|