2018-09-20 02:32:31 -04:00
|
|
|
/* eslint-disable jasmine/no-unsafe-spy */
|
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
import Poll from '~/lib/utils/poll';
|
2018-07-30 10:25:34 -04:00
|
|
|
import { successCodes } from '~/lib/utils/http_status';
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
|
|
|
|
const timer = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (service.fetch.calls.count() === waitForCount) {
|
|
|
|
successCallback();
|
|
|
|
} else {
|
|
|
|
timer();
|
|
|
|
}
|
2017-05-10 15:41:06 -04:00
|
|
|
}, 0);
|
2017-04-12 08:42:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
timer();
|
|
|
|
};
|
|
|
|
|
2017-05-11 08:09:13 -04:00
|
|
|
function mockServiceCall(service, response, shouldFail = false) {
|
2017-05-11 07:48:24 -04:00
|
|
|
const action = shouldFail ? Promise.reject : Promise.resolve;
|
|
|
|
const responseObject = response;
|
|
|
|
|
|
|
|
if (!responseObject.headers) responseObject.headers = {};
|
|
|
|
|
2017-05-11 08:09:13 -04:00
|
|
|
service.fetch.and.callFake(action.bind(Promise, responseObject));
|
2017-03-21 14:07:02 -04:00
|
|
|
}
|
|
|
|
|
2017-05-11 08:09:13 -04:00
|
|
|
describe('Poll', () => {
|
2017-05-10 15:41:06 -04:00
|
|
|
const service = jasmine.createSpyObj('service', ['fetch']);
|
2017-07-17 21:35:43 -04:00
|
|
|
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
|
|
|
|
2017-05-11 08:09:13 -04:00
|
|
|
afterEach(() => {
|
|
|
|
callbacks.success.calls.reset();
|
|
|
|
callbacks.error.calls.reset();
|
2017-07-17 21:35:43 -04:00
|
|
|
callbacks.notification.calls.reset();
|
2017-05-11 08:09:13 -04:00
|
|
|
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 => {
|
2017-05-11 15:11:03 -04:00
|
|
|
mockServiceCall(service, { status: 200 });
|
2017-07-17 21:35:43 -04:00
|
|
|
setup();
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-21 14:07:02 -04:00
|
|
|
expect(callbacks.success).toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).not.toHaveBeenCalled();
|
2017-04-12 08:42:01 -04:00
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
done();
|
2017-05-11 08:09:13 -04:00
|
|
|
});
|
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 => {
|
2017-05-11 07:48:24 -04:00
|
|
|
mockServiceCall(service, { status: 500 }, true);
|
2017-07-17 21:35:43 -04:00
|
|
|
setup();
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-21 14:07:02 -04:00
|
|
|
expect(callbacks.success).not.toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).toHaveBeenCalled();
|
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
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 => {
|
2017-07-17 21:35:43 -04:00
|
|
|
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 => {
|
2017-05-11 07:48:24 -04:00
|
|
|
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();
|
2017-04-12 08:42:01 -04:00
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
|
|
|
|
2018-07-30 10:25:34 -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 => {
|
2018-07-30 10:25:34 -04:00
|
|
|
mockServiceCall(service, { status: httpCode, headers: { 'poll-interval': 1 } });
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2018-07-30 10:25:34 -04:00
|
|
|
const Polling = new Poll({
|
|
|
|
resource: service,
|
|
|
|
method: 'fetch',
|
|
|
|
data: { page: 1 },
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
});
|
2017-04-12 08:42:01 -04:00
|
|
|
|
2018-07-30 10:25:34 -04:00
|
|
|
Polling.makeRequest();
|
2017-04-12 08:42:01 -04:00
|
|
|
|
2018-07-30 10:25:34 -04:00
|
|
|
waitForAllCallsToFinish(service, 2, () => {
|
|
|
|
Polling.stop();
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2018-07-30 10:25:34 -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
|
|
|
|
2018-07-30 10:25:34 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-04-12 08:42:01 -04:00
|
|
|
});
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
2017-03-23 11:57:03 -04:00
|
|
|
|
|
|
|
describe('stop', () => {
|
2018-10-17 03:13:26 -04:00
|
|
|
it('stops polling when method is called', done => {
|
2017-05-11 07:48:24 -04:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-23 11:57:03 -04:00
|
|
|
|
|
|
|
const Polling = new Poll({
|
|
|
|
resource: service,
|
|
|
|
method: 'fetch',
|
|
|
|
data: { page: 1 },
|
|
|
|
successCallback: () => {
|
|
|
|
Polling.stop();
|
|
|
|
},
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(Polling, 'stop').and.callThrough();
|
|
|
|
|
|
|
|
Polling.makeRequest();
|
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-23 11:57:03 -04:00
|
|
|
expect(service.fetch.calls.count()).toEqual(1);
|
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
|
|
|
expect(Polling.stop).toHaveBeenCalled();
|
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
done();
|
|
|
|
});
|
2017-03-23 11:57:03 -04:00
|
|
|
});
|
|
|
|
});
|
2017-03-24 12:59:14 -04:00
|
|
|
|
|
|
|
describe('restart', () => {
|
2018-10-17 03:13:26 -04:00
|
|
|
it('should restart polling when its called', done => {
|
2017-05-11 07:48:24 -04:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-24 12:59:14 -04:00
|
|
|
|
|
|
|
const Polling = new Poll({
|
|
|
|
resource: service,
|
|
|
|
method: 'fetch',
|
|
|
|
data: { page: 1 },
|
|
|
|
successCallback: () => {
|
|
|
|
Polling.stop();
|
|
|
|
setTimeout(() => {
|
2017-11-14 10:35:29 -05:00
|
|
|
Polling.restart({ data: { page: 4 } });
|
2017-03-24 12:59:14 -04:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
});
|
|
|
|
|
|
|
|
spyOn(Polling, 'stop').and.callThrough();
|
2017-04-12 08:42:01 -04:00
|
|
|
spyOn(Polling, 'restart').and.callThrough();
|
2017-03-24 12:59:14 -04:00
|
|
|
|
|
|
|
Polling.makeRequest();
|
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
waitForAllCallsToFinish(service, 2, () => {
|
|
|
|
Polling.stop();
|
|
|
|
|
2017-03-24 12:59:14 -04:00
|
|
|
expect(service.fetch.calls.count()).toEqual(2);
|
2017-11-14 10:35:29 -05:00
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
|
2017-03-24 12:59:14 -04:00
|
|
|
expect(Polling.stop).toHaveBeenCalled();
|
2017-04-12 08:42:01 -04:00
|
|
|
expect(Polling.restart).toHaveBeenCalled();
|
2017-11-14 10:35:29 -05:00
|
|
|
expect(Polling.options.data).toEqual({ page: 4 });
|
2017-04-12 08:42:01 -04:00
|
|
|
done();
|
|
|
|
});
|
2017-03-24 12:59:14 -04:00
|
|
|
});
|
|
|
|
});
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|