summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/poll_until_complete_spec.js
blob: 3ce17ecfc8ce86a56bbae041ed21f9323154a1ab (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import AxiosMockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants';
import axios from '~/lib/utils/axios_utils';
import httpStatusCodes, { HTTP_STATUS_NO_CONTENT } from '~/lib/utils/http_status';
import pollUntilComplete from '~/lib/utils/poll_until_complete';

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(HTTP_STATUS_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);
      }));
  });
});