2017-03-21 14:07:02 -04:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueResource from 'vue-resource';
|
|
|
|
import Poll from '~/lib/utils/poll';
|
|
|
|
|
|
|
|
Vue.use(VueResource);
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}, 5);
|
|
|
|
};
|
|
|
|
|
|
|
|
timer();
|
|
|
|
};
|
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
class ServiceMock {
|
|
|
|
constructor(endpoint) {
|
|
|
|
this.service = Vue.resource(endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch() {
|
|
|
|
return this.service.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Poll', () => {
|
|
|
|
let callbacks;
|
2017-04-12 08:42:01 -04:00
|
|
|
let service;
|
2017-03-21 14:07:02 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
callbacks = {
|
|
|
|
success: () => {},
|
|
|
|
error: () => {},
|
|
|
|
};
|
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
service = new ServiceMock('endpoint');
|
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
spyOn(callbacks, 'success');
|
|
|
|
spyOn(callbacks, 'error');
|
2017-04-12 08:42:01 -04:00
|
|
|
spyOn(service, 'fetch').and.callThrough();
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the success callback when no header for interval is provided', (done) => {
|
|
|
|
const successInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 200 }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(successInterceptor);
|
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 08:42:01 -04:00
|
|
|
resource: service,
|
2017-03-21 14:07:02 -04:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
}).makeRequest();
|
|
|
|
|
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
|
|
|
|
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor);
|
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls the error callback whe the http request returns an error', (done) => {
|
|
|
|
const errorInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 500 }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(errorInterceptor);
|
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 08:42:01 -04:00
|
|
|
resource: service,
|
2017-03-21 14:07:02 -04:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
}).makeRequest();
|
|
|
|
|
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
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor);
|
2017-03-21 14:07:02 -04:00
|
|
|
|
2017-04-12 08:42:01 -04:00
|
|
|
done();
|
|
|
|
});
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call the success callback when the interval header is -1', (done) => {
|
|
|
|
const intervalInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': -1 } }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(intervalInterceptor);
|
|
|
|
|
|
|
|
new Poll({
|
2017-04-12 08:42:01 -04:00
|
|
|
resource: service,
|
2017-03-21 14:07:02 -04:00
|
|
|
method: 'fetch',
|
|
|
|
successCallback: callbacks.success,
|
|
|
|
errorCallback: callbacks.error,
|
|
|
|
}).makeRequest();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(callbacks.success).toHaveBeenCalled();
|
|
|
|
expect(callbacks.error).not.toHaveBeenCalled();
|
2017-04-12 08:42:01 -04:00
|
|
|
|
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor);
|
|
|
|
|
2017-03-21 14:07:02 -04:00
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('starts polling when http status is 200 and interval header is provided', (done) => {
|
|
|
|
const pollInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(pollInterceptor);
|
|
|
|
|
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
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
const pollInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(pollInterceptor);
|
|
|
|
|
|
|
|
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
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
const pollInterceptor = (request, next) => {
|
|
|
|
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.http.interceptors.push(pollInterceptor);
|
|
|
|
|
|
|
|
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
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-03-24 12:59:14 -04:00
|
|
|
});
|
|
|
|
});
|
2017-03-21 14:07:02 -04:00
|
|
|
});
|