debian-mirror-gitlab/spec/frontend/__helpers__/backoff_helper.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 lines
885 B
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
/**
* A mock version of a commonUtils `backOff` to test multiple
* retries.
*
* Usage:
*
* ```
* import * as commonUtils from '~/lib/utils/common_utils';
* import { backoffMockImplementation } from '../../helpers/backoff_helper';
*
* beforeEach(() => {
* // ...
* jest.spyOn(commonUtils, 'backOff').mockImplementation(backoffMockImplementation);
* });
* ```
*
* @param {Function} callback
*/
2021-03-08 18:12:59 +05:30
export const backoffMockImplementation = (callback) => {
2020-10-24 23:57:45 +05:30
const q = new Promise((resolve, reject) => {
2021-03-08 18:12:59 +05:30
const stop = (arg) => (arg instanceof Error ? reject(arg) : resolve(arg));
2020-10-24 23:57:45 +05:30
const next = () => callback(next, stop);
// Define a timeout based on a mock timer
setTimeout(() => {
callback(next, stop);
});
});
// Run all resolved promises in chain
jest.runOnlyPendingTimers();
return q;
};
export default { backoffMockImplementation };