Commit a94ee468 authored by Filipa Lacerda's avatar Filipa Lacerda

Adds tests and changes after review

parent ca5982af
......@@ -8,6 +8,7 @@ import httpStatusCodes from './http_status';
* new poll({
* resource: resource,
* method: 'name',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
......@@ -16,6 +17,7 @@ import httpStatusCodes from './http_status';
* new poll({
* resource: this.service,
* method: 'get',
* data: {page: 1, scope: 'all'},
* successCallback: () => {},
* errorCallback: () => {},
* }).makeRequest();
......@@ -30,7 +32,9 @@ import httpStatusCodes from './http_status';
*/
export default class poll {
constructor(options = {}) {
this.options = options;
this.options = Object.assign({}, {
data: {},
}, options);
this.intervalHeader = 'POLL-INTERVAL';
}
......@@ -42,9 +46,7 @@ export default class poll {
if (pollInterval > 0 && response.status === httpStatusCodes.OK) {
this.options.successCallback(response);
setTimeout(() => {
this.makeRequest()
.then(this.checkConditions)
.catch(error => this.options.errorCallback(error));
this.makeRequest();
}, pollInterval);
} else {
this.options.successCallback(response);
......@@ -52,8 +54,10 @@ export default class poll {
}
makeRequest() {
return this.options.resource[this.options.method]()
.then(this.checkConditions.bind(this))
.catch(error => this.options.errorCallback(error));
const { resource, method, data, errorCallback } = this.options;
return resource[method](data)
.then(response => this.checkConditions(response))
.catch(error => errorCallback(error));
}
}
import Vue from 'vue';
import VueResource from 'vue-resource';
import Poll from '~/lib/utils/poll';
Vue.use(VueResource);
class ServiceMock {
constructor(endpoint) {
this.service = Vue.resource(endpoint);
}
fetch() {
return this.service.get();
}
}
describe('Poll', () => {
let callbacks;
beforeEach(() => {
callbacks = {
success: () => {},
error: () => {},
};
spyOn(callbacks, 'success');
spyOn(callbacks, 'error');
});
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({
resource: new ServiceMock('endpoint'),
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
setTimeout(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor);
});
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({
resource: new ServiceMock('endpoint'),
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
setTimeout(() => {
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor);
});
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({
resource: new ServiceMock('endpoint'),
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
setTimeout(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor);
});
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);
const service = new ServiceMock('endpoint');
spyOn(service, 'fetch').and.callThrough();
new Poll({
resource: service,
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
}).makeRequest();
setTimeout(() => {
expect(service.fetch.calls.count()).toEqual(2);
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 5);
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment