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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlForm, GlDropdownItem, GlSprintf, GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import CreditCardValidationRequiredAlert from 'ee_component/billings/components/cc_validation_required_alert.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended, mountExtended } from 'helpers/vue_test_utils_helper';
import { TEST_HOST } from 'helpers/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import {
HTTP_STATUS_BAD_REQUEST,
HTTP_STATUS_INTERNAL_SERVER_ERROR,
HTTP_STATUS_OK,
} from '~/lib/utils/http_status';
import { redirectTo } from '~/lib/utils/url_utility';
import PipelineNewForm, {
POLLING_INTERVAL,
} from '~/ci/pipeline_new/components/pipeline_new_form.vue';
import ciConfigVariablesQuery from '~/ci/pipeline_new/graphql/queries/ci_config_variables.graphql';
import { resolvers } from '~/ci/pipeline_new/graphql/resolvers';
import RefsDropdown from '~/ci/pipeline_new/components/refs_dropdown.vue';
import {
mockCreditCardValidationRequiredError,
mockCiConfigVariablesResponse,
mockCiConfigVariablesResponseWithoutDesc,
mockEmptyCiConfigVariablesResponse,
mockError,
mockNoCachedCiConfigVariablesResponse,
mockQueryParams,
mockPostParams,
mockProjectId,
mockRefs,
mockYamlVariables,
mockPipelineConfigButtonText,
} from '../mock_data';
Vue.use(VueApollo);
jest.mock('~/lib/utils/url_utility', () => ({
redirectTo: jest.fn(),
}));
const projectRefsEndpoint = '/root/project/refs';
const pipelinesPath = '/root/project/-/pipelines';
const pipelinesEditorPath = '/root/project/-/ci/editor';
const projectPath = '/root/project/-/pipelines/config_variables';
const newPipelinePostResponse = { id: 1 };
const defaultBranch = 'main';
describe('Pipeline New Form', () => {
let wrapper;
let mock;
let mockApollo;
let mockCiConfigVariables;
let dummySubmitEvent;
const findForm = () => wrapper.findComponent(GlForm);
const findRefsDropdown = () => wrapper.findComponent(RefsDropdown);
const findSubmitButton = () => wrapper.findByTestId('run_pipeline_button');
const findVariableRows = () => wrapper.findAllByTestId('ci-variable-row');
const findRemoveIcons = () => wrapper.findAllByTestId('remove-ci-variable-row');
const findVariableTypes = () => wrapper.findAllByTestId('pipeline-form-ci-variable-type');
const findKeyInputs = () => wrapper.findAllByTestId('pipeline-form-ci-variable-key');
const findValueInputs = () => wrapper.findAllByTestId('pipeline-form-ci-variable-value');
const findValueDropdowns = () =>
wrapper.findAllByTestId('pipeline-form-ci-variable-value-dropdown');
const findValueDropdownItems = (dropdown) => dropdown.findAllComponents(GlDropdownItem);
const findErrorAlert = () => wrapper.findByTestId('run-pipeline-error-alert');
const findPipelineConfigButton = () => wrapper.findByTestId('ci-cd-pipeline-configuration');
const findWarningAlert = () => wrapper.findByTestId('run-pipeline-warning-alert');
const findWarningAlertSummary = () => findWarningAlert().findComponent(GlSprintf);
const findWarnings = () => wrapper.findAllByTestId('run-pipeline-warning');
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
const findCCAlert = () => wrapper.findComponent(CreditCardValidationRequiredAlert);
const getFormPostParams = () => JSON.parse(mock.history.post[0].data);
const advanceToNextFetch = (milliseconds) => {
jest.advanceTimersByTime(milliseconds);
};
const selectBranch = async (branch) => {
// Select a branch in the dropdown
findRefsDropdown().vm.$emit('input', {
shortName: branch,
fullName: `refs/heads/${branch}`,
});
await waitForPromises();
};
const changeKeyInputValue = async (keyInputIndex, value) => {
const input = findKeyInputs().at(keyInputIndex);
input.element.value = value;
input.trigger('change');
await nextTick();
};
const createComponentWithApollo = ({ method = shallowMountExtended, props = {} } = {}) => {
const handlers = [[ciConfigVariablesQuery, mockCiConfigVariables]];
mockApollo = createMockApollo(handlers, resolvers);
wrapper = method(PipelineNewForm, {
apolloProvider: mockApollo,
provide: {
projectRefsEndpoint,
},
propsData: {
projectId: mockProjectId,
pipelinesPath,
pipelinesEditorPath,
canViewPipelineEditor: true,
projectPath,
defaultBranch,
refParam: defaultBranch,
settingsLink: '',
maxWarnings: 25,
...props,
},
});
};
beforeEach(() => {
mock = new MockAdapter(axios);
mockCiConfigVariables = jest.fn();
mock.onGet(projectRefsEndpoint).reply(HTTP_STATUS_OK, mockRefs);
dummySubmitEvent = {
preventDefault: jest.fn(),
};
});
afterEach(() => {
mock.restore();
});
describe('Form', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockEmptyCiConfigVariablesResponse);
createComponentWithApollo({ props: mockQueryParams, method: mountExtended });
await waitForPromises();
});
it('displays the correct values for the provided query params', async () => {
expect(findVariableTypes().at(0).props('text')).toBe('Variable');
expect(findVariableTypes().at(1).props('text')).toBe('File');
expect(findRefsDropdown().props('value')).toEqual({ shortName: '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('');
expect(findVariableTypes().at(2).props('text')).toBe('Variable');
});
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 nextTick();
expect(findVariableRows()).toHaveLength(2);
});
it('creates blank variable on input change event', async () => {
const input = findKeyInputs().at(2);
input.element.value = 'test_var_2';
input.trigger('change');
await nextTick();
expect(findVariableRows()).toHaveLength(4);
expect(findKeyInputs().at(3).element.value).toBe('');
expect(findValueInputs().at(3).element.value).toBe('');
});
});
describe('Pipeline creation', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockEmptyCiConfigVariablesResponse);
mock.onPost(pipelinesPath).reply(HTTP_STATUS_OK, newPipelinePostResponse);
});
it('does not submit the native HTML form', async () => {
createComponentWithApollo();
findForm().vm.$emit('submit', dummySubmitEvent);
expect(dummySubmitEvent.preventDefault).toHaveBeenCalled();
});
it('disables the submit button immediately after submitting', async () => {
createComponentWithApollo();
expect(findSubmitButton().props('disabled')).toBe(false);
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
expect(findSubmitButton().props('disabled')).toBe(true);
});
it('creates pipeline with full ref and variables', async () => {
createComponentWithApollo();
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
expect(getFormPostParams().ref).toEqual(`refs/heads/${defaultBranch}`);
expect(redirectTo).toHaveBeenCalledWith(`${pipelinesPath}/${newPipelinePostResponse.id}`);
});
it('creates a pipeline with short ref and variables from the query params', async () => {
createComponentWithApollo({ props: mockQueryParams });
await waitForPromises();
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
expect(getFormPostParams()).toEqual(mockPostParams);
expect(redirectTo).toHaveBeenCalledWith(`${pipelinesPath}/${newPipelinePostResponse.id}`);
});
});
describe('When the ref has been changed', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockEmptyCiConfigVariablesResponse);
createComponentWithApollo({ method: mountExtended });
await waitForPromises();
});
it('variables persist between ref changes', async () => {
await selectBranch('main');
await changeKeyInputValue(0, 'build_var');
await selectBranch('branch-1');
await changeKeyInputValue(0, 'deploy_var');
await selectBranch('main');
expect(findKeyInputs().at(0).element.value).toBe('build_var');
expect(findVariableRows().length).toBe(2);
await selectBranch('branch-1');
expect(findKeyInputs().at(0).element.value).toBe('deploy_var');
expect(findVariableRows().length).toBe(2);
});
it('skips query call when form variables are already cached', async () => {
await selectBranch('main');
await changeKeyInputValue(0, 'build_var');
expect(mockCiConfigVariables).toHaveBeenCalledTimes(1);
await selectBranch('branch-1');
expect(mockCiConfigVariables).toHaveBeenCalledTimes(2);
// no additional call since `main` form values have been cached
await selectBranch('main');
expect(mockCiConfigVariables).toHaveBeenCalledTimes(2);
});
});
describe('When there are no variables in the API cache', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockNoCachedCiConfigVariablesResponse);
createComponentWithApollo({ method: mountExtended });
await waitForPromises();
});
it('stops polling after CONFIG_VARIABLES_TIMEOUT ms have passed', async () => {
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(mockCiConfigVariables).toHaveBeenCalledTimes(3);
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(mockCiConfigVariables).toHaveBeenCalledTimes(3);
});
it('shows loading icon while query polls for updated values', async () => {
expect(findLoadingIcon().exists()).toBe(true);
expect(mockCiConfigVariables).toHaveBeenCalledTimes(1);
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(findLoadingIcon().exists()).toBe(true);
expect(mockCiConfigVariables).toHaveBeenCalledTimes(2);
});
it('hides loading icon and stops polling after query fetches the updated values', async () => {
expect(findLoadingIcon().exists()).toBe(true);
mockCiConfigVariables.mockResolvedValue(mockCiConfigVariablesResponse);
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(findLoadingIcon().exists()).toBe(false);
expect(mockCiConfigVariables).toHaveBeenCalledTimes(2);
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(mockCiConfigVariables).toHaveBeenCalledTimes(2);
});
});
const testBehaviorWhenCacheIsPopulated = (queryResponse) => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(queryResponse);
createComponentWithApollo({ method: mountExtended });
});
it('does not poll for new values', async () => {
await waitForPromises();
expect(mockCiConfigVariables).toHaveBeenCalledTimes(1);
advanceToNextFetch(POLLING_INTERVAL);
await waitForPromises();
expect(mockCiConfigVariables).toHaveBeenCalledTimes(1);
});
it('loading icon is shown when content is requested and hidden when received', async () => {
expect(findLoadingIcon().exists()).toBe(true);
await waitForPromises();
expect(findLoadingIcon().exists()).toBe(false);
});
};
describe('When no variables are defined in the CI configuration and the cache is updated', () => {
testBehaviorWhenCacheIsPopulated(mockEmptyCiConfigVariablesResponse);
it('displays an empty form', async () => {
mockCiConfigVariables.mockResolvedValue(mockEmptyCiConfigVariablesResponse);
createComponentWithApollo({ method: mountExtended });
await waitForPromises();
expect(findKeyInputs().at(0).element.value).toBe('');
expect(findValueInputs().at(0).element.value).toBe('');
expect(findVariableTypes().at(0).props('text')).toBe('Variable');
});
});
describe('When CI configuration has defined variables and they are stored in the cache', () => {
testBehaviorWhenCacheIsPopulated(mockCiConfigVariablesResponse);
describe('with different predefined values', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockCiConfigVariablesResponse);
createComponentWithApollo({ method: mountExtended });
await waitForPromises();
});
it('multi-line strings are added to the value field without removing line breaks', () => {
expect(findValueInputs().at(1).element.value).toBe(mockYamlVariables[1].value);
});
it('multiple predefined values are rendered as a dropdown', () => {
const dropdown = findValueDropdowns().at(0);
const dropdownItems = findValueDropdownItems(dropdown);
const { valueOptions } = mockYamlVariables[2];
expect(dropdownItems.at(0).text()).toBe(valueOptions[0]);
expect(dropdownItems.at(1).text()).toBe(valueOptions[1]);
expect(dropdownItems.at(2).text()).toBe(valueOptions[2]);
});
it('variable with multiple predefined values sets value as the default', () => {
const dropdown = findValueDropdowns().at(0);
const { valueOptions } = mockYamlVariables[2];
expect(dropdown.props('text')).toBe(valueOptions[1]);
});
});
describe('with description', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockCiConfigVariablesResponse);
createComponentWithApollo({ props: mockQueryParams, method: mountExtended });
await waitForPromises();
});
it('displays all the variables', async () => {
expect(findVariableRows()).toHaveLength(6);
});
it('displays a variable from yml', () => {
expect(findKeyInputs().at(0).element.value).toBe(mockYamlVariables[0].key);
expect(findValueInputs().at(0).element.value).toBe(mockYamlVariables[0].value);
});
it('displays a variable from provided query params', () => {
expect(findKeyInputs().at(3).element.value).toBe(
Object.keys(mockQueryParams.variableParams)[0],
);
expect(findValueInputs().at(3).element.value).toBe(
Object.values(mockQueryParams.fileParams)[0],
);
});
it('adds a description to the first variable from yml', () => {
expect(findVariableRows().at(0).text()).toContain(mockYamlVariables[0].description);
});
it('removes the description when a variable key changes', async () => {
findKeyInputs().at(0).element.value = 'yml_var_modified';
findKeyInputs().at(0).trigger('change');
await nextTick();
expect(findVariableRows().at(0).text()).not.toContain(mockYamlVariables[0].description);
});
});
describe('without description', () => {
beforeEach(async () => {
mockCiConfigVariables.mockResolvedValue(mockCiConfigVariablesResponseWithoutDesc);
createComponentWithApollo({ method: mountExtended });
await waitForPromises();
});
it('displays variables with description only', async () => {
expect(findVariableRows()).toHaveLength(2); // extra empty variable is added at the end
});
});
});
describe('Form errors and warnings', () => {
beforeEach(() => {
mockCiConfigVariables.mockResolvedValue(mockEmptyCiConfigVariablesResponse);
createComponentWithApollo();
});
describe('when the refs cannot be loaded', () => {
beforeEach(() => {
mock
.onGet(projectRefsEndpoint, { params: { search: '' } })
.reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
findRefsDropdown().vm.$emit('loadingError');
});
it('shows both an error alert', () => {
expect(findErrorAlert().exists()).toBe(true);
expect(findWarningAlert().exists()).toBe(false);
});
});
describe('when the error response can be handled', () => {
beforeEach(async () => {
mock.onPost(pipelinesPath).reply(HTTP_STATUS_BAD_REQUEST, mockError);
findForm().vm.$emit('submit', dummySubmitEvent);
await 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);
});
it('re-enables the submit button', () => {
expect(findSubmitButton().props('disabled')).toBe(false);
});
it('shows pipeline configuration button for user who can view', () => {
expect(findPipelineConfigButton().exists()).toBe(true);
expect(findPipelineConfigButton().text()).toBe(mockPipelineConfigButtonText);
});
it('does not show pipeline configuration button for user who can not view', () => {
createComponentWithApollo({ props: { canViewPipelineEditor: false } });
expect(findPipelineConfigButton().exists()).toBe(false);
});
it('does not show the credit card validation required alert', () => {
expect(findCCAlert().exists()).toBe(false);
});
describe('when the error response is credit card validation required', () => {
beforeEach(async () => {
mock
.onPost(pipelinesPath)
.reply(HTTP_STATUS_BAD_REQUEST, mockCreditCardValidationRequiredError);
window.gon = {
subscriptions_url: TEST_HOST,
payment_form_url: TEST_HOST,
};
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
});
it('shows credit card validation required alert', () => {
expect(findErrorAlert().exists()).toBe(false);
expect(findCCAlert().exists()).toBe(true);
});
it('clears error and hides the alert on dismiss', async () => {
expect(findCCAlert().exists()).toBe(true);
expect(wrapper.vm.$data.error).toBe(mockCreditCardValidationRequiredError.errors[0]);
findCCAlert().vm.$emit('dismiss');
await nextTick();
expect(findCCAlert().exists()).toBe(false);
expect(wrapper.vm.$data.error).toBe(null);
});
});
});
describe('when the error response cannot be handled', () => {
beforeEach(async () => {
mock.onPost(pipelinesPath).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, 'something went wrong');
findForm().vm.$emit('submit', dummySubmitEvent);
await waitForPromises();
});
it('re-enables the submit button', () => {
expect(findSubmitButton().props('disabled')).toBe(false);
});
});
});
});
|