summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/axios_utils_spec.js
blob: d5c39567f06d2a1d536627bbe4a953dbb1980dbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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');
    });
  });
});