BigW Consortium Gitlab

Removed vue and vue-resource from poll_spec in an attempt to fix the transient…

Removed vue and vue-resource from poll_spec in an attempt to fix the transient failures relating to async timeout
parent 180ec711
import Vue from 'vue';
import VueResource from 'vue-resource';
import Poll from '~/lib/utils/poll'; import Poll from '~/lib/utils/poll';
Vue.use(VueResource);
const waitForAllCallsToFinish = (service, waitForCount, successCallback) => { const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
const timer = () => { const timer = () => {
setTimeout(() => { setTimeout(() => {
...@@ -12,25 +8,20 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => { ...@@ -12,25 +8,20 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
} else { } else {
timer(); timer();
} }
}, 5); }, 0);
}; };
timer(); timer();
}; };
class ServiceMock { function mockServiceCall(service, mockCall) {
constructor(endpoint) { service.fetch.calls.reset();
this.service = Vue.resource(endpoint); service.fetch.and.callFake(mockCall);
}
fetch() {
return this.service.get();
}
} }
describe('Poll', () => { describe('Poll', () => {
const service = jasmine.createSpyObj('service', ['fetch']);
let callbacks; let callbacks;
let service;
beforeEach(() => { beforeEach(() => {
callbacks = { callbacks = {
...@@ -38,19 +29,15 @@ describe('Poll', () => { ...@@ -38,19 +29,15 @@ describe('Poll', () => {
error: () => {}, error: () => {},
}; };
service = new ServiceMock('endpoint');
spyOn(callbacks, 'success'); spyOn(callbacks, 'success');
spyOn(callbacks, 'error'); spyOn(callbacks, 'error');
spyOn(service, 'fetch').and.callThrough();
}); });
it('calls the success callback when no header for interval is provided', (done) => { it('calls the success callback when no header for interval is provided', (done) => {
const successInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 200 })); service,
}; () => Promise.resolve({ status: 200, headers: {} }),
);
Vue.http.interceptors.push(successInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -63,18 +50,15 @@ describe('Poll', () => { ...@@ -63,18 +50,15 @@ describe('Poll', () => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor);
done(); done();
}, 0); }, 0);
}); });
it('calls the error callback whe the http request returns an error', (done) => { it('calls the error callback whe the http request returns an error', (done) => {
const errorInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 500 })); service,
}; () => Promise.reject({ status: 500, headers: {} }),
);
Vue.http.interceptors.push(errorInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -86,18 +70,16 @@ describe('Poll', () => { ...@@ -86,18 +70,16 @@ describe('Poll', () => {
waitForAllCallsToFinish(service, 1, () => { waitForAllCallsToFinish(service, 1, () => {
expect(callbacks.success).not.toHaveBeenCalled(); expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled(); expect(callbacks.error).toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor);
done(); done();
}); });
}); });
it('should call the success callback when the interval header is -1', (done) => { it('should call the success callback when the interval header is -1', (done) => {
const intervalInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': -1 } })); service,
}; () => Promise.resolve({ status: 200, headers: { 'poll-interval': -1 } }),
);
Vue.http.interceptors.push(intervalInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -110,18 +92,15 @@ describe('Poll', () => { ...@@ -110,18 +92,15 @@ describe('Poll', () => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor);
done(); done();
}, 0); }, 0);
}); });
it('starts polling when http status is 200 and interval header is provided', (done) => { it('starts polling when http status is 200 and interval header is provided', (done) => {
const pollInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } })); service,
}; () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -141,19 +120,16 @@ describe('Poll', () => { ...@@ -141,19 +120,16 @@ describe('Poll', () => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
done(); done();
}); });
}); });
describe('stop', () => { describe('stop', () => {
it('stops polling when method is called', (done) => { it('stops polling when method is called', (done) => {
const pollInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } })); service,
}; () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -174,8 +150,6 @@ describe('Poll', () => { ...@@ -174,8 +150,6 @@ describe('Poll', () => {
expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled(); expect(Polling.stop).toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
done(); done();
}); });
}); });
...@@ -183,11 +157,10 @@ describe('Poll', () => { ...@@ -183,11 +157,10 @@ describe('Poll', () => {
describe('restart', () => { describe('restart', () => {
it('should restart polling when its called', (done) => { it('should restart polling when its called', (done) => {
const pollInterceptor = (request, next) => { mockServiceCall(
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } })); service,
}; () => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -215,8 +188,6 @@ describe('Poll', () => { ...@@ -215,8 +188,6 @@ describe('Poll', () => {
expect(Polling.stop).toHaveBeenCalled(); expect(Polling.stop).toHaveBeenCalled();
expect(Polling.restart).toHaveBeenCalled(); expect(Polling.restart).toHaveBeenCalled();
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
done(); done();
}); });
}); });
......
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