summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/charts/single_stat_spec.js
blob: 144a4e0c968e7d4dd43ad4d0db46243a29f66e25 (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
import { shallowMount } from '@vue/test-utils';
import { GlSingleStat } from '@gitlab/ui/dist/charts';
import SingleStatChart from '~/monitoring/components/charts/single_stat.vue';
import { singleStatGraphData } from '../../graph_data';

describe('Single Stat Chart component', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(SingleStatChart, {
      propsData: {
        graphData: singleStatGraphData({}, { unit: 'MB' }),
        ...props,
      },
    });
  };

  const findChart = () => wrapper.find(GlSingleStat);

  beforeEach(() => {
    createComponent();
  });

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

  describe('computed', () => {
    describe('statValue', () => {
      it('should display the correct value', () => {
        expect(findChart().props('value')).toBe('1.00');
      });

      it('should display the correct value unit', () => {
        expect(findChart().props('unit')).toBe('MB');
      });

      it('should change the value representation to a percentile one', () => {
        createComponent({
          graphData: singleStatGraphData({ max_value: 120 }, { value: 91 }),
        });

        expect(findChart().props('value')).toBe('75.83');
        expect(findChart().props('unit')).toBe('%');
      });

      it('should display NaN for non numeric maxValue values', () => {
        createComponent({
          graphData: singleStatGraphData({ max_value: 'not a number' }),
        });

        expect(findChart().props('value')).toContain('NaN');
      });

      it('should display NaN for missing query values', () => {
        createComponent({
          graphData: singleStatGraphData({ max_value: 120 }, { value: 'NaN' }),
        });

        expect(findChart().props('value')).toContain('NaN');
      });

      it('should not display `unit` when `unit` is undefined', () => {
        createComponent({
          graphData: singleStatGraphData({}, { unit: undefined }),
        });

        expect(findChart().props('value')).not.toContain('undefined');
      });

      it('should not display `unit` when `unit` is null', () => {
        createComponent({
          graphData: singleStatGraphData({}, { unit: null }),
        });

        expect(findChart().props('value')).not.toContain('null');
      });

      describe('when a field attribute is set', () => {
        it('displays a label value instead of metric value when field attribute is used', () => {
          createComponent({
            graphData: singleStatGraphData({ field: 'job' }, { isVector: true }),
          });

          expect(findChart().props('value')).toContain('prometheus');
        });

        it('displays No data to display if field attribute is not present', () => {
          createComponent({
            graphData: singleStatGraphData({ field: 'this-does-not-exist' }),
          });

          expect(findChart().props('value')).toContain('No data to display');
        });
      });
    });
  });
});