Commit 53e7bf04 authored by Simon Knox's avatar Simon Knox

fix transient rspec failure due to Poll.js race condition

don't treat aborted requests as failures, they are often triggered during unload (e.g after clicking a link)
parent 00861676
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
*/ */
export default { export default {
ABORTED: 0,
NO_CONTENT: 204, NO_CONTENT: 204,
OK: 200, OK: 200,
}; };
...@@ -81,6 +81,9 @@ export default class Poll { ...@@ -81,6 +81,9 @@ export default class Poll {
}) })
.catch((error) => { .catch((error) => {
notificationCallback(false); notificationCallback(false);
if (error.status === httpStatusCodes.ABORTED) {
return;
}
errorCallback(error); errorCallback(error);
}); });
} }
......
---
title: fix transient js error in rspec tests
merge_request:
author:
...@@ -25,23 +25,28 @@ function mockServiceCall(service, response, shouldFail = false) { ...@@ -25,23 +25,28 @@ function mockServiceCall(service, response, shouldFail = false) {
describe('Poll', () => { describe('Poll', () => {
const service = jasmine.createSpyObj('service', ['fetch']); 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(() => { afterEach(() => {
callbacks.success.calls.reset(); callbacks.success.calls.reset();
callbacks.error.calls.reset(); callbacks.error.calls.reset();
callbacks.notification.calls.reset();
service.fetch.calls.reset(); service.fetch.calls.reset();
}); });
it('calls the success callback when no header for interval is provided', (done) => { it('calls the success callback when no header for interval is provided', (done) => {
mockServiceCall(service, { status: 200 }); mockServiceCall(service, { status: 200 });
setup();
new Poll({
resource: service,
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
waitForAllCallsToFinish(service, 1, () => { waitForAllCallsToFinish(service, 1, () => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
...@@ -51,15 +56,9 @@ describe('Poll', () => { ...@@ -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); mockServiceCall(service, { status: 500 }, true);
setup();
new Poll({
resource: service,
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
waitForAllCallsToFinish(service, 1, () => { waitForAllCallsToFinish(service, 1, () => {
expect(callbacks.success).not.toHaveBeenCalled(); expect(callbacks.success).not.toHaveBeenCalled();
...@@ -69,15 +68,22 @@ describe('Poll', () => { ...@@ -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) => { it('should call the success callback when the interval header is -1', (done) => {
mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } }); mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } });
setup().then(() => {
new Poll({
resource: service,
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest().then(() => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment