summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/common_utils_spec.js
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/utils/common_utils_spec.js')
-rw-r--r--spec/frontend/lib/utils/common_utils_spec.js69
1 files changed, 31 insertions, 38 deletions
diff --git a/spec/frontend/lib/utils/common_utils_spec.js b/spec/frontend/lib/utils/common_utils_spec.js
index 0be0bf89210..763a9bd30fe 100644
--- a/spec/frontend/lib/utils/common_utils_spec.js
+++ b/spec/frontend/lib/utils/common_utils_spec.js
@@ -266,15 +266,18 @@ describe('common_utils', () => {
});
describe('debounceByAnimationFrame', () => {
- it('debounces a function to allow a maximum of one call per animation frame', (done) => {
+ it('debounces a function to allow a maximum of one call per animation frame', () => {
const spy = jest.fn();
const debouncedSpy = commonUtils.debounceByAnimationFrame(spy);
- window.requestAnimationFrame(() => {
- debouncedSpy();
- debouncedSpy();
+
+ return new Promise((resolve) => {
window.requestAnimationFrame(() => {
- expect(spy).toHaveBeenCalledTimes(1);
- done();
+ debouncedSpy();
+ debouncedSpy();
+ window.requestAnimationFrame(() => {
+ expect(spy).toHaveBeenCalledTimes(1);
+ resolve();
+ });
});
});
});
@@ -372,28 +375,24 @@ describe('common_utils', () => {
jest.spyOn(window, 'setTimeout');
});
- it('solves the promise from the callback', (done) => {
+ it('solves the promise from the callback', () => {
const expectedResponseValue = 'Success!';
- commonUtils
+ return commonUtils
.backOff((next, stop) =>
new Promise((resolve) => {
resolve(expectedResponseValue);
- })
- .then((resp) => {
- stop(resp);
- })
- .catch(done.fail),
+ }).then((resp) => {
+ stop(resp);
+ }),
)
.then((respBackoff) => {
expect(respBackoff).toBe(expectedResponseValue);
- done();
- })
- .catch(done.fail);
+ });
});
- it('catches the rejected promise from the callback ', (done) => {
+ it('catches the rejected promise from the callback ', () => {
const errorMessage = 'Mistakes were made!';
- commonUtils
+ return commonUtils
.backOff((next, stop) => {
new Promise((resolve, reject) => {
reject(new Error(errorMessage));
@@ -406,39 +405,34 @@ describe('common_utils', () => {
.catch((errBackoffResp) => {
expect(errBackoffResp instanceof Error).toBe(true);
expect(errBackoffResp.message).toBe(errorMessage);
- done();
});
});
- it('solves the promise correctly after retrying a third time', (done) => {
+ it('solves the promise correctly after retrying a third time', () => {
let numberOfCalls = 1;
const expectedResponseValue = 'Success!';
- commonUtils
+ return commonUtils
.backOff((next, stop) =>
- Promise.resolve(expectedResponseValue)
- .then((resp) => {
- if (numberOfCalls < 3) {
- numberOfCalls += 1;
- next();
- jest.runOnlyPendingTimers();
- } else {
- stop(resp);
- }
- })
- .catch(done.fail),
+ Promise.resolve(expectedResponseValue).then((resp) => {
+ if (numberOfCalls < 3) {
+ numberOfCalls += 1;
+ next();
+ jest.runOnlyPendingTimers();
+ } else {
+ stop(resp);
+ }
+ }),
)
.then((respBackoff) => {
const timeouts = window.setTimeout.mock.calls.map(([, timeout]) => timeout);
expect(timeouts).toEqual([2000, 4000]);
expect(respBackoff).toBe(expectedResponseValue);
- done();
- })
- .catch(done.fail);
+ });
});
- it('rejects the backOff promise after timing out', (done) => {
- commonUtils
+ it('rejects the backOff promise after timing out', () => {
+ return commonUtils
.backOff((next) => {
next();
jest.runOnlyPendingTimers();
@@ -449,7 +443,6 @@ describe('common_utils', () => {
expect(timeouts).toEqual([2000, 4000, 8000, 16000, 32000, 32000]);
expect(errBackoffResp instanceof Error).toBe(true);
expect(errBackoffResp.message).toBe('BACKOFF_TIMEOUT');
- done();
});
});
});