diff options
author | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-05-11 12:48:24 +0100 |
---|---|---|
committer | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-05-11 12:48:24 +0100 |
commit | dbab076b05e4bbd6faa9f59cf07401de76081fd5 (patch) | |
tree | 0b935bf29e1a1ff42410106471edae8f36b3491f /spec/javascripts/lib | |
parent | 4a4b586aa0cd1298d515fb172a7bd90ff3591970 (diff) | |
download | gitlab-ce-dbab076b05e4bbd6faa9f59cf07401de76081fd5.tar.gz |
improved mockservicecall and fixed up a settimeout
Diffstat (limited to 'spec/javascripts/lib')
-rw-r--r-- | spec/javascripts/lib/utils/poll_spec.js | 49 |
1 files changed, 17 insertions, 32 deletions
diff --git a/spec/javascripts/lib/utils/poll_spec.js b/spec/javascripts/lib/utils/poll_spec.js index bba7fc387ff..b9e1cc005fb 100644 --- a/spec/javascripts/lib/utils/poll_spec.js +++ b/spec/javascripts/lib/utils/poll_spec.js @@ -14,12 +14,17 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => { timer(); }; -function mockServiceCall(service, mockCall) { +function mockServiceCall(service, response, shouldFail) { + const action = shouldFail ? Promise.reject : Promise.resolve; + const responseObject = response; + + if (!responseObject.headers) responseObject.headers = {}; + service.fetch.calls.reset(); - service.fetch.and.callFake(mockCall); + service.fetch.and.callFake(() => action(responseObject)); } -describe('Poll', () => { +fdescribe('Poll', () => { const service = jasmine.createSpyObj('service', ['fetch']); let callbacks; @@ -33,11 +38,8 @@ describe('Poll', () => { spyOn(callbacks, 'error'); }); - it('calls the success callback when no header for interval is provided', (done) => { - mockServiceCall( - service, - () => Promise.resolve({ status: 200, headers: {} }), - ); + fit('calls the success callback when no header for interval is provided', (done) => { + mockServiceCall(service, { status: 200 }); new Poll({ resource: service, @@ -55,10 +57,7 @@ describe('Poll', () => { }); it('calls the error callback whe the http request returns an error', (done) => { - mockServiceCall( - service, - () => Promise.reject({ status: 500, headers: {} }), - ); + mockServiceCall(service, { status: 500 }, true); new Poll({ resource: service, @@ -76,31 +75,23 @@ describe('Poll', () => { }); it('should call the success callback when the interval header is -1', (done) => { - mockServiceCall( - service, - () => Promise.resolve({ status: 200, headers: { 'poll-interval': -1 } }), - ); + mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } }); new Poll({ resource: service, method: 'fetch', successCallback: callbacks.success, errorCallback: callbacks.error, - }).makeRequest(); - - setTimeout(() => { + }).makeRequest().then(() => { expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled(); done(); - }, 0); + }).catch(done.fail); }); it('starts polling when http status is 200 and interval header is provided', (done) => { - mockServiceCall( - service, - () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }), - ); + mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } }); const Polling = new Poll({ resource: service, @@ -126,10 +117,7 @@ describe('Poll', () => { describe('stop', () => { it('stops polling when method is called', (done) => { - mockServiceCall( - service, - () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }), - ); + mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } }); const Polling = new Poll({ resource: service, @@ -157,10 +145,7 @@ describe('Poll', () => { describe('restart', () => { it('should restart polling when its called', (done) => { - mockServiceCall( - service, - () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }), - ); + mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } }); const Polling = new Poll({ resource: service, |