2017-03-21 18:07:02 +00:00
|
|
|
import Poll from '~/lib/utils/poll';
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
|
|
|
|
const timer = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (service.fetch.calls.count() === waitForCount) {
|
|
|
|
successCallback();
|
|
|
|
} else {
|
|
|
|
timer();
|
|
|
|
}
|
2017-05-10 19:41:06 +00:00
|
|
|
}, 0);
|
2017-04-12 12:42:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
timer();
|
|
|
|
};
|
|
|
|
|
2017-05-11 12:09:13 +00:00
|
|
|
function mockServiceCall(service, response, shouldFail = false) {
|
2017-05-11 11:48:24 +00:00
|
|
|
const action = shouldFail ? Promise.reject : Promise.resolve;
|
|
|
|
const responseObject = response;
|
|
|
|
|
|
|
|
if (!responseObject.headers) responseObject.headers = {};
|
|
|
|
|
2017-05-11 12:09:13 +00:00
|
|
|
service.fetch.and.callFake(action.bind(Promise, responseObject));
|
2017-03-21 18:07:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 12:09:13 +00:00
|
|
|
describe('Poll', () => {
|
2017-05-10 19:41:06 +00:00
|
|
|
const service = jasmine.createSpyObj('service', ['fetch']);
|
2017-05-11 12:09:13 +00:00
|
|
|
const callbacks = jasmine.createSpyObj('callbacks', ['success', 'error']);
|
2017-03-21 18:07:02 +00:00
|
|
|
|
2017-05-11 12:09:13 +00:00
|
|
|
afterEach(() => {
|
|
|
|
callbacks.success.calls.reset();
|
|
|
|
callbacks.error.calls.reset();
|
|
|
|
service.fetch.calls.reset();
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|
|
|
|
|
2017-05-11 12:09:13 +00:00
|
|
|
it('calls the success callback when no header for interval is provided', (done) => {
|
2017-05-11 19:11:03 +00:00
|
|
|
mockServiceCall(service, { status: 200 });
|
2017-03-21 18:07:02 +00:00
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 12:42:01 +00:00
|
|
|
resource: service,
|
2017-03-21 18:07:02 +00:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
}).makeRequest();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-21 18:07:02 +00:00
|
|
|
expect(callbacks.success).toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).not.toHaveBeenCalled();
|
2017-04-12 12:42:01 +00:00
|
|
|
|
2017-03-21 18:07:02 +00:00
|
|
|
done();
|
2017-05-11 12:09:13 +00:00
|
|
|
});
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the error callback whe the http request returns an error', (done) => {
|
2017-05-11 11:48:24 +00:00
|
|
|
mockServiceCall(service, { status: 500 }, true);
|
2017-03-21 18:07:02 +00:00
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 12:42:01 +00:00
|
|
|
resource: service,
|
2017-03-21 18:07:02 +00:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
}).makeRequest();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-21 18:07:02 +00:00
|
|
|
expect(callbacks.success).not.toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).toHaveBeenCalled();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
done();
|
|
|
|
});
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the success callback when the interval header is -1', (done) => {
|
2017-05-11 11:48:24 +00:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } });
|
2017-03-21 18:07:02 +00:00
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 12:42:01 +00:00
|
|
|
resource: service,
|
2017-03-21 18:07:02 +00:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
2017-05-11 11:48:24 +00:00
|
|
|
}).makeRequest().then(() => {
|
2017-03-21 18:07:02 +00:00
|
|
|
expect(callbacks.success).toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).not.toHaveBeenCalled();
|
2017-04-12 12:42:01 +00:00
|
|
|
|
2017-03-21 18:07:02 +00:00
|
|
|
done();
|
2017-05-11 11:48:24 +00:00
|
|
|
}).catch(done.fail);
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('starts polling when http status is 200 and interval header is provided', (done) => {
|
2017-05-11 11:48:24 +00:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-21 18:07:02 +00:00
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
const Polling = new Poll({
|
2017-03-21 18:07:02 +00:00
|
|
|
resource: service,
|
|
|
|
method: 'fetch',
|
2017-03-22 12:37:38 +00:00
|
|
|
data: { page: 1 },
|
2017-03-21 18:07:02 +00:00
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
2017-04-12 12:42:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Polling.makeRequest();
|
|
|
|
|
|
|
|
waitForAllCallsToFinish(service, 2, () => {
|
|
|
|
Polling.stop();
|
2017-03-21 18:07:02 +00:00
|
|
|
|
|
|
|
expect(service.fetch.calls.count()).toEqual(2);
|
2017-03-22 12:37:38 +00:00
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
2017-03-21 18:07:02 +00:00
|
|
|
expect(callbacks.success).toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).not.toHaveBeenCalled();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
done();
|
|
|
|
});
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|
2017-03-23 15:57:03 +00:00
|
|
|
|
|
|
|
describe('stop', () => {
|
|
|
|
it('stops polling when method is called', (done) => {
|
2017-05-11 11:48:24 +00:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-23 15:57:03 +00: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 12:42:01 +00:00
|
|
|
waitForAllCallsToFinish(service, 1, () => {
|
2017-03-23 15:57:03 +00:00
|
|
|
expect(service.fetch.calls.count()).toEqual(1);
|
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
|
|
|
expect(Polling.stop).toHaveBeenCalled();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
done();
|
|
|
|
});
|
2017-03-23 15:57:03 +00:00
|
|
|
});
|
|
|
|
});
|
2017-03-24 16:59:14 +00:00
|
|
|
|
|
|
|
describe('restart', () => {
|
|
|
|
it('should restart polling when its called', (done) => {
|
2017-05-11 11:48:24 +00:00
|
|
|
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
|
2017-03-24 16:59:14 +00: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 12:42:01 +00:00
|
|
|
spyOn(Polling, 'restart').and.callThrough();
|
2017-03-24 16:59:14 +00:00
|
|
|
|
|
|
|
Polling.makeRequest();
|
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
waitForAllCallsToFinish(service, 2, () => {
|
|
|
|
Polling.stop();
|
|
|
|
|
2017-03-24 16:59:14 +00:00
|
|
|
expect(service.fetch.calls.count()).toEqual(2);
|
|
|
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
|
|
|
expect(Polling.stop).toHaveBeenCalled();
|
2017-04-12 12:42:01 +00:00
|
|
|
expect(Polling.restart).toHaveBeenCalled();
|
2017-03-24 16:59:14 +00:00
|
|
|
|
2017-04-12 12:42:01 +00:00
|
|
|
done();
|
|
|
|
});
|
2017-03-24 16:59:14 +00:00
|
|
|
});
|
|
|
|
});
|
2017-03-21 18:07:02 +00:00
|
|
|
});
|