gitlab-org--gitlab-foss/spec/javascripts/lib/utils/poll_spec.js

223 lines
6.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable jasmine/no-unsafe-spy */
2017-03-21 14:07:02 -04:00
import Poll from '~/lib/utils/poll';
import { successCodes } from '~/lib/utils/http_status';
2017-03-21 14:07:02 -04:00
const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
const timer = () => {
setTimeout(() => {
if (service.fetch.calls.count() === waitForCount) {
successCallback();
} else {
timer();
}
}, 0);
};
timer();
};
function mockServiceCall(service, response, shouldFail = false) {
const action = shouldFail ? Promise.reject : Promise.resolve;
const responseObject = response;
if (!responseObject.headers) responseObject.headers = {};
service.fetch.and.callFake(action.bind(Promise, responseObject));
2017-03-21 14:07:02 -04:00
}
describe('Poll', () => {
const service = jasmine.createSpyObj('service', ['fetch']);
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();
}
2017-03-21 14:07:02 -04:00
afterEach(() => {
callbacks.success.calls.reset();
callbacks.error.calls.reset();
callbacks.notification.calls.reset();
service.fetch.calls.reset();
2017-03-21 14:07:02 -04:00
});
2018-10-17 03:13:26 -04:00
it('calls the success callback when no header for interval is provided', done => {
mockServiceCall(service, { status: 200 });
setup();
2017-03-21 14:07:02 -04:00
waitForAllCallsToFinish(service, 1, () => {
2017-03-21 14:07:02 -04:00
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
2017-03-21 14:07:02 -04:00
done();
});
2017-03-21 14:07:02 -04:00
});
2018-10-17 03:13:26 -04:00
it('calls the error callback when the http request returns an error', done => {
mockServiceCall(service, { status: 500 }, true);
setup();
2017-03-21 14:07:02 -04:00
waitForAllCallsToFinish(service, 1, () => {
2017-03-21 14:07:02 -04:00
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled();
done();
});
2017-03-21 14:07:02 -04:00
});
2018-10-17 03:13:26 -04:00
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();
});
});
2018-10-17 03:13:26 -04:00
it('should call the success callback when the interval header is -1', done => {
mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } });
2018-10-17 03:13:26 -04:00
setup()
.then(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
2018-10-17 03:13:26 -04:00
done();
})
.catch(done.fail);
2017-03-21 14:07:02 -04:00
});
describe('for 2xx status code', () => {
successCodes.forEach(httpCode => {
2018-10-17 03:13:26 -04:00
it(`starts polling when http status is ${httpCode} and interval header is provided`, done => {
mockServiceCall(service, { status: httpCode, headers: { 'poll-interval': 1 } });
2017-03-21 14:07:02 -04:00
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: callbacks.success,
errorCallback: callbacks.error,
});
Polling.makeRequest();
waitForAllCallsToFinish(service, 2, () => {
Polling.stop();
2017-03-21 14:07:02 -04:00
expect(service.fetch.calls.count()).toEqual(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
2017-03-21 14:07:02 -04:00
done();
});
});
});
2017-03-21 14:07:02 -04:00
});
describe('stop', () => {
2018-10-17 03:13:26 -04:00
it('stops polling when method is called', done => {
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {
Polling.stop();
},
errorCallback: callbacks.error,
});
spyOn(Polling, 'stop').and.callThrough();
Polling.makeRequest();
waitForAllCallsToFinish(service, 1, () => {
expect(service.fetch.calls.count()).toEqual(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled();
done();
});
});
});
describe('enable', () => {
it('should enable polling upon a response', done => {
jasmine.clock().install();
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {},
});
Polling.enable({
data: { page: 4 },
response: { status: 200, headers: { 'poll-interval': 1 } },
});
jasmine.clock().tick(1);
jasmine.clock().uninstall();
waitForAllCallsToFinish(service, 1, () => {
Polling.stop();
expect(service.fetch.calls.count()).toEqual(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
describe('restart', () => {
2018-10-17 03:13:26 -04:00
it('should restart polling when its called', done => {
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {
Polling.stop();
setTimeout(() => {
Polling.restart({ data: { page: 4 } });
}, 0);
},
errorCallback: callbacks.error,
});
spyOn(Polling, 'stop').and.callThrough();
spyOn(Polling, 'enable').and.callThrough();
spyOn(Polling, 'restart').and.callThrough();
Polling.makeRequest();
waitForAllCallsToFinish(service, 2, () => {
Polling.stop();
expect(service.fetch.calls.count()).toEqual(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.stop).toHaveBeenCalled();
expect(Polling.enable).toHaveBeenCalled();
expect(Polling.restart).toHaveBeenCalled();
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
2017-03-21 14:07:02 -04:00
});