diff options
author | Simon Knox <psimyn@gmail.com> | 2017-07-18 11:35:43 +1000 |
---|---|---|
committer | Simon Knox <psimyn@gmail.com> | 2017-07-18 12:17:55 +1000 |
commit | 53e7bf047084876cf441c010f683119fc54dfb6a (patch) | |
tree | 70003b202939b301e0647bbc24038125e790272f | |
parent | 00861676a0cac3154153ec1afd7582991628e16e (diff) | |
download | gitlab-ce-53e7bf047084876cf441c010f683119fc54dfb6a.tar.gz |
fix transient rspec failure due to Poll.js race condition35225-transient-poll
don't treat aborted requests as failures, they are often triggered during unload (e.g after clicking a link)
-rw-r--r-- | app/assets/javascripts/lib/utils/http_status.js | 1 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/poll.js | 3 | ||||
-rw-r--r-- | changelogs/unreleased/35225-transient-poll.yml | 4 | ||||
-rw-r--r-- | spec/javascripts/lib/utils/poll_spec.js | 52 |
4 files changed, 37 insertions, 23 deletions
diff --git a/app/assets/javascripts/lib/utils/http_status.js b/app/assets/javascripts/lib/utils/http_status.js index 415e50f32ae..625e53ee9de 100644 --- a/app/assets/javascripts/lib/utils/http_status.js +++ b/app/assets/javascripts/lib/utils/http_status.js @@ -3,6 +3,7 @@ */ export default { + ABORTED: 0, NO_CONTENT: 204, OK: 200, }; diff --git a/app/assets/javascripts/lib/utils/poll.js b/app/assets/javascripts/lib/utils/poll.js index e31cc5fbabe..97666e13ebe 100644 --- a/app/assets/javascripts/lib/utils/poll.js +++ b/app/assets/javascripts/lib/utils/poll.js @@ -81,6 +81,9 @@ export default class Poll { }) .catch((error) => { notificationCallback(false); + if (error.status === httpStatusCodes.ABORTED) { + return; + } errorCallback(error); }); } diff --git a/changelogs/unreleased/35225-transient-poll.yml b/changelogs/unreleased/35225-transient-poll.yml new file mode 100644 index 00000000000..59e2e738c7b --- /dev/null +++ b/changelogs/unreleased/35225-transient-poll.yml @@ -0,0 +1,4 @@ +--- +title: fix transient js error in rspec tests +merge_request: +author: diff --git a/spec/javascripts/lib/utils/poll_spec.js b/spec/javascripts/lib/utils/poll_spec.js index 22f30191ab9..2aa7011ca51 100644 --- a/spec/javascripts/lib/utils/poll_spec.js +++ b/spec/javascripts/lib/utils/poll_spec.js @@ -25,23 +25,28 @@ function mockServiceCall(service, response, shouldFail = false) { describe('Poll', () => { const service = jasmine.createSpyObj('service', ['fetch']); - const callbacks = jasmine.createSpyObj('callbacks', ['success', 'error']); + const callbacks = jasmine.createSpyObj('callbacks', ['success', 'error', 'notification']); + + function setup() { + return new Poll({ + resource: service, + method: 'fetch', + successCallback: callbacks.success, + errorCallback: callbacks.error, + notificationCallback: callbacks.notification, + }).makeRequest(); + } afterEach(() => { callbacks.success.calls.reset(); callbacks.error.calls.reset(); + callbacks.notification.calls.reset(); service.fetch.calls.reset(); }); it('calls the success callback when no header for interval is provided', (done) => { mockServiceCall(service, { status: 200 }); - - new Poll({ - resource: service, - method: 'fetch', - successCallback: callbacks.success, - errorCallback: callbacks.error, - }).makeRequest(); + setup(); waitForAllCallsToFinish(service, 1, () => { expect(callbacks.success).toHaveBeenCalled(); @@ -51,15 +56,9 @@ describe('Poll', () => { }); }); - it('calls the error callback whe the http request returns an error', (done) => { + it('calls the error callback when the http request returns an error', (done) => { mockServiceCall(service, { status: 500 }, true); - - new Poll({ - resource: service, - method: 'fetch', - successCallback: callbacks.success, - errorCallback: callbacks.error, - }).makeRequest(); + setup(); waitForAllCallsToFinish(service, 1, () => { expect(callbacks.success).not.toHaveBeenCalled(); @@ -69,15 +68,22 @@ describe('Poll', () => { }); }); + it('skips the error callback when request is aborted', (done) => { + mockServiceCall(service, { status: 0 }, true); + setup(); + + waitForAllCallsToFinish(service, 1, () => { + expect(callbacks.success).not.toHaveBeenCalled(); + expect(callbacks.error).not.toHaveBeenCalled(); + expect(callbacks.notification).toHaveBeenCalled(); + + done(); + }); + }); + it('should call the success callback when the interval header is -1', (done) => { mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } }); - - new Poll({ - resource: service, - method: 'fetch', - successCallback: callbacks.success, - errorCallback: callbacks.error, - }).makeRequest().then(() => { + setup().then(() => { expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled(); |