diff options
author | Martin Hanzel <martin@hanzel.io> | 2019-09-05 12:56:17 +0000 |
---|---|---|
committer | Paul Slaughter <pslaughter@gitlab.com> | 2019-09-05 12:56:17 +0000 |
commit | bd20aeb64c4eed117831556c54b40ff4aee9bfd1 (patch) | |
tree | 6a94bdd21bc56943f159e9422249c41a0713823c /spec/frontend/lib/utils/axios_utils_spec.js | |
parent | 914af2f516acfa05cead52e438a35464abb4ac1d (diff) | |
download | gitlab-ce-bd20aeb64c4eed117831556c54b40ff4aee9bfd1.tar.gz |
Add helpers to wait for axios requests
Add two methods to the axios_utils Jest mock:
- `waitFor(url)`, which returns a Promise that resolves when the
next request to `url` finishes.
- `waitForAll()`, which returns a Promise that resolves when all
pending requests finish.
Diffstat (limited to 'spec/frontend/lib/utils/axios_utils_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/axios_utils_spec.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/axios_utils_spec.js b/spec/frontend/lib/utils/axios_utils_spec.js new file mode 100644 index 00000000000..d5c39567f06 --- /dev/null +++ b/spec/frontend/lib/utils/axios_utils_spec.js @@ -0,0 +1,45 @@ +/* eslint-disable promise/catch-or-return */ + +import AxiosMockAdapter from 'axios-mock-adapter'; + +import axios from '~/lib/utils/axios_utils'; + +describe('axios_utils', () => { + let mock; + + beforeEach(() => { + mock = new AxiosMockAdapter(axios); + mock.onAny('/ok').reply(200); + mock.onAny('/err').reply(500); + expect(axios.countActiveRequests()).toBe(0); + }); + + afterEach(() => axios.waitForAll().finally(() => mock.restore())); + + describe('waitForAll', () => { + it('resolves if there are no requests', () => axios.waitForAll()); + + it('waits for all requests to finish', () => { + const handler = jest.fn(); + axios.get('/ok').then(handler); + axios.get('/err').catch(handler); + + return axios.waitForAll().finally(() => { + expect(handler).toHaveBeenCalledTimes(2); + expect(handler.mock.calls[0][0].status).toBe(200); + expect(handler.mock.calls[1][0].response.status).toBe(500); + }); + }); + }); + + describe('waitFor', () => { + it('waits for requests on a specific URL', () => { + const handler = jest.fn(); + axios.get('/ok').finally(handler); + axios.waitFor('/err').finally(() => { + throw new Error('waitFor on /err should not be called'); + }); + return axios.waitFor('/ok'); + }); + }); +}); |