summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/charts/gauge_spec.js
blob: 484199698eaea61dc35f4a4286648f4a3c160cbc (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
import { GlGaugeChart } from '@gitlab/ui/dist/charts';
import { shallowMount } from '@vue/test-utils';
import GaugeChart from '~/monitoring/components/charts/gauge.vue';
import { gaugeChartGraphData } from '../../graph_data';

describe('Gauge Chart component', () => {
  const defaultGraphData = gaugeChartGraphData();

  let wrapper;

  const findGaugeChart = () => wrapper.findComponent(GlGaugeChart);

  const createWrapper = ({ ...graphProps } = {}) => {
    wrapper = shallowMount(GaugeChart, {
      propsData: {
        graphData: {
          ...defaultGraphData,
          ...graphProps,
        },
      },
    });
  };

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

  describe('chart component', () => {
    it('is rendered when props are passed', () => {
      createWrapper();

      expect(findGaugeChart().exists()).toBe(true);
    });
  });

  describe('min and max', () => {
    const MIN_DEFAULT = 0;
    const MAX_DEFAULT = 100;

    it('are passed to chart component', () => {
      createWrapper();

      expect(findGaugeChart().props('min')).toBe(100);
      expect(findGaugeChart().props('max')).toBe(1000);
    });

    const invalidCases = [undefined, NaN, 'a string'];

    it.each(invalidCases)(
      'if min has invalid value, defaults are used for both min and max',
      (invalidValue) => {
        createWrapper({ minValue: invalidValue });

        expect(findGaugeChart().props('min')).toBe(MIN_DEFAULT);
        expect(findGaugeChart().props('max')).toBe(MAX_DEFAULT);
      },
    );

    it.each(invalidCases)(
      'if max has invalid value, defaults are used for both min and max',
      (invalidValue) => {
        createWrapper({ minValue: invalidValue });

        expect(findGaugeChart().props('min')).toBe(MIN_DEFAULT);
        expect(findGaugeChart().props('max')).toBe(MAX_DEFAULT);
      },
    );

    it('if min is bigger than max, defaults are used for both min and max', () => {
      createWrapper({ minValue: 100, maxValue: 0 });

      expect(findGaugeChart().props('min')).toBe(MIN_DEFAULT);
      expect(findGaugeChart().props('max')).toBe(MAX_DEFAULT);
    });
  });

  describe('thresholds', () => {
    it('thresholds are set on chart', () => {
      createWrapper();

      expect(findGaugeChart().props('thresholds')).toEqual([500, 800]);
    });

    it('when no thresholds are defined, a default threshold is defined at 95% of max_value', () => {
      createWrapper({
        minValue: 0,
        maxValue: 100,
        thresholds: {},
      });

      expect(findGaugeChart().props('thresholds')).toEqual([95]);
    });

    it('when out of min-max bounds thresholds are defined, a default threshold is defined at 95% of the range between min_value and max_value', () => {
      createWrapper({
        thresholds: {
          values: [-10, 1500],
        },
      });

      expect(findGaugeChart().props('thresholds')).toEqual([855]);
    });

    describe('when mode is absolute', () => {
      it('only valid threshold values are used', () => {
        createWrapper({
          thresholds: {
            mode: 'absolute',
            values: [undefined, 10, 110, NaN, 'a string', 400],
          },
        });

        expect(findGaugeChart().props('thresholds')).toEqual([110, 400]);
      });

      it('if all threshold values are invalid, a default threshold is defined at 95% of the range between min_value and max_value', () => {
        createWrapper({
          thresholds: {
            mode: 'absolute',
            values: [NaN, undefined, 'a string', 1500],
          },
        });

        expect(findGaugeChart().props('thresholds')).toEqual([855]);
      });
    });

    describe('when mode is percentage', () => {
      it('when values outside of 0-100 bounds are used, a default threshold is defined at 95% of max_value', () => {
        createWrapper({
          thresholds: {
            mode: 'percentage',
            values: [110],
          },
        });

        expect(findGaugeChart().props('thresholds')).toEqual([855]);
      });

      it('if all threshold values are invalid, a default threshold is defined at 95% of max_value', () => {
        createWrapper({
          thresholds: {
            mode: 'percentage',
            values: [NaN, undefined, 'a string', 1500],
          },
        });

        expect(findGaugeChart().props('thresholds')).toEqual([855]);
      });
    });
  });

  describe('split (the number of ticks on the chart arc)', () => {
    const SPLIT_DEFAULT = 10;

    it('is passed to chart as prop', () => {
      createWrapper();

      expect(findGaugeChart().props('splitNumber')).toBe(20);
    });

    it('if not explicitly set, passes a default value to chart', () => {
      createWrapper({ split: '' });

      expect(findGaugeChart().props('splitNumber')).toBe(SPLIT_DEFAULT);
    });

    it('if set as a number that is not an integer, passes the default value to chart', () => {
      createWrapper({ split: 10.5 });

      expect(findGaugeChart().props('splitNumber')).toBe(SPLIT_DEFAULT);
    });

    it('if set as a negative number, passes the default value to chart', () => {
      createWrapper({ split: -10 });

      expect(findGaugeChart().props('splitNumber')).toBe(SPLIT_DEFAULT);
    });
  });

  describe('text (the text displayed on the gauge for the current value)', () => {
    it('displays the query result value when format is not set', () => {
      createWrapper({ format: '' });

      expect(findGaugeChart().props('text')).toBe('3');
    });

    it('displays the query result value when format is set to invalid value', () => {
      createWrapper({ format: 'invalid' });

      expect(findGaugeChart().props('text')).toBe('3');
    });

    it('displays a formatted query result value when format is set', () => {
      createWrapper();

      expect(findGaugeChart().props('text')).toBe('3kB');
    });

    it('displays a placeholder value when metric is empty', () => {
      createWrapper({ metrics: [] });

      expect(findGaugeChart().props('text')).toBe('--');
    });
  });

  describe('value', () => {
    it('correct value is passed', () => {
      createWrapper();

      expect(findGaugeChart().props('value')).toBe(3);
    });
  });
});