0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-22 02:18:39 +01:00
wagtail/client/tests/mock-fetch.js
Thibaud Colas af942a27e4
Reformat codebase with Prettier (#7912)
- Automated reformatting
- Manually change code where Prettier reformatting causes issues
- Revert "Disable Prettier formatting in CI for now"
2022-02-04 11:57:55 +00:00

45 lines
979 B
JavaScript

// Mocking the global.fetch and global.Headers
global.fetch = jest.fn();
global.Headers = jest.fn();
// Helper to mock a success response.
fetch.mockResponseSuccess = (body) => {
fetch.mockImplementationOnce(() =>
Promise.resolve({
json: () => Promise.resolve(JSON.parse(body)),
status: 200,
statusText: 'OK',
}),
);
};
// Helper to mock a failure response.
fetch.mockResponseFailure = () => {
fetch.mockImplementationOnce(() =>
Promise.resolve({
status: 500,
statusText: 'Internal Error',
}),
);
};
fetch.mockResponseCrash = () => {
fetch.mockImplementationOnce(() =>
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);
});
});
};