Commit 32893491 authored by Filipa Lacerda's avatar Filipa Lacerda Committed by Lin Jen-Shin

Merge branch 'poll-test-fix' into 'master'

Fixed random failures with Poll spec

Closes #30824

See merge request !10654
parent 30332e41
...@@ -4,6 +4,20 @@ import Poll from '~/lib/utils/poll'; ...@@ -4,6 +4,20 @@ import Poll from '~/lib/utils/poll';
Vue.use(VueResource); Vue.use(VueResource);
const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
const timer = () => {
setTimeout(() => {
if (service.fetch.calls.count() === waitForCount) {
successCallback();
} else {
timer();
}
}, 5);
};
timer();
};
class ServiceMock { class ServiceMock {
constructor(endpoint) { constructor(endpoint) {
this.service = Vue.resource(endpoint); this.service = Vue.resource(endpoint);
...@@ -16,6 +30,7 @@ class ServiceMock { ...@@ -16,6 +30,7 @@ class ServiceMock {
describe('Poll', () => { describe('Poll', () => {
let callbacks; let callbacks;
let service;
beforeEach(() => { beforeEach(() => {
callbacks = { callbacks = {
...@@ -23,8 +38,11 @@ describe('Poll', () => { ...@@ -23,8 +38,11 @@ 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) => {
...@@ -35,19 +53,20 @@ describe('Poll', () => { ...@@ -35,19 +53,20 @@ describe('Poll', () => {
Vue.http.interceptors.push(successInterceptor); Vue.http.interceptors.push(successInterceptor);
new Poll({ new Poll({
resource: new ServiceMock('endpoint'), resource: service,
method: 'fetch', method: 'fetch',
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
}).makeRequest(); }).makeRequest();
setTimeout(() => { waitForAllCallsToFinish(service, 1, () => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor);
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) => {
...@@ -58,19 +77,19 @@ describe('Poll', () => { ...@@ -58,19 +77,19 @@ describe('Poll', () => {
Vue.http.interceptors.push(errorInterceptor); Vue.http.interceptors.push(errorInterceptor);
new Poll({ new Poll({
resource: new ServiceMock('endpoint'), resource: service,
method: 'fetch', method: 'fetch',
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
}).makeRequest(); }).makeRequest();
setTimeout(() => { waitForAllCallsToFinish(service, 1, () => {
expect(callbacks.success).not.toHaveBeenCalled(); expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled(); expect(callbacks.error).toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor);
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) => {
...@@ -81,7 +100,7 @@ describe('Poll', () => { ...@@ -81,7 +100,7 @@ describe('Poll', () => {
Vue.http.interceptors.push(intervalInterceptor); Vue.http.interceptors.push(intervalInterceptor);
new Poll({ new Poll({
resource: new ServiceMock('endpoint'), resource: service,
method: 'fetch', method: 'fetch',
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
...@@ -90,10 +109,11 @@ describe('Poll', () => { ...@@ -90,10 +109,11 @@ describe('Poll', () => {
setTimeout(() => { setTimeout(() => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 0);
Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor);
done();
}, 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) => {
...@@ -103,26 +123,28 @@ describe('Poll', () => { ...@@ -103,26 +123,28 @@ describe('Poll', () => {
Vue.http.interceptors.push(pollInterceptor); Vue.http.interceptors.push(pollInterceptor);
const service = new ServiceMock('endpoint'); const Polling = new Poll({
spyOn(service, 'fetch').and.callThrough();
new Poll({
resource: service, resource: service,
method: 'fetch', method: 'fetch',
data: { page: 1 }, data: { page: 1 },
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
}).makeRequest(); });
Polling.makeRequest();
waitForAllCallsToFinish(service, 2, () => {
Polling.stop();
setTimeout(() => {
expect(service.fetch.calls.count()).toEqual(2); expect(service.fetch.calls.count()).toEqual(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
done();
}, 5);
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
done();
});
}); });
describe('stop', () => { describe('stop', () => {
...@@ -133,9 +155,6 @@ describe('Poll', () => { ...@@ -133,9 +155,6 @@ describe('Poll', () => {
Vue.http.interceptors.push(pollInterceptor); Vue.http.interceptors.push(pollInterceptor);
const service = new ServiceMock('endpoint');
spyOn(service, 'fetch').and.callThrough();
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
method: 'fetch', method: 'fetch',
...@@ -150,14 +169,15 @@ describe('Poll', () => { ...@@ -150,14 +169,15 @@ describe('Poll', () => {
Polling.makeRequest(); Polling.makeRequest();
setTimeout(() => { waitForAllCallsToFinish(service, 1, () => {
expect(service.fetch.calls.count()).toEqual(1); expect(service.fetch.calls.count()).toEqual(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled(); expect(Polling.stop).toHaveBeenCalled();
done();
}, 100);
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
done();
});
}); });
}); });
...@@ -169,10 +189,6 @@ describe('Poll', () => { ...@@ -169,10 +189,6 @@ describe('Poll', () => {
Vue.http.interceptors.push(pollInterceptor); Vue.http.interceptors.push(pollInterceptor);
const service = new ServiceMock('endpoint');
spyOn(service, 'fetch').and.callThrough();
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
method: 'fetch', method: 'fetch',
...@@ -187,17 +203,22 @@ describe('Poll', () => { ...@@ -187,17 +203,22 @@ describe('Poll', () => {
}); });
spyOn(Polling, 'stop').and.callThrough(); spyOn(Polling, 'stop').and.callThrough();
spyOn(Polling, 'restart').and.callThrough();
Polling.makeRequest(); Polling.makeRequest();
setTimeout(() => { waitForAllCallsToFinish(service, 2, () => {
Polling.stop();
expect(service.fetch.calls.count()).toEqual(2); expect(service.fetch.calls.count()).toEqual(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 }); expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled(); expect(Polling.stop).toHaveBeenCalled();
done(); expect(Polling.restart).toHaveBeenCalled();
}, 10);
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor); Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
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