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

app_spec.js « components « charts « pipelines « projects « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8876349c5e3f5b61be8b6d1fdb349be96a13b7a (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
import { GlTabs, GlTab } from '@gitlab/ui';
import { merge } from 'lodash';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import setWindowLocation from 'helpers/set_window_location_helper';
import { TEST_HOST } from 'helpers/test_constants';
import { mergeUrlParams, updateHistory, getParameterValues } from '~/lib/utils/url_utility';
import Component from '~/projects/pipelines/charts/components/app.vue';
import PipelineCharts from '~/projects/pipelines/charts/components/pipeline_charts.vue';
import API from '~/api';
import { mockTracking } from 'helpers/tracking_helper';
import {
  SNOWPLOW_DATA_SOURCE,
  SNOWPLOW_LABEL,
  SNOWPLOW_SCHEMA,
} from '~/projects/pipelines/charts/constants';

jest.mock('~/lib/utils/url_utility');

const DeploymentFrequencyChartsStub = { name: 'DeploymentFrequencyCharts', render: () => {} };
const LeadTimeChartsStub = { name: 'LeadTimeCharts', render: () => {} };
const TimeToRestoreServiceChartsStub = { name: 'TimeToRestoreServiceCharts', render: () => {} };
const ChangeFailureRateChartsStub = { name: 'ChangeFailureRateCharts', render: () => {} };
const ProjectQualitySummaryStub = { name: 'ProjectQualitySummary', render: () => {} };

describe('ProjectsPipelinesChartsApp', () => {
  let wrapper;

  function createComponent(mountOptions = {}) {
    wrapper = shallowMountExtended(
      Component,
      merge(
        {},
        {
          provide: {
            shouldRenderDoraCharts: true,
            shouldRenderQualitySummary: true,
          },
          stubs: {
            DeploymentFrequencyCharts: DeploymentFrequencyChartsStub,
            LeadTimeCharts: LeadTimeChartsStub,
            TimeToRestoreServiceCharts: TimeToRestoreServiceChartsStub,
            ChangeFailureRateCharts: ChangeFailureRateChartsStub,
            ProjectQualitySummary: ProjectQualitySummaryStub,
          },
        },
        mountOptions,
      ),
    );
  }

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

  const findGlTabs = () => wrapper.findComponent(GlTabs);
  const findAllGlTabs = () => wrapper.findAllComponents(GlTab);
  const findGlTabAtIndex = (index) => findAllGlTabs().at(index);
  const findLeadTimeCharts = () => wrapper.findComponent(LeadTimeChartsStub);
  const findTimeToRestoreServiceCharts = () =>
    wrapper.findComponent(TimeToRestoreServiceChartsStub);
  const findChangeFailureRateCharts = () => wrapper.findComponent(ChangeFailureRateChartsStub);
  const findDeploymentFrequencyCharts = () => wrapper.findComponent(DeploymentFrequencyChartsStub);
  const findPipelineCharts = () => wrapper.findComponent(PipelineCharts);
  const findProjectQualitySummary = () => wrapper.findComponent(ProjectQualitySummaryStub);

  describe('when all charts are available', () => {
    beforeEach(() => {
      createComponent();
    });

    describe.each`
      title                        | finderFn                          | index
      ${'Pipelines'}               | ${findPipelineCharts}             | ${0}
      ${'Deployment frequency'}    | ${findDeploymentFrequencyCharts}  | ${1}
      ${'Lead time'}               | ${findLeadTimeCharts}             | ${2}
      ${'Time to restore service'} | ${findTimeToRestoreServiceCharts} | ${3}
      ${'Change failure rate'}     | ${findChangeFailureRateCharts}    | ${4}
      ${'Project quality'}         | ${findProjectQualitySummary}      | ${5}
    `('Tabs', ({ title, finderFn, index }) => {
      it(`renders tab with a title ${title} at index ${index}`, () => {
        expect(findGlTabAtIndex(index).attributes('title')).toBe(title);
      });

      it(`renders the ${title} chart`, () => {
        expect(finderFn().exists()).toBe(true);
      });

      it(`updates the current tab and url when the ${title} tab is clicked`, async () => {
        let chartsPath;
        const tabName = title.toLowerCase().replace(/\s/g, '-');

        setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts`);

        mergeUrlParams.mockImplementation(({ chart }, path) => {
          expect(chart).toBe(tabName);
          expect(path).toBe(window.location.pathname);
          chartsPath = `${path}?chart=${chart}`;
          return chartsPath;
        });

        updateHistory.mockImplementation(({ url }) => {
          expect(url).toBe(chartsPath);
        });
        const tabs = findGlTabs();

        expect(tabs.attributes('value')).toBe('0');

        tabs.vm.$emit('input', index);

        await nextTick();

        expect(tabs.attributes('value')).toBe(index.toString());
      });
    });

    it('should not try to push history if the tab does not change', async () => {
      setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts`);

      mergeUrlParams.mockImplementation(({ chart }, path) => `${path}?chart=${chart}`);

      const tabs = findGlTabs();

      expect(tabs.attributes('value')).toBe('0');

      tabs.vm.$emit('input', 0);

      await nextTick();

      expect(updateHistory).not.toHaveBeenCalled();
    });

    describe('event tracking', () => {
      describe('RedisHLL events', () => {
        it.each`
          testId                           | event
          ${'pipelines-tab'}               | ${'p_analytics_ci_cd_pipelines'}
          ${'deployment-frequency-tab'}    | ${'p_analytics_ci_cd_deployment_frequency'}
          ${'lead-time-tab'}               | ${'p_analytics_ci_cd_lead_time'}
          ${'time-to-restore-service-tab'} | ${'p_analytics_ci_cd_time_to_restore_service'}
          ${'change-failure-rate-tab'}     | ${'p_analytics_ci_cd_change_failure_rate'}
        `('tracks the $event event when clicked', ({ testId, event }) => {
          const trackApiSpy = jest.spyOn(API, 'trackRedisHllUserEvent');

          expect(trackApiSpy).not.toHaveBeenCalled();

          wrapper.findByTestId(testId).vm.$emit('click');

          expect(trackApiSpy).toHaveBeenCalledWith(event);
        });
      });

      describe('Snowplow events', () => {
        it.each`
          testId                        | event
          ${'pipelines-tab'}            | ${'p_analytics_ci_cd_pipelines'}
          ${'deployment-frequency-tab'} | ${'p_analytics_ci_cd_deployment_frequency'}
          ${'lead-time-tab'}            | ${'p_analytics_ci_cd_lead_time'}
        `('tracks the $event event when clicked', ({ testId, event }) => {
          const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);

          wrapper.findByTestId(testId).vm.$emit('click');

          expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_tab', {
            label: SNOWPLOW_LABEL,
            context: {
              schema: SNOWPLOW_SCHEMA,
              data: {
                event_name: event,
                data_source: SNOWPLOW_DATA_SOURCE,
              },
            },
          });
        });

        it.each`
          tab
          ${'time-to-restore-service-tab'}
          ${'change-failure-rate-tab'}
        `('does not track when tab $tab is clicked', ({ tab }) => {
          const trackingSpy = mockTracking(undefined, wrapper.element, jest.spyOn);

          wrapper.findByTestId(tab).vm.$emit('click');

          expect(trackingSpy).not.toHaveBeenCalled();
        });
      });
    });
  });

  describe('when provided with a query param', () => {
    it.each`
      chart                        | tab
      ${'change-failure-rate'}     | ${'4'}
      ${'time-to-restore-service'} | ${'3'}
      ${'lead-time'}               | ${'2'}
      ${'deployment-frequency'}    | ${'1'}
      ${'pipelines'}               | ${'0'}
      ${'fake'}                    | ${'0'}
      ${''}                        | ${'0'}
    `('shows the correct tab for URL parameter "$chart"', ({ chart, tab }) => {
      setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts?chart=${chart}`);
      getParameterValues.mockImplementation((name) => {
        expect(name).toBe('chart');
        return chart ? [chart] : [];
      });
      createComponent();
      expect(findGlTabs().attributes('value')).toBe(tab);
    });

    it('should set the tab when the back button is clicked', async () => {
      let popstateHandler;

      window.addEventListener = jest.fn();

      window.addEventListener.mockImplementation((event, handler) => {
        if (event === 'popstate') {
          popstateHandler = handler;
        }
      });

      getParameterValues.mockImplementation((name) => {
        expect(name).toBe('chart');
        return [];
      });

      createComponent();

      expect(findGlTabs().attributes('value')).toBe('0');

      getParameterValues.mockImplementationOnce((name) => {
        expect(name).toBe('chart');
        return ['deployment-frequency'];
      });

      popstateHandler();

      await nextTick();

      expect(findGlTabs().attributes('value')).toBe('1');
    });
  });

  describe('when the dora charts are not available and project quality summary is not available', () => {
    beforeEach(() => {
      createComponent({
        provide: { shouldRenderDoraCharts: false, shouldRenderQualitySummary: false },
      });
    });

    it('does not render tabs', () => {
      expect(findGlTabs().exists()).toBe(false);
    });

    it('renders the pipeline charts', () => {
      expect(findPipelineCharts().exists()).toBe(true);
    });
  });

  describe('when the project quality summary is not available', () => {
    beforeEach(() => {
      createComponent({ provide: { shouldRenderQualitySummary: false } });
    });

    it('does not render the tab', () => {
      expect(findProjectQualitySummary().exists()).toBe(false);
    });
  });
});