summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_wizard/components/wrapper_spec.js
blob: 357a9d21723d217b5756bb40dddba43a27b17e3c (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
import { Document, parseDocument } from 'yaml';
import { GlProgressBar } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended, mountExtended } from 'helpers/vue_test_utils_helper';
import PipelineWizardWrapper, { i18n } from '~/pipeline_wizard/components/wrapper.vue';
import WizardStep from '~/pipeline_wizard/components/step.vue';
import CommitStep from '~/pipeline_wizard/components/commit.vue';
import YamlEditor from '~/pipeline_wizard/components/editor.vue';
import { sprintf } from '~/locale';
import {
  steps as stepsYaml,
  compiledScenario1,
  compiledScenario2,
  compiledScenario3,
} from '../mock/yaml';

describe('Pipeline Wizard - wrapper.vue', () => {
  let wrapper;
  const steps = parseDocument(stepsYaml).toJS();

  const getAsYamlNode = (value) => new Document(value).contents;
  const createComponent = (props = {}, mountFn = shallowMountExtended) => {
    wrapper = mountFn(PipelineWizardWrapper, {
      propsData: {
        projectPath: '/user/repo',
        defaultBranch: 'main',
        filename: '.gitlab-ci.yml',
        steps: getAsYamlNode(steps),
        ...props,
      },
      stubs: {
        CommitStep: true,
      },
    });
  };
  const getEditorContent = () => {
    return wrapper.getComponent(YamlEditor).props().doc.toString();
  };
  const getStepWrapper = () =>
    wrapper.findAllComponents(WizardStep).wrappers.find((w) => w.isVisible());
  const getGlProgressBarWrapper = () => wrapper.getComponent(GlProgressBar);
  const findFirstVisibleStep = () =>
    wrapper.findAllComponents('[data-testid="step"]').wrappers.find((w) => w.isVisible());
  const findFirstInputFieldForTarget = (target) =>
    wrapper.find(`[data-input-target="${target}"]`).find('input');

  describe('display', () => {
    afterEach(() => {
      wrapper.destroy();
    });

    it('shows the steps', () => {
      createComponent();

      expect(getStepWrapper().exists()).toBe(true);
    });

    it('shows the progress bar', () => {
      createComponent();

      const expectedMessage = sprintf(i18n.stepNofN, {
        currentStep: 1,
        stepCount: 3,
      });

      expect(wrapper.findByTestId('step-count').text()).toBe(expectedMessage);
      expect(getGlProgressBarWrapper().exists()).toBe(true);
    });

    it('shows the editor', () => {
      createComponent();

      expect(wrapper.findComponent(YamlEditor).exists()).toBe(true);
    });

    it('shows the editor header with the default filename', () => {
      createComponent();

      const expectedMessage = sprintf(i18n.draft, {
        filename: '.gitlab-ci.yml',
      });

      expect(wrapper.findByTestId('editor-header').text()).toBe(expectedMessage);
    });

    it('shows the editor header with a custom filename', async () => {
      const filename = 'my-file.yml';
      createComponent({
        filename,
      });

      const expectedMessage = sprintf(i18n.draft, {
        filename,
      });

      expect(wrapper.findByTestId('editor-header').text()).toBe(expectedMessage);
    });
  });

  describe('steps', () => {
    const totalSteps = steps.length + 1;

    // **Note** on `expectProgressBarValue`
    // Why are we expecting 50% here and not 66% or even 100%?
    // The reason is mostly a UX thing.
    // First, we count the commit step as an extra step, so that would
    // be 66% by now (2 of 3).
    // But then we add yet another one to the calc, because when we
    // arrived on the second step's page, it's not *completed* (which is
    // what the progress bar indicates). So in that case we're at 33%.
    // Lastly, we want to start out with the progress bar not at zero,
    // because UX research indicates that makes a process like this less
    // intimidating, so we're always adding one step to the value bar
    // (but not to the step counter. Now we're back at 50%.
    describe.each`
      step                                       | navigationEventChain                | expectStepNumber | expectCommitStepShown | expectStepDef | expectProgressBarValue
      ${'initial step'}                          | ${[]}                               | ${1}             | ${false}              | ${steps[0]}   | ${25}
      ${'second step'}                           | ${['next']}                         | ${2}             | ${false}              | ${steps[1]}   | ${50}
      ${'commit step'}                           | ${['next', 'next']}                 | ${3}             | ${true}               | ${null}       | ${75}
      ${'stepping back'}                         | ${['next', 'back']}                 | ${1}             | ${false}              | ${steps[0]}   | ${25}
      ${'clicking next>next>back'}               | ${['next', 'next', 'back']}         | ${2}             | ${false}              | ${steps[1]}   | ${50}
      ${'clicking all the way through and back'} | ${['next', 'next', 'back', 'back']} | ${1}             | ${false}              | ${steps[0]}   | ${25}
    `(
      '$step',
      ({
        navigationEventChain,
        expectStepNumber,
        expectCommitStepShown,
        expectStepDef,
        expectProgressBarValue,
      }) => {
        beforeAll(async () => {
          createComponent();

          for (const emittedValue of navigationEventChain) {
            findFirstVisibleStep().vm.$emit(emittedValue);
            // We have to wait for the next step to be mounted
            // before we can emit the next event, so we have to await
            // inside the loop.
            // eslint-disable-next-line no-await-in-loop
            await nextTick();
          }
        });

        afterAll(() => {
          wrapper.destroy();
        });

        if (expectCommitStepShown) {
          it('does not show the step wrapper', async () => {
            expect(wrapper.findComponent(WizardStep).isVisible()).toBe(false);
          });

          it('shows the commit step page', () => {
            expect(wrapper.findComponent(CommitStep).isVisible()).toBe(true);
          });
        } else {
          it('passes the correct step config to the step component', async () => {
            expect(getStepWrapper().props('inputs')).toMatchObject(expectStepDef.inputs);
          });

          it('does not show the commit step page', () => {
            expect(wrapper.findComponent(CommitStep).isVisible()).toBe(false);
          });
        }

        it('updates the progress bar', () => {
          expect(getGlProgressBarWrapper().attributes('value')).toBe(`${expectProgressBarValue}`);
        });

        it('updates the step number', () => {
          const expectedMessage = sprintf(i18n.stepNofN, {
            currentStep: expectStepNumber,
            stepCount: totalSteps,
          });

          expect(wrapper.findByTestId('step-count').text()).toBe(expectedMessage);
        });
      },
    );
  });

  describe('editor overlay', () => {
    beforeAll(() => {
      createComponent();
    });

    afterAll(() => {
      wrapper.destroy();
    });

    it('initially shows a placeholder', async () => {
      const editorContent = getEditorContent();

      await nextTick();

      expect(editorContent).toBe('foo: $FOO\nbar: $BAR\n');
    });

    it('shows an overlay with help text after setup', () => {
      expect(wrapper.findByTestId('placeholder-overlay').exists()).toBe(true);
      expect(wrapper.findByTestId('filename').text()).toBe('.gitlab-ci.yml');
      expect(wrapper.findByTestId('description').text()).toBe(i18n.overlayMessage);
    });

    it('does not show overlay when content has changed', async () => {
      const newCompiledDoc = new Document({ faa: 'bur' });

      await getStepWrapper().vm.$emit('update:compiled', newCompiledDoc);
      await nextTick();

      const overlay = wrapper.findByTestId('placeholder-overlay');

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

  describe('editor updates', () => {
    beforeAll(() => {
      createComponent();
    });

    afterAll(() => {
      wrapper.destroy();
    });

    it('editor reflects changes', async () => {
      const newCompiledDoc = new Document({ faa: 'bur' });
      await getStepWrapper().vm.$emit('update:compiled', newCompiledDoc);

      expect(getEditorContent()).toBe(newCompiledDoc.toString());
    });
  });

  describe('line highlights', () => {
    beforeAll(() => {
      createComponent();
    });

    afterAll(() => {
      wrapper.destroy();
    });

    it('highlight requests by the step get passed on to the editor', async () => {
      const highlight = 'foo';

      await getStepWrapper().vm.$emit('update:highlight', highlight);

      expect(wrapper.getComponent(YamlEditor).props('highlight')).toBe(highlight);
    });

    it('removes the highlight when clicking through to the commit step', async () => {
      // Simulate clicking through all steps until the last one
      await Promise.all(
        steps.map(async () => {
          await getStepWrapper().vm.$emit('next');
          await nextTick();
        }),
      );

      expect(wrapper.getComponent(YamlEditor).props('highlight')).toBe(null);
    });
  });

  describe('integration test', () => {
    beforeAll(async () => {
      createComponent({}, mountExtended);
    });

    it('updates the editor content after input on step 1', async () => {
      findFirstInputFieldForTarget('$FOO').setValue('fooVal');
      await nextTick();

      expect(getEditorContent()).toBe(compiledScenario1);
    });

    it('updates the editor content after input on step 2', async () => {
      findFirstVisibleStep().vm.$emit('next');
      await nextTick();

      findFirstInputFieldForTarget('$BAR').setValue('barVal');
      await nextTick();

      expect(getEditorContent()).toBe(compiledScenario2);
    });

    describe('navigating back', () => {
      let inputField;

      beforeAll(async () => {
        findFirstVisibleStep().vm.$emit('back');
        await nextTick();

        inputField = findFirstInputFieldForTarget('$FOO');
      });

      afterAll(() => {
        wrapper.destroy();
        inputField = undefined;
      });

      it('still shows the input values from the former visit', () => {
        expect(inputField.element.value).toBe('fooVal');
      });

      it('updates the editor content without modifying input that came from a later step', async () => {
        inputField.setValue('newFooVal');
        await nextTick();

        expect(getEditorContent()).toBe(compiledScenario3);
      });
    });
  });
});