Commit f99e8c07 authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'remove-vue-from-poll_spec-fix-transient-failures' into 'master'

Fix poll_spec transient failures

See merge request !11264
parents f938f944 55b6232c
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,45 +8,33 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => { ...@@ -12,45 +8,33 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
} else { } else {
timer(); timer();
} }
}, 5); }, 0);
}; };
timer(); timer();
}; };
class ServiceMock { function mockServiceCall(service, response, shouldFail = false) {
constructor(endpoint) { const action = shouldFail ? Promise.reject : Promise.resolve;
this.service = Vue.resource(endpoint); const responseObject = response;
}
if (!responseObject.headers) responseObject.headers = {};
fetch() { service.fetch.and.callFake(action.bind(Promise, responseObject));
return this.service.get();
}
} }
describe('Poll', () => { describe('Poll', () => {
let callbacks; const service = jasmine.createSpyObj('service', ['fetch']);
let service; const callbacks = jasmine.createSpyObj('callbacks', ['success', 'error']);
beforeEach(() => { afterEach(() => {
callbacks = { callbacks.success.calls.reset();
success: () => {}, callbacks.error.calls.reset();
error: () => {}, service.fetch.calls.reset();
};
service = new ServiceMock('endpoint');
spyOn(callbacks, 'success');
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(service, { status: 200 });
next(request.respondWith(JSON.stringify([]), { status: 200 }));
};
Vue.http.interceptors.push(successInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -63,18 +47,12 @@ describe('Poll', () => { ...@@ -63,18 +47,12 @@ 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); });
}); });
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(service, { status: 500 }, true);
next(request.respondWith(JSON.stringify([]), { status: 500 }));
};
Vue.http.interceptors.push(errorInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -86,42 +64,29 @@ describe('Poll', () => { ...@@ -86,42 +64,29 @@ 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(service, { status: 200, headers: { 'poll-interval': -1 } });
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': -1 } }));
};
Vue.http.interceptors.push(intervalInterceptor);
new Poll({ new Poll({
resource: service, resource: service,
method: 'fetch', method: 'fetch',
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
}).makeRequest(); }).makeRequest().then(() => {
setTimeout(() => {
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); }).catch(done.fail);
}); });
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(service, { status: 200, headers: { 'poll-interval': 1 } });
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
};
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -141,19 +106,13 @@ describe('Poll', () => { ...@@ -141,19 +106,13 @@ 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(service, { status: 200, headers: { 'poll-interval': 1 } });
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
};
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -174,8 +133,6 @@ describe('Poll', () => { ...@@ -174,8 +133,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 +140,7 @@ describe('Poll', () => { ...@@ -183,11 +140,7 @@ 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(service, { status: 200, headers: { 'poll-interval': 1 } });
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
};
Vue.http.interceptors.push(pollInterceptor);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -215,8 +168,6 @@ describe('Poll', () => { ...@@ -215,8 +168,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