summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/backoff_helper.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/__helpers__/backoff_helper.js')
-rw-r--r--spec/frontend/__helpers__/backoff_helper.js33
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..a971fcb0945
--- /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 };