2017-03-21 14:07:02 -04:00
|
|
|
import Poll from '~/lib/utils/poll';
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2017-05-11 08:09:13 -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
|
|
|
});
|
|
|
|
|
2017-07-17 21:35:43 -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
|
|
|
});
|
|
|
|
|
2017-07-17 21:35:43 -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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-21 14:07:02 -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 } });
|
2017-07-17 21:35:43 -04:00
|
|
|
setup().then(() => {
|
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 07:48:24 -04:00
|
|
|
}).catch(done.fail);
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('starts polling when http status is 200 and interval header is provided', (done) => {
|
2017-05-11 07:48:24 -04:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
const Polling = new Poll({
|
2017-03-21 14:07:02 -04:00
|
|
|
resource: service,
|
|
|
|
method: 'fetch',
|
2017-03-22 08:37:38 -04:00
|
|
|
data: { page: 1 },
|
2017-03-21 14:07:02 -04:00
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
2017-04-12 08:42:01 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Polling.makeRequest();
|
|
|
|
|
|
|
|
waitForAllCallsToFinish(service, 2, () => {
|
|
|
|
Polling.stop();
|
2017-03-21 14:07:02 -04:00
|
|
|
|
|
|
|
expect(service.fetch.calls.count()).toEqual(2);
|
2017-03-22 08:37:38 -04:00
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 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
|
|
|
done();
|
|
|
|
});
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
2017-03-23 11:57:03 -04:00
|
|
|
|
|
|
|
describe('stop', () => {
|
|
|
|
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', () => {
|
|
|
|
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(() => {
|
|
|
|
Polling.restart();
|
|
|
|
}, 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);
|
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
|
|
|
expect(Polling.stop).toHaveBeenCalled();
|
2017-04-12 08:42:01 -04:00
|
|
|
expect(Polling.restart).toHaveBeenCalled();
|
2017-03-24 12:59:14 -04:00
|
|
|
|
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
|
|
|
});
|