summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/charts/column_spec.js
blob: 16e2080c000bbdd1a91bb41f14166a8d2ae70a92 (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
import { shallowMount } from '@vue/test-utils';
import timezoneMock from 'timezone-mock';
import { GlColumnChart } from '@gitlab/ui/dist/charts';
import ColumnChart from '~/monitoring/components/charts/column.vue';

jest.mock('~/lib/utils/icon_utils', () => ({
  getSvgIconPathContent: jest.fn().mockResolvedValue('mockSvgPathContent'),
}));

const yAxisName = 'Y-axis mock name';
const yAxisFormat = 'bytes';
const yAxisPrecistion = 3;
const dataValues = [
  [1495700554.925, '8.0390625'],
  [1495700614.925, '8.0390625'],
  [1495700674.925, '8.0390625'],
];

describe('Column component', () => {
  let wrapper;

  const createWrapper = (props = {}) => {
    wrapper = shallowMount(ColumnChart, {
      propsData: {
        graphData: {
          yAxis: {
            name: yAxisName,
            format: yAxisFormat,
            precision: yAxisPrecistion,
          },
          metrics: [
            {
              result: [
                {
                  metric: {},
                  values: dataValues,
                },
              ],
            },
          ],
        },
        ...props,
      },
    });
  };
  const findChart = () => wrapper.find(GlColumnChart);
  const chartProps = prop => findChart().props(prop);

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

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

  describe('xAxisLabel', () => {
    const mockDate = Date.UTC(2020, 4, 26, 20); // 8:00 PM in GMT

    const useXAxisFormatter = date => {
      const { xAxis } = chartProps('option');
      const { formatter } = xAxis.axisLabel;
      return formatter(date);
    };

    it('x-axis is formatted correctly in m/d h:MM TT format', () => {
      expect(useXAxisFormatter(mockDate)).toEqual('5/26 8:00 PM');
    });

    describe('when in PT timezone', () => {
      beforeAll(() => {
        timezoneMock.register('US/Pacific');
      });

      afterAll(() => {
        timezoneMock.unregister();
      });

      it('by default, values are formatted in PT', () => {
        createWrapper();
        expect(useXAxisFormatter(mockDate)).toEqual('5/26 1:00 PM');
      });

      it('when the chart uses local timezone, y-axis is formatted in PT', () => {
        createWrapper({ timezone: 'LOCAL' });
        expect(useXAxisFormatter(mockDate)).toEqual('5/26 1:00 PM');
      });

      it('when the chart uses UTC, y-axis is formatted in UTC', () => {
        createWrapper({ timezone: 'UTC' });
        expect(useXAxisFormatter(mockDate)).toEqual('5/26 8:00 PM');
      });
    });
  });

  describe('wrapped components', () => {
    describe('GitLab UI column chart', () => {
      it('receives data properties needed for proper chart render', () => {
        expect(chartProps('data').values).toEqual(dataValues);
      });

      it('passes the y axis name correctly', () => {
        expect(chartProps('yAxisTitle')).toBe(yAxisName);
      });

      it('passes the y axis configuration correctly', () => {
        expect(chartProps('option').yAxis).toMatchObject({
          name: yAxisName,
          axisLabel: {
            formatter: expect.any(Function),
          },
          scale: false,
        });
      });

      it('passes a dataZoom configuration', () => {
        expect(chartProps('option').dataZoom).toBeDefined();
      });
    });
  });
});