From f64fbf0ec77a894f1a948b5be1ba8347a57376a1 Mon Sep 17 00:00:00 2001 From: Jose Ivan Vargas Date: Thu, 16 Mar 2017 18:38:52 -0600 Subject: Added unit tests for the w.gl.utils.backOff promise --- changelogs/unreleased/add-test-backoff-util.yml | 4 ++ spec/javascripts/lib/utils/common_utils_spec.js | 68 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 changelogs/unreleased/add-test-backoff-util.yml diff --git a/changelogs/unreleased/add-test-backoff-util.yml b/changelogs/unreleased/add-test-backoff-util.yml new file mode 100644 index 00000000000..f3f3b99caec --- /dev/null +++ b/changelogs/unreleased/add-test-backoff-util.yml @@ -0,0 +1,4 @@ +--- +title: Added tests for the w.gl.utils.backOff promise +merge_request: +author: diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js index f4d3e77e515..56d92530e0a 100644 --- a/spec/javascripts/lib/utils/common_utils_spec.js +++ b/spec/javascripts/lib/utils/common_utils_spec.js @@ -1,3 +1,4 @@ +/* eslint-disable arrow-body-style*/ require('~/lib/utils/common_utils'); (() => { @@ -163,5 +164,72 @@ require('~/lib/utils/common_utils'); expect(gl.utils.isMetaClick(e)).toBe(true); }); }); + + describe('gl.utils.backOff', () => { + it('solves the promise from the callback', (done) => { + const expectedResponseValue = 'Success!'; + gl.utils.backOff((next, stop) => { + return new Promise((resolve) => { + resolve(expectedResponseValue); + }).then((resp) => { + stop(resp); + }); + }).then((respBackoff) => { + expect(respBackoff).toBe(expectedResponseValue); + done(); + }); + }); + + it('catches the rejected promise from the callback ', (done) => { + const errorMessage = 'Mistakes were made!'; + gl.utils.backOff((next, stop) => { + return new Promise((resolve, reject) => { + reject(new Error(errorMessage)); + }).then((resp) => { + stop(resp); + }).catch(err => stop(err)); + }).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) => { + let numberOfCalls = 1; + const expectedResponseValue = 'Success!'; + gl.utils.backOff((next, stop) => { + return new Promise((resolve) => { + resolve(expectedResponseValue); + }).then((resp) => { + if (numberOfCalls < 3) { + numberOfCalls += 1; + next(); + } else { + stop(resp); + } + }); + }).then((respBackoff) => { + expect(respBackoff).toBe(expectedResponseValue); + expect(numberOfCalls).toBe(3); + done(); + }); + }, 10000); + + it('rejects the backOff promise after timing out', (done) => { + const expectedResponseValue = 'Success!'; + gl.utils.backOff((next) => { + return new Promise((resolve) => { + resolve(expectedResponseValue); + }).then((resp) => { + setTimeout(next(resp), 5000); // it will time out + }); + }, 3000).catch((errBackoffResp) => { + expect(errBackoffResp instanceof Error).toBe(true); + expect(errBackoffResp.message).toBe('BACKOFF_TIMEOUT'); + done(); + }); + }, 10000); + }); }); })(); -- cgit v1.2.1 From 20cbfb482be95a6a0e9f80ff7576bd3126a03ae3 Mon Sep 17 00:00:00 2001 From: Jose Ivan Vargas Date: Thu, 16 Mar 2017 19:07:04 -0600 Subject: Removed unused response in promise --- spec/javascripts/lib/utils/common_utils_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js index 56d92530e0a..58fe92cc9cd 100644 --- a/spec/javascripts/lib/utils/common_utils_spec.js +++ b/spec/javascripts/lib/utils/common_utils_spec.js @@ -221,8 +221,8 @@ require('~/lib/utils/common_utils'); gl.utils.backOff((next) => { return new Promise((resolve) => { resolve(expectedResponseValue); - }).then((resp) => { - setTimeout(next(resp), 5000); // it will time out + }).then(() => { + setTimeout(next(), 5000); // it will time out }); }, 3000).catch((errBackoffResp) => { expect(errBackoffResp instanceof Error).toBe(true); -- cgit v1.2.1 From 58f2db49ee7efa320d459052c84d578af2495a85 Mon Sep 17 00:00:00 2001 From: Jose Ivan Vargas Date: Mon, 20 Mar 2017 16:06:40 -0600 Subject: Changed code style as to not to disable any eslint rules --- spec/javascripts/lib/utils/common_utils_spec.js | 27 ++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js index 58fe92cc9cd..d2e24eb7eb2 100644 --- a/spec/javascripts/lib/utils/common_utils_spec.js +++ b/spec/javascripts/lib/utils/common_utils_spec.js @@ -1,4 +1,3 @@ -/* eslint-disable arrow-body-style*/ require('~/lib/utils/common_utils'); (() => { @@ -168,13 +167,13 @@ require('~/lib/utils/common_utils'); describe('gl.utils.backOff', () => { it('solves the promise from the callback', (done) => { const expectedResponseValue = 'Success!'; - gl.utils.backOff((next, stop) => { - return new Promise((resolve) => { + gl.utils.backOff((next, stop) => ( + new Promise((resolve) => { resolve(expectedResponseValue); }).then((resp) => { stop(resp); - }); - }).then((respBackoff) => { + }) + )).then((respBackoff) => { expect(respBackoff).toBe(expectedResponseValue); done(); }); @@ -183,7 +182,7 @@ require('~/lib/utils/common_utils'); it('catches the rejected promise from the callback ', (done) => { const errorMessage = 'Mistakes were made!'; gl.utils.backOff((next, stop) => { - return new Promise((resolve, reject) => { + new Promise((resolve, reject) => { reject(new Error(errorMessage)); }).then((resp) => { stop(resp); @@ -198,8 +197,8 @@ require('~/lib/utils/common_utils'); it('solves the promise correctly after retrying a third time', (done) => { let numberOfCalls = 1; const expectedResponseValue = 'Success!'; - gl.utils.backOff((next, stop) => { - return new Promise((resolve) => { + gl.utils.backOff((next, stop) => ( + new Promise((resolve) => { resolve(expectedResponseValue); }).then((resp) => { if (numberOfCalls < 3) { @@ -208,8 +207,8 @@ require('~/lib/utils/common_utils'); } else { stop(resp); } - }); - }).then((respBackoff) => { + }) + )).then((respBackoff) => { expect(respBackoff).toBe(expectedResponseValue); expect(numberOfCalls).toBe(3); done(); @@ -218,13 +217,13 @@ require('~/lib/utils/common_utils'); it('rejects the backOff promise after timing out', (done) => { const expectedResponseValue = 'Success!'; - gl.utils.backOff((next) => { - return new Promise((resolve) => { + gl.utils.backOff(next => ( + new Promise((resolve) => { resolve(expectedResponseValue); }).then(() => { setTimeout(next(), 5000); // it will time out - }); - }, 3000).catch((errBackoffResp) => { + }) + ), 3000).catch((errBackoffResp) => { expect(errBackoffResp instanceof Error).toBe(true); expect(errBackoffResp.message).toBe('BACKOFF_TIMEOUT'); done(); -- cgit v1.2.1