summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_new/components/pipeline_new_form_spec.js
blob: 197f646a22e5409a1eb0e7d52045dd2c70260fb3 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { mount, shallowMount } from '@vue/test-utils';
import { GlDropdown, GlDropdownItem, GlForm, GlSprintf, GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import httpStatusCodes from '~/lib/utils/http_status';
import axios from '~/lib/utils/axios_utils';
import PipelineNewForm from '~/pipeline_new/components/pipeline_new_form.vue';
import { mockRefs, mockParams, mockPostParams, mockProjectId, mockError } from '../mock_data';
import { redirectTo } from '~/lib/utils/url_utility';

jest.mock('~/lib/utils/url_utility', () => ({
  redirectTo: jest.fn(),
}));

const pipelinesPath = '/root/project/-/pipelines';
const configVariablesPath = '/root/project/-/pipelines/config_variables';
const postResponse = { id: 1 };

describe('Pipeline New Form', () => {
  let wrapper;
  let mock;

  const dummySubmitEvent = {
    preventDefault() {},
  };

  const findForm = () => wrapper.find(GlForm);
  const findDropdown = () => wrapper.find(GlDropdown);
  const findDropdownItems = () => wrapper.findAll(GlDropdownItem);
  const findVariableRows = () => wrapper.findAll('[data-testid="ci-variable-row"]');
  const findRemoveIcons = () => wrapper.findAll('[data-testid="remove-ci-variable-row"]');
  const findKeyInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-key"]');
  const findValueInputs = () => wrapper.findAll('[data-testid="pipeline-form-ci-variable-value"]');
  const findErrorAlert = () => wrapper.find('[data-testid="run-pipeline-error-alert"]');
  const findWarningAlert = () => wrapper.find('[data-testid="run-pipeline-warning-alert"]');
  const findWarningAlertSummary = () => findWarningAlert().find(GlSprintf);
  const findWarnings = () => wrapper.findAll('[data-testid="run-pipeline-warning"]');
  const findLoadingIcon = () => wrapper.find(GlLoadingIcon);
  const getExpectedPostParams = () => JSON.parse(mock.history.post[0].data);

  const createComponent = (term = '', props = {}, method = shallowMount) => {
    wrapper = method(PipelineNewForm, {
      propsData: {
        projectId: mockProjectId,
        pipelinesPath,
        configVariablesPath,
        refs: mockRefs,
        defaultBranch: 'master',
        settingsLink: '',
        maxWarnings: 25,
        ...props,
      },
      data() {
        return {
          searchTerm: term,
        };
      },
    });
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
    mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {});
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;

    mock.restore();
  });

  describe('Dropdown with branches and tags', () => {
    beforeEach(() => {
      mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, postResponse);
    });

    it('displays dropdown with all branches and tags', () => {
      createComponent();
      expect(findDropdownItems()).toHaveLength(mockRefs.length);
    });

    it('when user enters search term the list is filtered', () => {
      createComponent('master');

      expect(findDropdownItems()).toHaveLength(1);
      expect(
        findDropdownItems()
          .at(0)
          .text(),
      ).toBe('master');
    });
  });

  describe('Form', () => {
    beforeEach(async () => {
      createComponent('', mockParams, mount);

      mock.onPost(pipelinesPath).reply(httpStatusCodes.OK, postResponse);

      await waitForPromises();
    });

    it('displays the correct values for the provided query params', async () => {
      expect(findDropdown().props('text')).toBe('tag-1');
      expect(findVariableRows()).toHaveLength(3);
    });

    it('displays a variable from provided query params', () => {
      expect(findKeyInputs().at(0).element.value).toBe('test_var');
      expect(findValueInputs().at(0).element.value).toBe('test_var_val');
    });

    it('displays an empty variable for the user to fill out', async () => {
      expect(findKeyInputs().at(2).element.value).toBe('');
      expect(findValueInputs().at(2).element.value).toBe('');
    });

    it('does not display remove icon for last row', () => {
      expect(findRemoveIcons()).toHaveLength(2);
    });

    it('removes ci variable row on remove icon button click', async () => {
      findRemoveIcons()
        .at(1)
        .trigger('click');

      await wrapper.vm.$nextTick();

      expect(findVariableRows()).toHaveLength(2);
    });

    it('creates a pipeline on submit', async () => {
      findForm().vm.$emit('submit', dummySubmitEvent);

      await waitForPromises();

      expect(getExpectedPostParams()).toEqual(mockPostParams);
      expect(redirectTo).toHaveBeenCalledWith(`${pipelinesPath}/${postResponse.id}`);
    });

    it('creates blank variable on input change event', async () => {
      const input = findKeyInputs().at(2);
      input.element.value = 'test_var_2';
      input.trigger('change');

      await wrapper.vm.$nextTick();

      expect(findVariableRows()).toHaveLength(4);
      expect(findKeyInputs().at(3).element.value).toBe('');
      expect(findValueInputs().at(3).element.value).toBe('');
    });

    describe('when the form has been modified', () => {
      const selectRef = i =>
        findDropdownItems()
          .at(i)
          .vm.$emit('click');

      beforeEach(async () => {
        const input = findKeyInputs().at(0);
        input.element.value = 'test_var_2';
        input.trigger('change');

        findRemoveIcons()
          .at(1)
          .trigger('click');

        await wrapper.vm.$nextTick();
      });

      it('form values are restored when the ref changes', async () => {
        expect(findVariableRows()).toHaveLength(2);

        selectRef(1);
        await waitForPromises();

        expect(findVariableRows()).toHaveLength(3);
        expect(findKeyInputs().at(0).element.value).toBe('test_var');
      });

      it('form values are restored again when the ref is reverted', async () => {
        selectRef(1);
        await waitForPromises();

        selectRef(2);
        await waitForPromises();

        expect(findVariableRows()).toHaveLength(2);
        expect(findKeyInputs().at(0).element.value).toBe('test_var_2');
      });
    });
  });

  describe('when feature flag new_pipeline_form_prefilled_vars is enabled', () => {
    let origGon;

    const mockYmlKey = 'yml_var';
    const mockYmlValue = 'yml_var_val';
    const mockYmlDesc = 'A var from yml.';

    beforeAll(() => {
      origGon = window.gon;
      window.gon = { features: { newPipelineFormPrefilledVars: true } };
    });

    afterAll(() => {
      window.gon = origGon;
    });

    describe('loading state', () => {
      it('loading icon is shown when content is requested and hidden when received', async () => {
        createComponent('', mockParams, mount);

        mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
          [mockYmlKey]: {
            value: mockYmlValue,
            description: mockYmlDesc,
          },
        });

        expect(findLoadingIcon().exists()).toBe(true);

        await waitForPromises();

        expect(findLoadingIcon().exists()).toBe(false);
      });
    });

    describe('when yml defines a variable with description', () => {
      beforeEach(async () => {
        createComponent('', mockParams, mount);

        mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
          [mockYmlKey]: {
            value: mockYmlValue,
            description: mockYmlDesc,
          },
        });

        await waitForPromises();
      });

      it('displays all the variables', async () => {
        expect(findVariableRows()).toHaveLength(4);
      });

      it('displays a variable from yml', () => {
        expect(findKeyInputs().at(0).element.value).toBe(mockYmlKey);
        expect(findValueInputs().at(0).element.value).toBe(mockYmlValue);
      });

      it('displays a variable from provided query params', () => {
        expect(findKeyInputs().at(1).element.value).toBe('test_var');
        expect(findValueInputs().at(1).element.value).toBe('test_var_val');
      });

      it('adds a description to the first variable from yml', () => {
        expect(
          findVariableRows()
            .at(0)
            .text(),
        ).toContain(mockYmlDesc);
      });

      it('removes the description when a variable key changes', async () => {
        findKeyInputs().at(0).element.value = 'yml_var_modified';
        findKeyInputs()
          .at(0)
          .trigger('change');

        await wrapper.vm.$nextTick();

        expect(
          findVariableRows()
            .at(0)
            .text(),
        ).not.toContain(mockYmlDesc);
      });
    });

    describe('when yml defines a variable without description', () => {
      beforeEach(async () => {
        createComponent('', mockParams, mount);

        mock.onGet(configVariablesPath).reply(httpStatusCodes.OK, {
          [mockYmlKey]: {
            value: mockYmlValue,
            description: null,
          },
        });

        await waitForPromises();
      });

      it('displays all the variables', async () => {
        expect(findVariableRows()).toHaveLength(3);
      });
    });
  });

  describe('Form errors and warnings', () => {
    beforeEach(() => {
      createComponent();

      mock.onPost(pipelinesPath).reply(httpStatusCodes.BAD_REQUEST, mockError);

      findForm().vm.$emit('submit', dummySubmitEvent);

      return waitForPromises();
    });

    it('shows both error and warning', () => {
      expect(findErrorAlert().exists()).toBe(true);
      expect(findWarningAlert().exists()).toBe(true);
    });

    it('shows the correct error', () => {
      expect(findErrorAlert().text()).toBe(mockError.errors[0]);
    });

    it('shows the correct warning title', () => {
      const { length } = mockError.warnings;
      expect(findWarningAlertSummary().attributes('message')).toBe(`${length} warnings found:`);
    });

    it('shows the correct amount of warnings', () => {
      expect(findWarnings()).toHaveLength(mockError.warnings.length);
    });
  });
});