debian-mirror-gitlab/spec/frontend/lib/utils/poll_spec.js

226 lines
6 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
import Poll from '~/lib/utils/poll';
2018-11-18 11:00:15 +05:30
import { successCodes } from '~/lib/utils/http_status';
2020-05-24 23:13:21 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2017-08-17 22:00:37 +05:30
describe('Poll', () => {
2020-05-24 23:13:21 +05:30
let callbacks;
let service;
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
function setup() {
return new Poll({
2017-08-17 22:00:37 +05:30
resource: service,
method: 'fetch',
successCallback: callbacks.success,
errorCallback: callbacks.error,
2017-09-10 17:25:29 +05:30
notificationCallback: callbacks.notification,
2017-08-17 22:00:37 +05:30
}).makeRequest();
2017-09-10 17:25:29 +05:30
}
2020-05-24 23:13:21 +05:30
const mockServiceCall = (response, shouldFail = false) => {
const value = {
...response,
header: response.header || {},
};
if (shouldFail) {
service.fetch.mockRejectedValue(value);
} else {
service.fetch.mockResolvedValue(value);
}
};
const waitForAllCallsToFinish = (waitForCount, successCallback) => {
if (!waitForCount) {
return Promise.resolve().then(successCallback());
}
jest.runOnlyPendingTimers();
return waitForPromises().then(() => waitForAllCallsToFinish(waitForCount - 1, successCallback));
};
beforeEach(() => {
service = {
fetch: jest.fn(),
};
callbacks = {
success: jest.fn(),
error: jest.fn(),
notification: jest.fn(),
};
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('calls the success callback when no header for interval is provided', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 200 });
2017-09-10 17:25:29 +05:30
setup();
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(1, () => {
2017-08-17 22:00:37 +05:30
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
2017-09-10 17:25:29 +05:30
});
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('calls the error callback when the http request returns an error', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 500 }, true);
2017-09-10 17:25:29 +05:30
setup();
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(1, () => {
2017-08-17 22:00:37 +05:30
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled();
done();
});
});
2018-12-13 13:39:08 +05:30
it('skips the error callback when request is aborted', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 0 }, true);
2017-09-10 17:25:29 +05:30
setup();
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(1, () => {
2017-09-10 17:25:29 +05:30
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
expect(callbacks.notification).toHaveBeenCalled();
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
done();
});
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should call the success callback when the interval header is -1', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 200, headers: { 'poll-interval': -1 } });
2018-12-13 13:39:08 +05:30
setup()
.then(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
done();
})
.catch(done.fail);
2017-08-17 22:00:37 +05:30
});
2018-11-18 11:00:15 +05:30
describe('for 2xx status code', () => {
successCodes.forEach(httpCode => {
2018-12-13 13:39:08 +05:30
it(`starts polling when http status is ${httpCode} and interval header is provided`, done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: httpCode, headers: { 'poll-interval': 1 } });
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: callbacks.success,
errorCallback: callbacks.error,
});
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
Polling.makeRequest();
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(2, () => {
2018-11-18 11:00:15 +05:30
Polling.stop();
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
expect(service.fetch.mock.calls).toHaveLength(2);
2018-11-18 11:00:15 +05:30
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
done();
});
});
2017-08-17 22:00:37 +05:30
});
});
describe('stop', () => {
2018-12-13 13:39:08 +05:30
it('stops polling when method is called', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 200, headers: { 'poll-interval': 1 } });
2017-08-17 22:00:37 +05:30
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {
Polling.stop();
},
errorCallback: callbacks.error,
});
2020-05-24 23:13:21 +05:30
jest.spyOn(Polling, 'stop');
2017-08-17 22:00:37 +05:30
Polling.makeRequest();
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(1, () => {
expect(service.fetch.mock.calls).toHaveLength(1);
2017-08-17 22:00:37 +05:30
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled();
done();
});
});
});
2019-05-18 00:54:41 +05:30
describe('enable', () => {
it('should enable polling upon a response', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 200 });
2019-05-18 00:54:41 +05:30
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {},
});
Polling.enable({
data: { page: 4 },
response: { status: 200, headers: { 'poll-interval': 1 } },
});
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(1, () => {
2019-05-18 00:54:41 +05:30
Polling.stop();
2020-05-24 23:13:21 +05:30
expect(service.fetch.mock.calls).toHaveLength(1);
2019-05-18 00:54:41 +05:30
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
2017-08-17 22:00:37 +05:30
describe('restart', () => {
2018-12-13 13:39:08 +05:30
it('should restart polling when its called', done => {
2020-05-24 23:13:21 +05:30
mockServiceCall({ status: 200, headers: { 'poll-interval': 1 } });
2017-08-17 22:00:37 +05:30
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {
Polling.stop();
2020-05-24 23:13:21 +05:30
// Let's pretend that we asynchronously restart this.
// setTimeout is mocked but this will actually get triggered
// in waitForAllCalssToFinish.
2017-08-17 22:00:37 +05:30
setTimeout(() => {
2018-03-17 18:26:18 +05:30
Polling.restart({ data: { page: 4 } });
2020-05-24 23:13:21 +05:30
}, 1);
2017-08-17 22:00:37 +05:30
},
errorCallback: callbacks.error,
});
2020-05-24 23:13:21 +05:30
jest.spyOn(Polling, 'stop');
jest.spyOn(Polling, 'enable');
jest.spyOn(Polling, 'restart');
2017-08-17 22:00:37 +05:30
Polling.makeRequest();
2020-05-24 23:13:21 +05:30
waitForAllCallsToFinish(2, () => {
2017-08-17 22:00:37 +05:30
Polling.stop();
2020-05-24 23:13:21 +05:30
expect(service.fetch.mock.calls).toHaveLength(2);
2018-03-17 18:26:18 +05:30
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
2017-08-17 22:00:37 +05:30
expect(Polling.stop).toHaveBeenCalled();
2019-05-18 00:54:41 +05:30
expect(Polling.enable).toHaveBeenCalled();
2017-08-17 22:00:37 +05:30
expect(Polling.restart).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
expect(Polling.options.data).toEqual({ page: 4 });
2017-08-17 22:00:37 +05:30
done();
});
});
});
});