diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-10 21:07:43 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-10 21:07:43 +0300 |
commit | 6f0f893bd87535b61e0ecb1ce069eaa7fcb9e5be (patch) | |
tree | 8af92b29c838e9af2fd70f9a4a2314a08f4af922 /spec/frontend/lib | |
parent | 8b1228b0d409d7751f01d9fb72ebfbbf62399486 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/lib')
-rw-r--r-- | spec/frontend/lib/utils/poll_until_complete_spec.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/poll_until_complete_spec.js b/spec/frontend/lib/utils/poll_until_complete_spec.js new file mode 100644 index 00000000000..15602b87b9c --- /dev/null +++ b/spec/frontend/lib/utils/poll_until_complete_spec.js @@ -0,0 +1,89 @@ +import AxiosMockAdapter from 'axios-mock-adapter'; +import axios from '~/lib/utils/axios_utils'; +import pollUntilComplete from '~/lib/utils/poll_until_complete'; +import httpStatusCodes from '~/lib/utils/http_status'; +import { TEST_HOST } from 'helpers/test_constants'; + +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(() => { + mock.onGet(endpoint).replyOnce(httpStatusCodes.OK, mockData); + }); + + 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) + .replyOnce(httpStatusCodes.NO_CONTENT, undefined, pollIntervalHeader) + .onGet(endpoint) + .replyOnce(httpStatusCodes.OK, mockData); + }); + + 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(() => { + mock.onGet(endpoint).replyOnce(httpStatusCodes.NOT_FOUND, errorMessage); + }); + + it('rejects with the error response', () => + pollUntilComplete(endpoint).catch(error => { + expect(error.response.data).toBe(errorMessage); + })); + }); + + describe('given params', () => { + const params = { foo: 'bar' }; + beforeEach(() => { + mock.onGet(endpoint, { params }).replyOnce(httpStatusCodes.OK, mockData); + }); + + it('requests the expected URL', () => + pollUntilComplete(endpoint, { params }).then(({ data }) => { + expect(data).toBe(mockData); + })); + }); +}); |