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

ci_validate_spec.js « validate « components « pipeline_editor « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b66b44e5f06893f9c83805ba39084acab0b73f4e (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
import Vue from 'vue';
import {
  GlAlert,
  GlDisclosureDropdown,
  GlEmptyState,
  GlIcon,
  GlLoadingIcon,
  GlPopover,
} from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import MockAdapter from 'axios-mock-adapter';

import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { resolvers } from '~/ci/pipeline_editor/graphql/resolvers';
import CiLintResults from '~/ci/pipeline_editor/components/lint/ci_lint_results.vue';
import CiValidate, { i18n } from '~/ci/pipeline_editor/components/validate/ci_validate.vue';
import ValidatePipelinePopover from '~/ci/pipeline_editor/components/popovers/validate_pipeline_popover.vue';
import getBlobContent from '~/ci/pipeline_editor/graphql/queries/blob_content.query.graphql';
import { pipelineEditorTrackingOptions } from '~/ci/pipeline_editor/constants';
import {
  mockBlobContentQueryResponse,
  mockCiLintPath,
  mockCiYml,
  mockSimulatePipelineHelpPagePath,
} from '../../mock_data';
import {
  mockLintDataError,
  mockLintDataValid,
  mockLintDataErrorRest,
  mockLintDataValidRest,
} from '../../../ci_lint/mock_data';

let mockAxios;

Vue.use(VueApollo);

const defaultProvide = {
  ciConfigPath: '/path/to/ci-config',
  ciLintPath: mockCiLintPath,
  currentBranch: 'main',
  projectFullPath: '/path/to/project',
  validateTabIllustrationPath: '/path/to/img',
  simulatePipelineHelpPagePath: mockSimulatePipelineHelpPagePath,
};

describe('Pipeline Editor Validate Tab', () => {
  let wrapper;
  let mockBlobContentData;
  let trackingSpy;

  const createComponent = ({ props, stubs } = {}) => {
    const handlers = [[getBlobContent, mockBlobContentData]];
    const mockApollo = createMockApollo(handlers, resolvers);

    wrapper = shallowMountExtended(CiValidate, {
      propsData: {
        ciFileContent: mockCiYml,
        ...props,
      },
      stubs,
      provide: {
        ...defaultProvide,
      },
      apolloProvider: mockApollo,
    });
  };

  const findAlert = () => wrapper.findComponent(GlAlert);
  const findCancelBtn = () => wrapper.findByTestId('cancel-simulation');
  const findContentChangeStatus = () => wrapper.findByTestId('content-status');
  const findCta = () => wrapper.findByTestId('simulate-pipeline-button');
  const findDisabledCtaTooltip = () => wrapper.findByTestId('cta-tooltip');
  const findHelpIcon = () => wrapper.findComponent(GlIcon);
  const findEmptyState = () => wrapper.findComponent(GlEmptyState);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findPipelineSource = () => wrapper.findComponent(GlDisclosureDropdown);
  const findPopover = () => wrapper.findComponent(GlPopover);
  const findCiLintResults = () => wrapper.findComponent(CiLintResults);
  const findResultsCta = () => wrapper.findByTestId('resimulate-pipeline-button');

  beforeEach(() => {
    mockAxios = new MockAdapter(axios);
    mockAxios.onPost(defaultProvide.ciLintPath).reply(HTTP_STATUS_OK, mockLintDataValidRest);

    mockBlobContentData = jest.fn();
  });

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

  describe('while initial CI content is loading', () => {
    beforeEach(() => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);

      createComponent();
    });

    it('renders disabled CTA with tooltip', () => {
      expect(findCta().props('disabled')).toBe(true);
      expect(findDisabledCtaTooltip().exists()).toBe(true);
    });
  });

  describe('after initial CI content is loaded', () => {
    beforeEach(async () => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
      await createComponent({ stubs: { GlPopover, ValidatePipelinePopover } });
    });

    it('renders disabled pipeline source dropdown', () => {
      expect(findPipelineSource().exists()).toBe(true);
      expect(findPipelineSource().attributes('toggletext')).toBe(i18n.pipelineSourceDefault);
      expect(findPipelineSource().props('disabled')).toBe(true);
    });

    it('renders enabled CTA without tooltip', () => {
      expect(findCta().exists()).toBe(true);
      expect(findCta().props('disabled')).toBe(false);
      expect(findDisabledCtaTooltip().exists()).toBe(false);
    });

    it('popover is set to render when hovering over help icon', () => {
      expect(findPopover().props('target')).toBe(findHelpIcon().attributes('id'));
      expect(findPopover().props('triggers')).toBe('hover focus');
    });
  });

  describe('simulating the pipeline', () => {
    beforeEach(async () => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
      await createComponent();

      trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
    });

    afterEach(() => {
      unmockTracking();
    });

    it('tracks the simulation event', () => {
      const {
        label,
        actions: { simulatePipeline },
      } = pipelineEditorTrackingOptions;
      findCta().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, simulatePipeline, { label });
    });

    it('renders loading state while simulation is ongoing', async () => {
      await findCta().vm.$emit('click');

      expect(findLoadingIcon().exists()).toBe(true);
      expect(findCancelBtn().exists()).toBe(true);
      expect(findCta().props('loading')).toBe(true);
    });

    it('calls endpoint with the correct input', async () => {
      findCta().vm.$emit('click');

      await waitForPromises();

      expect(mockAxios.history.post).toHaveLength(1);
      expect(mockAxios.history.post[0].data).toBe(
        JSON.stringify({
          content: mockCiYml,
          dry_run: true,
        }),
      );
    });

    describe('when results are successful', () => {
      beforeEach(async () => {
        findCta().vm.$emit('click');

        await waitForPromises();
      });

      it('renders success alert', () => {
        expect(findAlert().exists()).toBe(true);
        expect(findAlert().attributes('variant')).toBe('success');
        expect(findAlert().attributes('title')).toBe(i18n.successAlertTitle);
      });

      it('does not render content change status or CTA for results page', () => {
        expect(findContentChangeStatus().exists()).toBe(false);
        expect(findResultsCta().exists()).toBe(false);
      });

      it('renders CI lint results with correct props', () => {
        expect(findCiLintResults().exists()).toBe(true);
        expect(findCiLintResults().props()).toMatchObject({
          dryRun: true,
          hideAlert: true,
          isValid: true,
          jobs: mockLintDataValid.data.lintCI.jobs,
        });
      });
    });

    describe('when results have errors', () => {
      beforeEach(async () => {
        mockAxios.onPost(defaultProvide.ciLintPath).reply(HTTP_STATUS_OK, mockLintDataErrorRest);
        findCta().vm.$emit('click');

        await waitForPromises();
      });

      it('renders error alert', () => {
        expect(findAlert().exists()).toBe(true);
        expect(findAlert().attributes('variant')).toBe('danger');
        expect(findAlert().attributes('title')).toBe(i18n.errorAlertTitle);
      });

      it('renders CI lint results with correct props', () => {
        expect(findCiLintResults().exists()).toBe(true);
        expect(findCiLintResults().props()).toMatchObject({
          dryRun: true,
          hideAlert: true,
          isValid: false,
          errors: mockLintDataError.data.lintCI.errors,
          warnings: mockLintDataError.data.lintCI.warnings,
        });
      });
    });
  });

  describe('when CI content has changed after a simulation', () => {
    beforeEach(async () => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
      await createComponent();

      trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);
      findCta().vm.$emit('click');
      await waitForPromises();
    });

    afterEach(() => {
      unmockTracking();
    });

    it('tracks the second simulation event', async () => {
      const {
        label,
        actions: { resimulatePipeline },
      } = pipelineEditorTrackingOptions;

      await wrapper.setProps({ ciFileContent: 'new yaml content' });
      findResultsCta().vm.$emit('click');

      expect(trackingSpy).toHaveBeenCalledWith(undefined, resimulatePipeline, { label });
    });

    it('renders content change status', async () => {
      await wrapper.setProps({ ciFileContent: 'new yaml content' });

      expect(findContentChangeStatus().exists()).toBe(true);
      expect(findResultsCta().exists()).toBe(true);
    });

    it('calls mutation with new content', async () => {
      const newContent = 'new yaml content';
      await wrapper.setProps({ ciFileContent: newContent });
      findResultsCta().vm.$emit('click');

      await waitForPromises();

      expect(mockAxios.history.post).toHaveLength(2);
      expect(mockAxios.history.post[1].data).toBe(
        JSON.stringify({
          content: newContent,
          dry_run: true,
        }),
      );
    });
  });

  describe('canceling a simulation', () => {
    beforeEach(async () => {
      mockBlobContentData.mockResolvedValue(mockBlobContentQueryResponse);
      await createComponent();
    });

    it('returns to init state', async () => {
      // init state
      expect(findEmptyState().exists()).toBe(true);
      expect(findCiLintResults().exists()).toBe(false);

      // mutations should have successful results
      await findCta().vm.$emit('click');

      // cancel before simulation succeeds
      expect(findCancelBtn().exists()).toBe(true);
      await findCancelBtn().vm.$emit('click');

      // should still render init state
      expect(findEmptyState().exists()).toBe(true);
      expect(findCiLintResults().exists()).toBe(false);
    });
  });
});