summaryrefslogtreecommitdiff
path: root/spec/frontend/analytics/cycle_analytics/value_stream_metrics_spec.js
blob: b96580eeb2d3fe71a26523db4685f60b2ada6f6e (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
import { GlSkeletonLoader } from '@gitlab/ui';
import { nextTick } from 'vue';
import metricsData from 'test_fixtures/projects/analytics/value_stream_analytics/summary.json';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import ValueStreamMetrics from '~/analytics/shared/components/value_stream_metrics.vue';
import { METRIC_TYPE_SUMMARY } from '~/api/analytics_api';
import { VSA_METRICS_GROUPS, METRICS_POPOVER_CONTENT } from '~/analytics/shared/constants';
import { prepareTimeMetricsData } from '~/analytics/shared/utils';
import MetricTile from '~/analytics/shared/components/metric_tile.vue';
import ValueStreamsDashboardLink from '~/analytics/shared/components/value_streams_dashboard_link.vue';
import { createAlert } from '~/flash';
import { group } from './mock_data';

jest.mock('~/flash');

describe('ValueStreamMetrics', () => {
  let wrapper;
  let mockGetValueStreamSummaryMetrics;
  let mockFilterFn;

  const { full_path: requestPath } = group;
  const fakeReqName = 'Mock metrics';
  const metricsRequestFactory = () => ({
    request: mockGetValueStreamSummaryMetrics,
    endpoint: METRIC_TYPE_SUMMARY,
    name: fakeReqName,
  });

  const createComponent = (props = {}) => {
    return shallowMountExtended(ValueStreamMetrics, {
      propsData: {
        requestPath,
        requestParams: {},
        requests: [metricsRequestFactory()],
        ...props,
      },
    });
  };

  const findVSDLink = () => wrapper.findComponent(ValueStreamsDashboardLink);
  const findMetrics = () => wrapper.findAllComponents(MetricTile);
  const findMetricsGroups = () => wrapper.findAllByTestId('vsa-metrics-group');

  const expectToHaveRequest = (fields) => {
    expect(mockGetValueStreamSummaryMetrics).toHaveBeenCalledWith({
      endpoint: METRIC_TYPE_SUMMARY,
      requestPath,
      ...fields,
    });
  };

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

  describe('with successful requests', () => {
    beforeEach(() => {
      mockGetValueStreamSummaryMetrics = jest.fn().mockResolvedValue({ data: metricsData });
    });

    it('will display a loader with pending requests', async () => {
      wrapper = createComponent();
      await nextTick();

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

    describe('with data loaded', () => {
      beforeEach(async () => {
        wrapper = createComponent();
        await waitForPromises();
      });

      it('fetches data from the value stream analytics endpoint', () => {
        expectToHaveRequest({ params: {} });
      });

      describe.each`
        index | identifier                   | value                   | label
        ${0}  | ${metricsData[0].identifier} | ${metricsData[0].value} | ${metricsData[0].title}
        ${1}  | ${metricsData[1].identifier} | ${metricsData[1].value} | ${metricsData[1].title}
        ${2}  | ${metricsData[2].identifier} | ${metricsData[2].value} | ${metricsData[2].title}
        ${3}  | ${metricsData[3].identifier} | ${metricsData[3].value} | ${metricsData[3].title}
      `('metric tiles', ({ identifier, index, value, label }) => {
        it(`renders a metric tile component for "${label}"`, () => {
          const metric = findMetrics().at(index);
          expect(metric.props('metric')).toMatchObject({ identifier, value, label });
          expect(metric.isVisible()).toBe(true);
        });
      });

      it('will not display a loading icon', () => {
        expect(wrapper.findComponent(GlSkeletonLoader).exists()).toBe(false);
      });

      describe('filterFn', () => {
        const transferredMetricsData = prepareTimeMetricsData(metricsData, METRICS_POPOVER_CONTENT);

        it('with a filter function, will call the function with the metrics data', async () => {
          const filteredData = [
            { identifier: 'issues', value: '3', title: 'New Issues', description: 'foo' },
          ];
          mockFilterFn = jest.fn(() => filteredData);

          wrapper = createComponent({
            filterFn: mockFilterFn,
          });

          await waitForPromises();

          expect(mockFilterFn).toHaveBeenCalledWith(transferredMetricsData);
          expect(wrapper.vm.metrics).toEqual(filteredData);
        });

        it('without a filter function, it will only update the metrics', async () => {
          wrapper = createComponent();

          await waitForPromises();

          expect(mockFilterFn).not.toHaveBeenCalled();
          expect(wrapper.vm.metrics).toEqual(transferredMetricsData);
        });
      });

      describe('with additional params', () => {
        beforeEach(async () => {
          wrapper = createComponent({
            requestParams: {
              'project_ids[]': [1],
              created_after: '2020-01-01',
              created_before: '2020-02-01',
            },
          });

          await waitForPromises();
        });

        it('fetches data for the `getValueStreamSummaryMetrics` request', () => {
          expectToHaveRequest({
            params: {
              'project_ids[]': [1],
              created_after: '2020-01-01',
              created_before: '2020-02-01',
            },
          });
        });
      });

      describe('groupBy', () => {
        beforeEach(async () => {
          mockGetValueStreamSummaryMetrics = jest.fn().mockResolvedValue({ data: metricsData });
          wrapper = createComponent({ groupBy: VSA_METRICS_GROUPS });
          await waitForPromises();
        });

        it('renders the metrics as separate groups', () => {
          const groups = findMetricsGroups();
          expect(groups).toHaveLength(VSA_METRICS_GROUPS.length);
        });

        it('renders titles for each group', () => {
          const groups = findMetricsGroups();
          groups.wrappers.forEach((g, index) => {
            const { title } = VSA_METRICS_GROUPS[index];
            expect(g.html()).toContain(title);
          });
        });
      });
    });
  });

  describe('Value Streams Dashboard Link', () => {
    it('will render when a dashboardsPath is set', async () => {
      wrapper = createComponent({ groupBy: VSA_METRICS_GROUPS, dashboardsPath: 'fake-group-path' });
      await waitForPromises();

      const vsdLink = findVSDLink();

      expect(vsdLink.exists()).toBe(true);
      expect(vsdLink.props()).toEqual({ requestPath: 'fake-group-path' });
    });

    it('does not render without a dashboardsPath', async () => {
      wrapper = createComponent({ groupBy: VSA_METRICS_GROUPS });
      await waitForPromises();

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

  describe('with a request failing', () => {
    beforeEach(async () => {
      mockGetValueStreamSummaryMetrics = jest.fn().mockRejectedValue();
      wrapper = createComponent();

      await waitForPromises();
    });

    it('should render an error message', () => {
      expect(createAlert).toHaveBeenCalledWith({
        message: `There was an error while fetching value stream analytics ${fakeReqName} data.`,
      });
    });
  });
});