summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/dashboard_panel_builder_spec.js
blob: 08c69701bd2bccca99b193089d23d2820b273b9c (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
import { shallowMount } from '@vue/test-utils';
import { GlCard, GlForm, GlFormTextarea, GlAlert } from '@gitlab/ui';
import { createStore } from '~/monitoring/stores';
import DashboardPanel from '~/monitoring/components/dashboard_panel.vue';
import * as types from '~/monitoring/stores/mutation_types';
import { metricsDashboardResponse } from '../fixture_data';
import { mockTimeRange } from '../mock_data';

import DashboardPanelBuilder from '~/monitoring/components/dashboard_panel_builder.vue';
import DateTimePicker from '~/vue_shared/components/date_time_picker/date_time_picker.vue';

const mockPanel = metricsDashboardResponse.dashboard.panel_groups[0].panels[0];

describe('dashboard invalid url parameters', () => {
  let store;
  let wrapper;
  let mockShowToast;

  const createComponent = (props = {}, options = {}) => {
    wrapper = shallowMount(DashboardPanelBuilder, {
      propsData: { ...props },
      store,
      stubs: {
        GlCard,
      },
      mocks: {
        $toast: {
          show: mockShowToast,
        },
      },
      options,
    });
  };

  const findForm = () => wrapper.find(GlForm);
  const findTxtArea = () => findForm().find(GlFormTextarea);
  const findSubmitBtn = () => findForm().find('[type="submit"]');
  const findClipboardCopyBtn = () => wrapper.find({ ref: 'clipboardCopyBtn' });
  const findViewDocumentationBtn = () => wrapper.find({ ref: 'viewDocumentationBtn' });
  const findOpenRepositoryBtn = () => wrapper.find({ ref: 'openRepositoryBtn' });
  const findPanel = () => wrapper.find(DashboardPanel);
  const findTimeRangePicker = () => wrapper.find(DateTimePicker);
  const findRefreshButton = () => wrapper.find('[data-testid="previewRefreshButton"]');

  beforeEach(() => {
    mockShowToast = jest.fn();
    store = createStore();
    createComponent();
    jest.spyOn(store, 'dispatch').mockResolvedValue();
  });

  afterEach(() => {});

  it('is mounted', () => {
    expect(wrapper.exists()).toBe(true);
  });

  it('displays an empty dashboard panel', () => {
    expect(findPanel().exists()).toBe(true);
    expect(findPanel().props('graphData')).toBe(null);
  });

  it('does not fetch initial data by default', () => {
    expect(store.dispatch).not.toHaveBeenCalled();
  });

  describe('yml form', () => {
    it('form exists and can be submitted', () => {
      expect(findForm().exists()).toBe(true);
      expect(findSubmitBtn().exists()).toBe(true);
      expect(findSubmitBtn().props('disabled')).toBe(false);
    });

    it('form has a text area with a default value', () => {
      expect(findTxtArea().exists()).toBe(true);

      const value = findTxtArea().attributes('value');

      // Panel definition should contain a title and a type
      expect(value).toContain('title:');
      expect(value).toContain('type:');
    });

    it('"copy to clipboard" button works', () => {
      findClipboardCopyBtn().vm.$emit('click');
      const clipboardText = findClipboardCopyBtn().attributes('data-clipboard-text');

      expect(clipboardText).toContain('title:');
      expect(clipboardText).toContain('type:');

      expect(mockShowToast).toHaveBeenCalledTimes(1);
    });

    it('on submit fetches a panel preview', () => {
      findForm().vm.$emit('submit', new Event('submit'));

      return wrapper.vm.$nextTick().then(() => {
        expect(store.dispatch).toHaveBeenCalledWith(
          'monitoringDashboard/fetchPanelPreview',
          expect.stringContaining('title:'),
        );
      });
    });

    describe('when form is submitted', () => {
      beforeEach(() => {
        store.commit(`monitoringDashboard/${types.REQUEST_PANEL_PREVIEW}`, 'mock yml content');
        return wrapper.vm.$nextTick();
      });

      it('submit button is disabled', () => {
        expect(findSubmitBtn().props('disabled')).toBe(true);
      });
    });
  });

  describe('time range picker', () => {
    it('is visible by default', () => {
      expect(findTimeRangePicker().exists()).toBe(true);
    });

    it('when changed does not trigger data fetch unless preview panel button is clicked', () => {
      // mimic initial state where SET_PANEL_PREVIEW_IS_SHOWN is set to false
      store.commit(`monitoringDashboard/${types.SET_PANEL_PREVIEW_IS_SHOWN}`, false);

      return wrapper.vm.$nextTick(() => {
        expect(store.dispatch).not.toHaveBeenCalled();
      });
    });

    it('when changed triggers data fetch if preview panel button is clicked', () => {
      findForm().vm.$emit('submit', new Event('submit'));

      store.commit(`monitoringDashboard/${types.SET_PANEL_PREVIEW_TIME_RANGE}`, mockTimeRange);

      return wrapper.vm.$nextTick(() => {
        expect(store.dispatch).toHaveBeenCalled();
      });
    });
  });

  describe('refresh', () => {
    it('is visible by default', () => {
      expect(findRefreshButton().exists()).toBe(true);
    });

    it('when clicked does not trigger data fetch unless preview panel button is clicked', () => {
      // mimic initial state where SET_PANEL_PREVIEW_IS_SHOWN is set to false
      store.commit(`monitoringDashboard/${types.SET_PANEL_PREVIEW_IS_SHOWN}`, false);

      return wrapper.vm.$nextTick(() => {
        expect(store.dispatch).not.toHaveBeenCalled();
      });
    });

    it('when clicked triggers data fetch if preview panel button is clicked', () => {
      // mimic state where preview is visible. SET_PANEL_PREVIEW_IS_SHOWN is set to true
      store.commit(`monitoringDashboard/${types.SET_PANEL_PREVIEW_IS_SHOWN}`, true);

      findRefreshButton().vm.$emit('click');

      return wrapper.vm.$nextTick(() => {
        expect(store.dispatch).toHaveBeenCalledWith(
          'monitoringDashboard/fetchPanelPreviewMetrics',
          undefined,
        );
      });
    });
  });

  describe('instructions card', () => {
    const mockDocsPath = '/docs-path';
    const mockProjectPath = '/project-path';

    beforeEach(() => {
      store.state.monitoringDashboard.addDashboardDocumentationPath = mockDocsPath;
      store.state.monitoringDashboard.projectPath = mockProjectPath;

      createComponent();
    });

    it('displays next actions for the user', () => {
      expect(findViewDocumentationBtn().exists()).toBe(true);
      expect(findViewDocumentationBtn().attributes('href')).toBe(mockDocsPath);

      expect(findOpenRepositoryBtn().exists()).toBe(true);
      expect(findOpenRepositoryBtn().attributes('href')).toBe(mockProjectPath);
    });
  });

  describe('when there is an error', () => {
    const mockError = 'an error ocurred!';

    beforeEach(() => {
      store.commit(`monitoringDashboard/${types.RECEIVE_PANEL_PREVIEW_FAILURE}`, mockError);
      return wrapper.vm.$nextTick();
    });

    it('displays an alert', () => {
      expect(wrapper.find(GlAlert).exists()).toBe(true);
      expect(wrapper.find(GlAlert).text()).toBe(mockError);
    });

    it('displays an empty dashboard panel', () => {
      expect(findPanel().props('graphData')).toBe(null);
    });

    it('changing time range should not refetch data', () => {
      store.commit(`monitoringDashboard/${types.SET_PANEL_PREVIEW_TIME_RANGE}`, mockTimeRange);

      return wrapper.vm.$nextTick(() => {
        expect(store.dispatch).not.toHaveBeenCalled();
      });
    });
  });

  describe('when panel data is available', () => {
    beforeEach(() => {
      store.commit(`monitoringDashboard/${types.RECEIVE_PANEL_PREVIEW_SUCCESS}`, mockPanel);
      return wrapper.vm.$nextTick();
    });

    it('displays no alert', () => {
      expect(wrapper.find(GlAlert).exists()).toBe(false);
    });

    it('displays panel with data', () => {
      const { title, type } = wrapper.find(DashboardPanel).props('graphData');

      expect(title).toBe(mockPanel.title);
      expect(type).toBe(mockPanel.type);
    });
  });
});