summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/panel_type_spec.js
blob: 54a63e7f61fbd24b329812b8585d434eeb50fb1f (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
import { shallowMount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import { setTestTimeout } from 'helpers/timeout';
import axios from '~/lib/utils/axios_utils';
import PanelType from '~/monitoring/components/panel_type.vue';
import EmptyChart from '~/monitoring/components/charts/empty_chart.vue';
import TimeSeriesChart from '~/monitoring/components/charts/time_series.vue';
import AnomalyChart from '~/monitoring/components/charts/anomaly.vue';
import { graphDataPrometheusQueryRange } from '../../javascripts/monitoring/mock_data';
import { anomalyMockGraphData } from '../../frontend/monitoring/mock_data';
import { createStore } from '~/monitoring/stores';

global.IS_EE = true;
global.URL.createObjectURL = jest.fn();

describe('Panel Type component', () => {
  let axiosMock;
  let store;
  let panelType;
  const dashboardWidth = 100;
  const exampleText = 'example_text';

  beforeEach(() => {
    setTestTimeout(1000);
    axiosMock = new AxiosMockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.reset();
  });

  describe('When no graphData is available', () => {
    let glEmptyChart;
    // Deep clone object before modifying
    const graphDataNoResult = JSON.parse(JSON.stringify(graphDataPrometheusQueryRange));
    graphDataNoResult.queries[0].result = [];

    beforeEach(() => {
      panelType = shallowMount(PanelType, {
        propsData: {
          clipboardText: 'dashboard_link',
          dashboardWidth,
          graphData: graphDataNoResult,
        },
        sync: false,
        attachToDocument: true,
      });
    });

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

    describe('Empty Chart component', () => {
      beforeEach(() => {
        glEmptyChart = panelType.find(EmptyChart);
      });

      it('is a Vue instance', () => {
        expect(glEmptyChart.isVueInstance()).toBe(true);
      });

      it('it receives a graph title', () => {
        const props = glEmptyChart.props();

        expect(props.graphTitle).toBe(panelType.vm.graphData.title);
      });
    });
  });

  describe('when Graph data is available', () => {
    const propsData = {
      clipboardText: exampleText,
      dashboardWidth,
      graphData: graphDataPrometheusQueryRange,
    };

    beforeEach(done => {
      store = createStore();
      panelType = shallowMount(PanelType, {
        propsData,
        store,
        sync: false,
        attachToDocument: true,
      });
      panelType.vm.$nextTick(done);
    });

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

    describe('Time Series Chart panel type', () => {
      it('is rendered', () => {
        expect(panelType.find(TimeSeriesChart).isVueInstance()).toBe(true);
        expect(panelType.find(TimeSeriesChart).exists()).toBe(true);
      });

      it('sets clipboard text on the dropdown', () => {
        const link = () => panelType.find('.js-chart-link');
        const clipboardText = () => link().element.dataset.clipboardText;

        expect(clipboardText()).toBe(exampleText);
      });
    });

    describe('Anomaly Chart panel type', () => {
      beforeEach(done => {
        panelType.setProps({
          graphData: anomalyMockGraphData,
        });
        panelType.vm.$nextTick(done);
      });

      it('is rendered with an anomaly chart', () => {
        expect(panelType.find(AnomalyChart).isVueInstance()).toBe(true);
        expect(panelType.find(AnomalyChart).exists()).toBe(true);
      });
    });
  });

  describe('when downloading metrics data as CSV', () => {
    beforeEach(done => {
      graphDataPrometheusQueryRange.y_label = 'metric';
      store = createStore();
      panelType = shallowMount(PanelType, {
        propsData: {
          clipboardText: exampleText,
          dashboardWidth,
          graphData: graphDataPrometheusQueryRange,
        },
        store,
        sync: false,
        attachToDocument: true,
      });
      panelType.vm.$nextTick(done);
    });

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

    describe('csvText', () => {
      it('converts metrics data from json to csv', () => {
        const header = `timestamp,${graphDataPrometheusQueryRange.y_label}`;
        const data = graphDataPrometheusQueryRange.queries[0].result[0].values;
        const firstRow = `${data[0][0]},${data[0][1]}`;
        const secondRow = `${data[1][0]},${data[1][1]}`;

        expect(panelType.vm.csvText).toBe(`${header}\r\n${firstRow}\r\n${secondRow}\r\n`);
      });
    });

    describe('downloadCsv', () => {
      it('produces a link with a Blob', () => {
        expect(global.URL.createObjectURL).toHaveBeenLastCalledWith(expect.any(Blob));
        expect(global.URL.createObjectURL).toHaveBeenLastCalledWith(
          expect.objectContaining({
            size: panelType.vm.csvText.length,
            type: 'text/plain',
          }),
        );
      });
    });
  });
});