diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-24 15:09:34 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-24 15:09:34 +0300 |
commit | 0c27b33a8d0e2582b09579676677df5eaebfdbe2 (patch) | |
tree | ddc30f64f1a1da653cf89ff554f48376fde1d6ac /spec/frontend/helpers | |
parent | ff83f24eacc7c78884458d7261086b4f3a9547bb (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/helpers')
-rw-r--r-- | spec/frontend/helpers/backoff_helper.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/spec/frontend/helpers/backoff_helper.js b/spec/frontend/helpers/backoff_helper.js new file mode 100644 index 00000000000..e5c0308d3fb --- /dev/null +++ b/spec/frontend/helpers/backoff_helper.js @@ -0,0 +1,33 @@ +/** + * 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 + */ +export const backoffMockImplementation = callback => { + const q = new Promise((resolve, reject) => { + const stop = arg => (arg instanceof Error ? reject(arg) : resolve(arg)); + 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 }; |