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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
2020-10-24 23:57:45 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2020-03-13 15:44:24 +05:30
import axios from '~/lib/utils/axios_utils';
2023-03-17 16:20:25 +05:30
import {
HTTP_STATUS_NO_CONTENT,
HTTP_STATUS_NOT_FOUND,
HTTP_STATUS_OK,
} from '~/lib/utils/http_status';
2021-03-11 19:13:27 +05:30
import pollUntilComplete from '~/lib/utils/poll_until_complete';
2020-03-13 15:44:24 +05:30
const endpoint = `${TEST_HOST}/foo`;
const mockData = 'mockData';
const pollInterval = 1234;
const pollIntervalHeader = {
'Poll-Interval': pollInterval,
};
describe('pollUntilComplete', () => {
let mock;
beforeEach(() => {
mock = new AxiosMockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('given an immediate success response', () => {
beforeEach(() => {
2023-03-17 16:20:25 +05:30
mock.onGet(endpoint).replyOnce(HTTP_STATUS_OK, mockData);
2020-03-13 15:44:24 +05:30
});
it('resolves with the response', () =>
pollUntilComplete(endpoint).then(({ data }) => {
expect(data).toBe(mockData);
}));
});
describe(`given the endpoint returns NO_CONTENT with a Poll-Interval before succeeding`, () => {
beforeEach(() => {
mock
.onGet(endpoint)
2023-03-04 22:38:38 +05:30
.replyOnce(HTTP_STATUS_NO_CONTENT, undefined, pollIntervalHeader)
2020-03-13 15:44:24 +05:30
.onGet(endpoint)
2023-03-17 16:20:25 +05:30
.replyOnce(HTTP_STATUS_OK, mockData);
2020-03-13 15:44:24 +05:30
});
it('calls the endpoint until it succeeds, and resolves with the response', () =>
Promise.all([
pollUntilComplete(endpoint).then(({ data }) => {
expect(data).toBe(mockData);
expect(mock.history.get).toHaveLength(2);
}),
// To ensure the above pollUntilComplete() promise is actually
// fulfilled, we must explictly run the timers forward by the time
// indicated in the headers *after* each previous request has been
// fulfilled.
axios
// wait for initial NO_CONTENT response to be fulfilled
.waitForAll()
.then(() => {
jest.advanceTimersByTime(pollInterval);
}),
]));
});
describe('given the endpoint returns an error status', () => {
const errorMessage = 'error message';
beforeEach(() => {
2023-03-17 16:20:25 +05:30
mock.onGet(endpoint).replyOnce(HTTP_STATUS_NOT_FOUND, errorMessage);
2020-03-13 15:44:24 +05:30
});
it('rejects with the error response', () =>
2021-03-08 18:12:59 +05:30
pollUntilComplete(endpoint).catch((error) => {
2020-03-13 15:44:24 +05:30
expect(error.response.data).toBe(errorMessage);
}));
});
describe('given params', () => {
const params = { foo: 'bar' };
beforeEach(() => {
2023-03-17 16:20:25 +05:30
mock.onGet(endpoint, { params }).replyOnce(HTTP_STATUS_OK, mockData);
2020-03-13 15:44:24 +05:30
});
it('requests the expected URL', () =>
pollUntilComplete(endpoint, { params }).then(({ data }) => {
expect(data).toBe(mockData);
}));
});
});