2017-02-12 16:29:56 +01:00
|
|
|
// Mocking the global.fetch and global.Headers
|
|
|
|
global.fetch = jest.fn();
|
|
|
|
global.Headers = jest.fn();
|
|
|
|
|
2024-02-14 11:46:38 +01:00
|
|
|
// Helper to mock a success JSON response.
|
|
|
|
fetch.mockResponseSuccessJSON = (body) => {
|
2017-02-12 16:29:56 +01:00
|
|
|
fetch.mockImplementationOnce(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
json: () => Promise.resolve(JSON.parse(body)),
|
2024-01-19 23:15:20 +01:00
|
|
|
ok: true,
|
2017-02-12 16:29:56 +01:00
|
|
|
status: 200,
|
|
|
|
statusText: 'OK',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-02-14 11:46:38 +01:00
|
|
|
// Helper to mock a success text response.
|
|
|
|
fetch.mockResponseSuccessText = (body) => {
|
|
|
|
fetch.mockImplementationOnce(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
text: () => Promise.resolve(body),
|
|
|
|
ok: true,
|
|
|
|
status: 200,
|
|
|
|
statusText: 'OK',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-02-12 16:29:56 +01:00
|
|
|
// Helper to mock a failure response.
|
|
|
|
fetch.mockResponseFailure = () => {
|
|
|
|
fetch.mockImplementationOnce(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
status: 500,
|
|
|
|
statusText: 'Internal Error',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch.mockResponseCrash = () => {
|
|
|
|
fetch.mockImplementationOnce(() =>
|
2022-05-20 17:08:13 +02:00
|
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
2017-02-12 16:29:56 +01:00
|
|
|
Promise.reject({
|
|
|
|
status: 500,
|
|
|
|
statusText: 'Internal Error',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper to mock a timeout response.
|
|
|
|
fetch.mockResponseTimeout = () => {
|
|
|
|
fetch.mockImplementationOnce(() => {
|
|
|
|
const timeout = 1000;
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => setTimeout(resolve, timeout), timeout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|