Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline_editor_tabs_spec.js « components « pipeline_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27707f8b01ae493f9bfad9586cac422fcb3ae0b0 (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
332
333
334
335
336
337
338
339
340
341
342
// TODO

import { GlAlert, GlBadge, GlLoadingIcon, GlTabs } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import Vue, { nextTick } from 'vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import setWindowLocation from 'helpers/set_window_location_helper';
import CiConfigMergedPreview from '~/pipeline_editor/components/editor/ci_config_merged_preview.vue';
import CiValidate from '~/pipeline_editor/components/validate/ci_validate.vue';
import WalkthroughPopover from '~/pipeline_editor/components/popovers/walkthrough_popover.vue';
import PipelineEditorTabs from '~/pipeline_editor/components/pipeline_editor_tabs.vue';
import EditorTab from '~/pipeline_editor/components/ui/editor_tab.vue';
import {
  CREATE_TAB,
  EDITOR_APP_STATUS_EMPTY,
  EDITOR_APP_STATUS_LOADING,
  EDITOR_APP_STATUS_INVALID,
  EDITOR_APP_STATUS_VALID,
  TAB_QUERY_PARAM,
  VALIDATE_TAB,
  VALIDATE_TAB_BADGE_DISMISSED_KEY,
} from '~/pipeline_editor/constants';
import PipelineGraph from '~/pipelines/components/pipeline_graph/pipeline_graph.vue';
import getBlobContent from '~/pipeline_editor/graphql/queries/blob_content.query.graphql';
import {
  mockBlobContentQueryResponse,
  mockCiLintPath,
  mockCiYml,
  mockLintResponse,
  mockLintResponseWithoutMerged,
} from '../mock_data';

Vue.use(VueApollo);

Vue.config.ignoredElements = ['gl-emoji'];

describe('Pipeline editor tabs component', () => {
  let wrapper;
  const MockTextEditor = {
    template: '<div />',
  };

  const createComponent = ({
    listeners = {},
    props = {},
    provide = {},
    appStatus = EDITOR_APP_STATUS_VALID,
    mountFn = shallowMount,
    options = {},
  } = {}) => {
    wrapper = mountFn(PipelineEditorTabs, {
      propsData: {
        ciConfigData: mockLintResponse,
        ciFileContent: mockCiYml,
        currentTab: CREATE_TAB,
        isNewCiConfigFile: true,
        showDrawer: false,
        ...props,
      },
      data() {
        return {
          appStatus,
        };
      },
      provide: {
        ciConfigPath: '/path/to/ci-config',
        ciLintPath: mockCiLintPath,
        currentBranch: 'main',
        projectFullPath: '/path/to/project',
        simulatePipelineHelpPagePath: 'path/to/help/page',
        validateTabIllustrationPath: 'path/to/svg',
        ...provide,
      },
      stubs: {
        TextEditor: MockTextEditor,
        EditorTab,
      },
      listeners,
      ...options,
    });
  };

  let mockBlobContentData;
  let mockApollo;

  const createComponentWithApollo = ({ props, provide = {}, mountFn = shallowMount } = {}) => {
    const handlers = [[getBlobContent, mockBlobContentData]];
    mockApollo = createMockApollo(handlers);

    createComponent({
      props,
      provide,
      mountFn,
      options: {
        apolloProvider: mockApollo,
      },
    });
  };

  const findEditorTab = () => wrapper.find('[data-testid="editor-tab"]');
  const findMergedTab = () => wrapper.find('[data-testid="merged-tab"]');
  const findValidateTab = () => wrapper.find('[data-testid="validate-tab"]');
  const findVisualizationTab = () => wrapper.find('[data-testid="visualization-tab"]');

  const findAlert = () => wrapper.findComponent(GlAlert);
  const findBadge = () => wrapper.findComponent(GlBadge);
  const findCiValidate = () => wrapper.findComponent(CiValidate);
  const findGlTabs = () => wrapper.findComponent(GlTabs);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findPipelineGraph = () => wrapper.findComponent(PipelineGraph);
  const findTextEditor = () => wrapper.findComponent(MockTextEditor);
  const findMergedPreview = () => wrapper.findComponent(CiConfigMergedPreview);
  const findWalkthroughPopover = () => wrapper.findComponent(WalkthroughPopover);

  beforeEach(() => {
    mockBlobContentData = jest.fn();
  });

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

  describe('editor tab', () => {
    it('displays editor only after the tab is mounted', async () => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
      createComponentWithApollo({ mountFn: mount });

      expect(findTextEditor().exists()).toBe(false);

      await nextTick();

      expect(findTextEditor().exists()).toBe(true);
      expect(findEditorTab().exists()).toBe(true);
    });
  });

  describe('visualization tab', () => {
    describe('while loading', () => {
      beforeEach(() => {
        createComponent({ appStatus: EDITOR_APP_STATUS_LOADING });
      });

      it('displays a loading icon if the lint query is loading', () => {
        expect(findLoadingIcon().exists()).toBe(true);
        expect(findPipelineGraph().exists()).toBe(false);
      });
    });
    describe('after loading', () => {
      beforeEach(() => {
        createComponent();
      });

      it('display the tab and visualization', () => {
        expect(findVisualizationTab().exists()).toBe(true);
        expect(findPipelineGraph().exists()).toBe(true);
      });
    });
  });

  describe('validate tab', () => {
    describe('after loading', () => {
      beforeEach(() => {
        createComponent();
      });

      it('displays the tab and the validate component', () => {
        expect(findValidateTab().exists()).toBe(true);
        expect(findCiValidate().exists()).toBe(true);
      });
    });

    describe('NEW badge', () => {
      describe('default', () => {
        beforeEach(() => {
          mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
          createComponentWithApollo({
            mountFn: mount,
            props: {
              currentTab: VALIDATE_TAB,
            },
          });
        });

        it('renders badge by default', () => {
          expect(findBadge().exists()).toBe(true);
          expect(findBadge().text()).toBe(wrapper.vm.$options.i18n.new);
        });

        it('hides badge when moving away from the validate tab', async () => {
          expect(findBadge().exists()).toBe(true);

          await findEditorTab().vm.$emit('click');

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

      describe('if badge has been dismissed before', () => {
        beforeEach(() => {
          localStorage.setItem(VALIDATE_TAB_BADGE_DISMISSED_KEY, 'true');
          mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
          createComponentWithApollo({ mountFn: mount });
        });

        it('does not render badge if it has been dismissed before', () => {
          expect(findBadge().exists()).toBe(false);
        });
      });
    });
  });

  describe('merged tab', () => {
    describe('while loading', () => {
      beforeEach(() => {
        createComponent({ appStatus: EDITOR_APP_STATUS_LOADING });
      });

      it('displays a loading icon if the lint query is loading', () => {
        expect(findLoadingIcon().exists()).toBe(true);
      });
    });

    describe('when there is a fetch error', () => {
      beforeEach(() => {
        createComponent({ props: { ciConfigData: mockLintResponseWithoutMerged } });
      });

      it('show an error message', () => {
        expect(findAlert().exists()).toBe(true);
        expect(findAlert().text()).toBe(wrapper.vm.$options.errorTexts.loadMergedYaml);
      });

      it('does not render the `merged_preview` component', () => {
        expect(findMergedPreview().exists()).toBe(false);
      });
    });

    describe('after loading', () => {
      beforeEach(() => {
        createComponent();
      });

      it('display the tab and the merged preview component', () => {
        expect(findMergedTab().exists()).toBe(true);
        expect(findMergedPreview().exists()).toBe(true);
      });
    });
  });

  describe('show tab content based on status', () => {
    it.each`
      appStatus                    | editor  | viz      | validate | merged
      ${undefined}                 | ${true} | ${true}  | ${true}  | ${true}
      ${EDITOR_APP_STATUS_EMPTY}   | ${true} | ${false} | ${true}  | ${false}
      ${EDITOR_APP_STATUS_INVALID} | ${true} | ${false} | ${true}  | ${true}
      ${EDITOR_APP_STATUS_VALID}   | ${true} | ${true}  | ${true}  | ${true}
    `(
      'when status is $appStatus, we show - editor:$editor | viz:$viz | validate:$validate | merged:$merged',
      ({ appStatus, editor, viz, validate, merged }) => {
        createComponent({ appStatus });

        expect(findTextEditor().exists()).toBe(editor);
        expect(findPipelineGraph().exists()).toBe(viz);
        expect(findValidateTab().exists()).toBe(validate);
        expect(findMergedPreview().exists()).toBe(merged);
      },
    );
  });

  describe('default tab based on url query param', () => {
    const gitlabUrl = 'https://gitlab.test/ci/editor/';
    const matchObject = {
      hostname: 'gitlab.test',
      pathname: '/ci/editor/',
      search: '',
    };

    it(`is ${CREATE_TAB} if the query param ${TAB_QUERY_PARAM} is not present`, () => {
      setWindowLocation(gitlabUrl);
      createComponent();

      expect(window.location).toMatchObject(matchObject);
    });

    it(`is ${CREATE_TAB} tab if the query param ${TAB_QUERY_PARAM} is invalid`, () => {
      const queryValue = 'FOO';
      setWindowLocation(`${gitlabUrl}?${TAB_QUERY_PARAM}=${queryValue}`);
      createComponent();

      // If the query param remains unchanged, then we have ignored it.
      expect(window.location).toMatchObject({
        ...matchObject,
        search: `?${TAB_QUERY_PARAM}=${queryValue}`,
      });
    });
  });

  describe('glTabs', () => {
    beforeEach(() => {
      createComponent();
    });

    it('passes the `sync-active-tab-with-query-params` prop', () => {
      expect(findGlTabs().props('syncActiveTabWithQueryParams')).toBe(true);
    });
  });

  describe('pipeline editor walkthrough', () => {
    describe('when isNewCiConfigFile prop is true (default)', () => {
      beforeEach(() => {
        createComponent();
      });

      it('shows walkthrough popover', async () => {
        expect(findWalkthroughPopover().exists()).toBe(true);
      });
    });

    describe('when isNewCiConfigFile prop is false', () => {
      it('does not show walkthrough popover', async () => {
        createComponent({ props: { isNewCiConfigFile: false } });
        expect(findWalkthroughPopover().exists()).toBe(false);
      });
    });
  });

  it('sets listeners on walkthrough popover', async () => {
    const handler = jest.fn();

    createComponent({
      listeners: {
        event: handler,
      },
    });
    await nextTick();

    findWalkthroughPopover().vm.$emit('event');

    expect(handler).toHaveBeenCalled();
  });
});