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

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

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-12-04 20:38:33 +05:30
/* eslint-disable promise/catch-or-return */
import AxiosMockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
2019-12-04 20:38:33 +05:30
describe('axios_utils', () => {
let mock;
beforeEach(() => {
mock = new AxiosMockAdapter(axios);
2023-04-23 21:23:45 +05:30
mock.onAny('/ok').reply(HTTP_STATUS_OK);
mock.onAny('/err').reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
2020-05-24 23:13:21 +05:30
// eslint-disable-next-line jest/no-standalone-expect
2019-12-04 20:38:33 +05:30
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);
2023-04-23 21:23:45 +05:30
expect(handler.mock.calls[0][0].status).toBe(HTTP_STATUS_OK);
expect(handler.mock.calls[1][0].response.status).toBe(HTTP_STATUS_INTERNAL_SERVER_ERROR);
2019-12-04 20:38:33 +05:30
});
});
});
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');
});
});
});