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 @@
*/
export default {
ABORTED: 0,
NO_CONTENT: 204,
OK: 200,
};
......@@ -81,6 +81,9 @@ export default class Poll {
})
.catch((error) => {
notificationCallback(false);
if (error.status === httpStatusCodes.ABORTED) {
return;
}
errorCallback(error);
});
}
......
---
title: fix transient js error in rspec tests
merge_request:
author:
......@@ -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();
......
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