summaryrefslogtreecommitdiff
path: root/spec/frontend/analytics/instance_statistics/components/pipelines_chart_spec.js
blob: a06d66f783e2c859eb5e4c6d0bb5ecbe80c0b715 (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
import { createLocalVue, shallowMount } from '@vue/test-utils';
import { GlLineChart } from '@gitlab/ui/dist/charts';
import { GlAlert } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import createMockApollo from 'jest/helpers/mock_apollo_helper';
import PipelinesChart from '~/analytics/instance_statistics/components/pipelines_chart.vue';
import pipelinesStatsQuery from '~/analytics/instance_statistics/graphql/queries/pipeline_stats.query.graphql';
import ChartSkeletonLoader from '~/vue_shared/components/resizable_chart/skeleton_loader.vue';
import { mockCountsData1, mockCountsData2 } from '../mock_data';
import { getApolloResponse } from '../apollo_mock_data';

const localVue = createLocalVue();
localVue.use(VueApollo);

describe('PipelinesChart', () => {
  let wrapper;
  let queryHandler;

  const createApolloProvider = pipelineStatsHandler => {
    return createMockApollo([[pipelinesStatsQuery, pipelineStatsHandler]]);
  };

  const createComponent = apolloProvider => {
    return shallowMount(PipelinesChart, {
      localVue,
      apolloProvider,
    });
  };

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

  const findLoader = () => wrapper.find(ChartSkeletonLoader);
  const findChart = () => wrapper.find(GlLineChart);
  const findAlert = () => wrapper.find(GlAlert);

  describe('while loading', () => {
    beforeEach(() => {
      queryHandler = jest.fn().mockReturnValue(new Promise(() => {}));
      const apolloProvider = createApolloProvider(queryHandler);
      wrapper = createComponent(apolloProvider);
    });

    it('requests data', () => {
      expect(queryHandler).toBeCalledTimes(1);
    });

    it('displays the skeleton loader', () => {
      expect(findLoader().exists()).toBe(true);
    });

    it('hides the chart', () => {
      expect(findChart().exists()).toBe(false);
    });

    it('does not show an error', () => {
      expect(findAlert().exists()).toBe(false);
    });
  });

  describe('without data', () => {
    beforeEach(() => {
      const emptyResponse = getApolloResponse();
      queryHandler = jest.fn().mockResolvedValue(emptyResponse);
      const apolloProvider = createApolloProvider(queryHandler);
      wrapper = createComponent(apolloProvider);
    });

    it('renders an no data message', () => {
      expect(findAlert().text()).toBe('There is no data available.');
    });

    it('hides the skeleton loader', () => {
      expect(findLoader().exists()).toBe(false);
    });

    it('renders the chart', () => {
      expect(findChart().exists()).toBe(false);
    });
  });

  describe('with data', () => {
    beforeEach(() => {
      const response = getApolloResponse({
        pipelinesTotal: mockCountsData1,
        pipelinesSucceeded: mockCountsData2,
        pipelinesFailed: mockCountsData2,
        pipelinesCanceled: mockCountsData1,
        pipelinesSkipped: mockCountsData1,
      });
      queryHandler = jest.fn().mockResolvedValue(response);
      const apolloProvider = createApolloProvider(queryHandler);
      wrapper = createComponent(apolloProvider);
    });

    it('requests data', () => {
      expect(queryHandler).toBeCalledTimes(1);
    });

    it('hides the skeleton loader', () => {
      expect(findLoader().exists()).toBe(false);
    });

    it('renders the chart', () => {
      expect(findChart().exists()).toBe(true);
    });

    it('passes the data to the line chart', () => {
      expect(findChart().props('data')).toMatchSnapshot();
    });

    it('does not show an error', () => {
      expect(findAlert().exists()).toBe(false);
    });
  });

  describe('when fetching more data', () => {
    const recordedAt = '2020-08-01';
    describe('when the fetchMore query returns data', () => {
      beforeEach(async () => {
        const newData = { recordedAt, count: 5 };
        const firstResponse = getApolloResponse({
          pipelinesTotal: mockCountsData2,
          pipelinesSucceeded: mockCountsData2,
          pipelinesFailed: mockCountsData1,
          pipelinesCanceled: mockCountsData2,
          pipelinesSkipped: mockCountsData2,
          hasNextPage: true,
        });
        const secondResponse = getApolloResponse({
          pipelinesTotal: [newData],
          pipelinesSucceeded: [newData],
          pipelinesFailed: [newData],
          pipelinesCanceled: [newData],
          pipelinesSkipped: [newData],
          hasNextPage: false,
        });
        queryHandler = jest
          .fn()
          .mockResolvedValueOnce(firstResponse)
          .mockResolvedValueOnce(secondResponse);
        const apolloProvider = createApolloProvider(queryHandler);
        wrapper = createComponent(apolloProvider);

        await wrapper.vm.$nextTick();
      });

      it('requests data twice', () => {
        expect(queryHandler).toBeCalledTimes(2);
      });

      it('passes the data to the line chart', () => {
        expect(findChart().props('data')).toMatchSnapshot();
      });
    });

    describe('when the fetchMore query throws an error', () => {
      beforeEach(async () => {
        const response = getApolloResponse({
          pipelinesTotal: mockCountsData2,
          pipelinesSucceeded: mockCountsData2,
          pipelinesFailed: mockCountsData1,
          pipelinesCanceled: mockCountsData2,
          pipelinesSkipped: mockCountsData2,
          hasNextPage: true,
        });
        queryHandler = jest.fn().mockResolvedValue(response);
        const apolloProvider = createApolloProvider(queryHandler);
        wrapper = createComponent(apolloProvider);
        jest
          .spyOn(wrapper.vm.$apollo.queries.pipelineStats, 'fetchMore')
          .mockImplementation(jest.fn().mockRejectedValue());
        await wrapper.vm.$nextTick();
      });

      it('calls fetchMore', () => {
        expect(wrapper.vm.$apollo.queries.pipelineStats.fetchMore).toHaveBeenCalledTimes(1);
      });

      it('show an error message', () => {
        expect(findAlert().text()).toBe(
          'Could not load the pipelines chart. Please refresh the page to try again.',
        );
      });
    });
  });
});