summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/stores/modules/terminal/actions/checks_spec.js
blob: 09be1e333b3f9cec15a733c7cc07a063f53f09b5 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import { TEST_HOST } from 'spec/test_constants';
import * as actions from '~/ide/stores/modules/terminal/actions/checks';
import {
  CHECK_CONFIG,
  CHECK_RUNNERS,
  RETRY_RUNNERS_INTERVAL,
} from '~/ide/stores/modules/terminal/constants';
import * as messages from '~/ide/stores/modules/terminal/messages';
import * as mutationTypes from '~/ide/stores/modules/terminal/mutation_types';
import axios from '~/lib/utils/axios_utils';
import {
  HTTP_STATUS_FORBIDDEN,
  HTTP_STATUS_NOT_FOUND,
  HTTP_STATUS_UNPROCESSABLE_ENTITY,
} from '~/lib/utils/http_status';

const TEST_PROJECT_PATH = 'lorem/root';
const TEST_BRANCH_ID = 'main';
const TEST_YAML_HELP_PATH = `${TEST_HOST}/test/yaml/help`;
const TEST_RUNNERS_HELP_PATH = `${TEST_HOST}/test/runners/help`;

describe('IDE store terminal check actions', () => {
  let mock;
  let state;
  let rootState;
  let rootGetters;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    state = {
      paths: {
        webTerminalConfigHelpPath: TEST_YAML_HELP_PATH,
        webTerminalRunnersHelpPath: TEST_RUNNERS_HELP_PATH,
      },
      checks: {
        config: { isLoading: true },
      },
    };
    rootState = {
      currentBranchId: TEST_BRANCH_ID,
    };
    rootGetters = {
      currentProject: {
        id: 7,
        path_with_namespace: TEST_PROJECT_PATH,
      },
    };
  });

  afterEach(() => {
    mock.restore();
  });

  describe('requestConfigCheck', () => {
    it('handles request loading', () => {
      return testAction(
        actions.requestConfigCheck,
        null,
        {},
        [{ type: mutationTypes.REQUEST_CHECK, payload: CHECK_CONFIG }],
        [],
      );
    });
  });

  describe('receiveConfigCheckSuccess', () => {
    it('handles successful response', () => {
      return testAction(
        actions.receiveConfigCheckSuccess,
        null,
        {},
        [
          { type: mutationTypes.SET_VISIBLE, payload: true },
          { type: mutationTypes.RECEIVE_CHECK_SUCCESS, payload: CHECK_CONFIG },
        ],
        [],
      );
    });
  });

  describe('receiveConfigCheckError', () => {
    it('handles error response', () => {
      const status = HTTP_STATUS_UNPROCESSABLE_ENTITY;
      const payload = { response: { status } };

      return testAction(
        actions.receiveConfigCheckError,
        payload,
        state,
        [
          {
            type: mutationTypes.SET_VISIBLE,
            payload: true,
          },
          {
            type: mutationTypes.RECEIVE_CHECK_ERROR,
            payload: {
              type: CHECK_CONFIG,
              message: messages.configCheckError(status, TEST_YAML_HELP_PATH),
            },
          },
        ],
        [],
      );
    });

    [HTTP_STATUS_FORBIDDEN, HTTP_STATUS_NOT_FOUND].forEach((status) => {
      it(`hides tab, when status is ${status}`, () => {
        const payload = { response: { status } };

        return testAction(
          actions.receiveConfigCheckError,
          payload,
          state,
          [
            {
              type: mutationTypes.SET_VISIBLE,
              payload: false,
            },
            expect.objectContaining({ type: mutationTypes.RECEIVE_CHECK_ERROR }),
          ],
          [],
        );
      });
    });
  });

  describe('fetchConfigCheck', () => {
    it('dispatches request and receive', () => {
      mock.onPost(/.*\/ide_terminals\/check_config/).reply(200, {});

      return testAction(
        actions.fetchConfigCheck,
        null,
        {
          ...rootGetters,
          ...rootState,
        },
        [],
        [{ type: 'requestConfigCheck' }, { type: 'receiveConfigCheckSuccess' }],
      );
    });

    it('when error, dispatches request and receive', () => {
      mock.onPost(/.*\/ide_terminals\/check_config/).reply(400, {});

      return testAction(
        actions.fetchConfigCheck,
        null,
        {
          ...rootGetters,
          ...rootState,
        },
        [],
        [
          { type: 'requestConfigCheck' },
          { type: 'receiveConfigCheckError', payload: expect.any(Error) },
        ],
      );
    });
  });

  describe('requestRunnersCheck', () => {
    it('handles request loading', () => {
      return testAction(
        actions.requestRunnersCheck,
        null,
        {},
        [{ type: mutationTypes.REQUEST_CHECK, payload: CHECK_RUNNERS }],
        [],
      );
    });
  });

  describe('receiveRunnersCheckSuccess', () => {
    it('handles successful response, with data', () => {
      const payload = [{}];

      return testAction(
        actions.receiveRunnersCheckSuccess,
        payload,
        state,
        [{ type: mutationTypes.RECEIVE_CHECK_SUCCESS, payload: CHECK_RUNNERS }],
        [],
      );
    });

    it('handles successful response, with empty data', () => {
      const commitPayload = {
        type: CHECK_RUNNERS,
        message: messages.runnersCheckEmpty(TEST_RUNNERS_HELP_PATH),
      };

      return testAction(
        actions.receiveRunnersCheckSuccess,
        [],
        state,
        [{ type: mutationTypes.RECEIVE_CHECK_ERROR, payload: commitPayload }],
        [{ type: 'retryRunnersCheck' }],
      );
    });
  });

  describe('receiveRunnersCheckError', () => {
    it('dispatches handle with message', () => {
      const commitPayload = {
        type: CHECK_RUNNERS,
        message: messages.UNEXPECTED_ERROR_RUNNERS,
      };

      return testAction(
        actions.receiveRunnersCheckError,
        null,
        {},
        [{ type: mutationTypes.RECEIVE_CHECK_ERROR, payload: commitPayload }],
        [],
      );
    });
  });

  describe('retryRunnersCheck', () => {
    it('dispatches fetch again after timeout', () => {
      const dispatch = jest.fn().mockName('dispatch');

      actions.retryRunnersCheck({ dispatch, state });

      expect(dispatch).not.toHaveBeenCalled();

      jest.advanceTimersByTime(RETRY_RUNNERS_INTERVAL + 1);

      expect(dispatch).toHaveBeenCalledWith('fetchRunnersCheck', { background: true });
    });

    it('does not dispatch fetch if config check is error', () => {
      const dispatch = jest.fn().mockName('dispatch');
      state.checks.config = {
        isLoading: false,
        isValid: false,
      };

      actions.retryRunnersCheck({ dispatch, state });

      expect(dispatch).not.toHaveBeenCalled();

      jest.advanceTimersByTime(RETRY_RUNNERS_INTERVAL + 1);

      expect(dispatch).not.toHaveBeenCalled();
    });
  });

  describe('fetchRunnersCheck', () => {
    it('dispatches request and receive', () => {
      mock.onGet(/api\/.*\/projects\/.*\/runners/, { params: { scope: 'active' } }).reply(200, []);

      return testAction(
        actions.fetchRunnersCheck,
        {},
        rootGetters,
        [],
        [{ type: 'requestRunnersCheck' }, { type: 'receiveRunnersCheckSuccess', payload: [] }],
      );
    });

    it('does not dispatch request when background is true', () => {
      mock.onGet(/api\/.*\/projects\/.*\/runners/, { params: { scope: 'active' } }).reply(200, []);

      return testAction(
        actions.fetchRunnersCheck,
        { background: true },
        rootGetters,
        [],
        [{ type: 'receiveRunnersCheckSuccess', payload: [] }],
      );
    });

    it('dispatches request and receive, when error', () => {
      mock.onGet(/api\/.*\/projects\/.*\/runners/, { params: { scope: 'active' } }).reply(500, []);

      return testAction(
        actions.fetchRunnersCheck,
        {},
        rootGetters,
        [],
        [
          { type: 'requestRunnersCheck' },
          { type: 'receiveRunnersCheckError', payload: expect.any(Error) },
        ],
      );
    });
  });
});