summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/charts/options_spec.js
blob: 064ce6f204ca07f7d14fe05ea3ef91612459c6ad (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { SUPPORTED_FORMATS } from '~/lib/utils/unit_format';
import {
  getYAxisOptions,
  getTooltipFormatter,
  getValidThresholds,
} from '~/monitoring/components/charts/options';

describe('options spec', () => {
  describe('getYAxisOptions', () => {
    it('default options', () => {
      const options = getYAxisOptions();

      expect(options).toMatchObject({
        name: expect.any(String),
        axisLabel: {
          formatter: expect.any(Function),
        },
        scale: true,
        boundaryGap: [expect.any(Number), expect.any(Number)],
      });

      expect(options.name).not.toHaveLength(0);
    });

    it('name options', () => {
      const yAxisName = 'My axis values';
      const options = getYAxisOptions({
        name: yAxisName,
      });

      expect(options).toMatchObject({
        name: yAxisName,
        nameLocation: 'center',
        nameGap: expect.any(Number),
      });
    });

    it('formatter options defaults to engineering notation', () => {
      const options = getYAxisOptions();

      expect(options.axisLabel.formatter).toEqual(expect.any(Function));
      expect(options.axisLabel.formatter(3002.1)).toBe('3k');
    });

    it('formatter options allows for precision to be set explicitly', () => {
      const options = getYAxisOptions({
        precision: 4,
      });

      expect(options.axisLabel.formatter).toEqual(expect.any(Function));
      expect(options.axisLabel.formatter(5002.1)).toBe('5.0021k');
    });

    it('formatter options allows for overrides in milliseconds', () => {
      const options = getYAxisOptions({
        format: SUPPORTED_FORMATS.milliseconds,
      });

      expect(options.axisLabel.formatter).toEqual(expect.any(Function));
      expect(options.axisLabel.formatter(1.1234)).toBe('1.12ms');
    });

    it('formatter options allows for overrides in bytes', () => {
      const options = getYAxisOptions({
        format: SUPPORTED_FORMATS.bytes,
      });

      expect(options.axisLabel.formatter).toEqual(expect.any(Function));
      expect(options.axisLabel.formatter(1)).toBe('1.00B');
    });
  });

  describe('getTooltipFormatter', () => {
    it('default format', () => {
      const formatter = getTooltipFormatter();

      expect(formatter).toEqual(expect.any(Function));
      expect(formatter(0.11111)).toBe('111.1m');
    });

    it('defined format', () => {
      const formatter = getTooltipFormatter({
        format: SUPPORTED_FORMATS.bytes,
      });

      expect(formatter(1)).toBe('1.000B');
    });
  });

  describe('getValidThresholds', () => {
    const invalidCases = [null, undefined, NaN, 'a string', true, false];

    let thresholds;

    afterEach(() => {
      thresholds = null;
    });

    it('returns same thresholds when passed values within range', () => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [10, 50],
      });

      expect(thresholds).toEqual([10, 50]);
    });

    it('filters out thresholds that are out of range', () => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [-5, 10, 110],
      });

      expect(thresholds).toEqual([10]);
    });
    it('filters out duplicate thresholds', () => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [5, 5, 10, 10],
      });

      expect(thresholds).toEqual([5, 10]);
    });

    it('sorts passed thresholds and applies only the first two in ascending order', () => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [10, 1, 35, 20, 5],
      });

      expect(thresholds).toEqual([1, 5]);
    });

    it('thresholds equal to min or max are filtered out', () => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [0, 100],
      });

      expect(thresholds).toEqual([]);
    });

    it.each(invalidCases)('invalid values for thresholds are filtered out', (invalidValue) => {
      thresholds = getValidThresholds({
        mode: 'absolute',
        range: { min: 0, max: 100 },
        values: [10, invalidValue],
      });

      expect(thresholds).toEqual([10]);
    });

    describe('range', () => {
      it('when range is not defined, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          values: [10, 20],
        });

        expect(thresholds).toEqual([]);
      });

      it('when min is not defined, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          range: { max: 100 },
          values: [10, 20],
        });

        expect(thresholds).toEqual([]);
      });

      it('when max is not defined, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          range: { min: 0 },
          values: [10, 20],
        });

        expect(thresholds).toEqual([]);
      });

      it('when min is larger than max, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          range: { min: 100, max: 0 },
          values: [10, 20],
        });

        expect(thresholds).toEqual([]);
      });

      it.each(invalidCases)(
        'when min has invalid value, empty result is returned',
        (invalidValue) => {
          thresholds = getValidThresholds({
            mode: 'absolute',
            range: { min: invalidValue, max: 100 },
            values: [10, 20],
          });

          expect(thresholds).toEqual([]);
        },
      );

      it.each(invalidCases)(
        'when max has invalid value, empty result is returned',
        (invalidValue) => {
          thresholds = getValidThresholds({
            mode: 'absolute',
            range: { min: 0, max: invalidValue },
            values: [10, 20],
          });

          expect(thresholds).toEqual([]);
        },
      );
    });

    describe('values', () => {
      it('if values parameter is omitted, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          range: { min: 0, max: 100 },
        });

        expect(thresholds).toEqual([]);
      });

      it('if there are no values passed, empty result is returned', () => {
        thresholds = getValidThresholds({
          mode: 'absolute',
          range: { min: 0, max: 100 },
          values: [],
        });

        expect(thresholds).toEqual([]);
      });

      it.each(invalidCases)(
        'if invalid values are passed, empty result is returned',
        (invalidValue) => {
          thresholds = getValidThresholds({
            mode: 'absolute',
            range: { min: 0, max: 100 },
            values: [invalidValue],
          });

          expect(thresholds).toEqual([]);
        },
      );
    });

    describe('mode', () => {
      it.each(invalidCases)(
        'if invalid values are passed, empty result is returned',
        (invalidValue) => {
          thresholds = getValidThresholds({
            mode: invalidValue,
            range: { min: 0, max: 100 },
            values: [10, 50],
          });

          expect(thresholds).toEqual([]);
        },
      );

      it('if mode is not passed, empty result is returned', () => {
        thresholds = getValidThresholds({
          range: { min: 0, max: 100 },
          values: [10, 50],
        });

        expect(thresholds).toEqual([]);
      });

      describe('absolute mode', () => {
        it('absolute mode behaves correctly', () => {
          thresholds = getValidThresholds({
            mode: 'absolute',
            range: { min: 0, max: 100 },
            values: [10, 50],
          });

          expect(thresholds).toEqual([10, 50]);
        });
      });

      describe('percentage mode', () => {
        it('percentage mode behaves correctly', () => {
          thresholds = getValidThresholds({
            mode: 'percentage',
            range: { min: 0, max: 1000 },
            values: [10, 50],
          });

          expect(thresholds).toEqual([100, 500]);
        });

        const outOfPercentBoundsValues = [-1, 0, 100, 101];
        it.each(outOfPercentBoundsValues)(
          'when values out of 0-100 range are passed, empty result is returned',
          (invalidValue) => {
            thresholds = getValidThresholds({
              mode: 'percentage',
              range: { min: 0, max: 1000 },
              values: [invalidValue],
            });

            expect(thresholds).toEqual([]);
          },
        );
      });
    });

    it('calling without passing object parameter returns empty array', () => {
      thresholds = getValidThresholds();

      expect(thresholds).toEqual([]);
    });
  });
});