0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 01:22:07 +01:00
wagtail/client/tests/mock-fetch.js
LB Johnston d7e52d1498 Add unit tests & small fixes to OrderableController
- Fix edge case where keyboard ordering and then drag drop ordering could get out of sync
- Ensure the sortable instance is cleaned up if the controller gets removed
- Relates to PR #11250 & Issue #10909
2024-01-20 10:05:21 +10:00

47 lines
1.0 KiB
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)),
ok: true,
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(() =>
// eslint-disable-next-line prefer-promise-reject-errors
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);
});
});
};