summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/dashboards_dropdown_spec.js
blob: b29d86cbc5b789607a7165efb358bc5ae8c9ee7a (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
328
329
330
331
332
333
334
335
import { shallowMount } from '@vue/test-utils';
import { GlDropdownItem, GlModal, GlLoadingIcon, GlAlert, GlIcon } from '@gitlab/ui';
import waitForPromises from 'helpers/wait_for_promises';

import DashboardsDropdown from '~/monitoring/components/dashboards_dropdown.vue';
import DuplicateDashboardForm from '~/monitoring/components/duplicate_dashboard_form.vue';

import { dashboardGitResponse } from '../mock_data';

const defaultBranch = 'master';

const starredDashboards = dashboardGitResponse.filter(({ starred }) => starred);
const notStarredDashboards = dashboardGitResponse.filter(({ starred }) => !starred);

describe('DashboardsDropdown', () => {
  let wrapper;
  let mockDashboards;
  let mockSelectedDashboard;

  function createComponent(props, opts = {}) {
    const storeOpts = {
      methods: {
        duplicateSystemDashboard: jest.fn(),
      },
      computed: {
        allDashboards: () => mockDashboards,
        selectedDashboard: () => mockSelectedDashboard,
      },
    };

    return shallowMount(DashboardsDropdown, {
      propsData: {
        ...props,
        defaultBranch,
      },
      sync: false,
      ...storeOpts,
      ...opts,
    });
  }

  const findItems = () => wrapper.findAll(GlDropdownItem);
  const findItemAt = i => wrapper.findAll(GlDropdownItem).at(i);
  const findSearchInput = () => wrapper.find({ ref: 'monitorDashboardsDropdownSearch' });
  const findNoItemsMsg = () => wrapper.find({ ref: 'monitorDashboardsDropdownMsg' });
  const findStarredListDivider = () => wrapper.find({ ref: 'starredListDivider' });
  const setSearchTerm = searchTerm => wrapper.setData({ searchTerm });

  beforeEach(() => {
    mockDashboards = dashboardGitResponse;
    mockSelectedDashboard = null;
  });

  describe('when it receives dashboards data', () => {
    beforeEach(() => {
      wrapper = createComponent();
    });

    it('displays an item for each dashboard', () => {
      expect(findItems().length).toEqual(dashboardGitResponse.length);
    });

    it('displays items with the dashboard display name, with starred dashboards first', () => {
      expect(findItemAt(0).text()).toBe(starredDashboards[0].display_name);
      expect(findItemAt(1).text()).toBe(notStarredDashboards[0].display_name);
      expect(findItemAt(2).text()).toBe(notStarredDashboards[1].display_name);
    });

    it('displays separator between starred and not starred dashboards', () => {
      expect(findStarredListDivider().exists()).toBe(true);
    });

    it('displays a search input', () => {
      expect(findSearchInput().isVisible()).toBe(true);
    });

    it('hides no message text by default', () => {
      expect(findNoItemsMsg().isVisible()).toBe(false);
    });

    it('filters dropdown items when searched for item exists in the list', () => {
      const searchTerm = 'Default';
      setSearchTerm(searchTerm);

      return wrapper.vm.$nextTick(() => {
        expect(findItems()).toHaveLength(1);
      });
    });

    it('shows no items found message when searched for item does not exists in the list', () => {
      const searchTerm = 'does-not-exist';
      setSearchTerm(searchTerm);

      return wrapper.vm.$nextTick(() => {
        expect(findNoItemsMsg().isVisible()).toBe(true);
      });
    });
  });

  describe('when the dashboard is missing a display name', () => {
    beforeEach(() => {
      mockDashboards = dashboardGitResponse.map(d => ({ ...d, display_name: undefined }));
      wrapper = createComponent();
    });

    it('displays items with the dashboard path, with starred dashboards first', () => {
      expect(findItemAt(0).text()).toBe(starredDashboards[0].path);
      expect(findItemAt(1).text()).toBe(notStarredDashboards[0].path);
      expect(findItemAt(2).text()).toBe(notStarredDashboards[1].path);
    });
  });

  describe('when it receives starred dashboards', () => {
    beforeEach(() => {
      mockDashboards = starredDashboards;
      wrapper = createComponent();
    });

    it('displays an item for each dashboard', () => {
      expect(findItems().length).toEqual(starredDashboards.length);
    });

    it('displays a star icon', () => {
      const star = findItemAt(0).find(GlIcon);
      expect(star.exists()).toBe(true);
      expect(star.attributes('name')).toBe('star');
    });

    it('displays no separator between starred and not starred dashboards', () => {
      expect(findStarredListDivider().exists()).toBe(false);
    });
  });

  describe('when it receives only not-starred dashboards', () => {
    beforeEach(() => {
      mockDashboards = notStarredDashboards;
      wrapper = createComponent();
    });

    it('displays an item for each dashboard', () => {
      expect(findItems().length).toEqual(notStarredDashboards.length);
    });

    it('displays no star icon', () => {
      const star = findItemAt(0).find(GlIcon);
      expect(star.exists()).toBe(false);
    });

    it('displays no separator between starred and not starred dashboards', () => {
      expect(findStarredListDivider().exists()).toBe(false);
    });
  });

  describe('when a system dashboard is selected', () => {
    let duplicateDashboardAction;
    let modalDirective;

    beforeEach(() => {
      [mockSelectedDashboard] = dashboardGitResponse;
      modalDirective = jest.fn();
      duplicateDashboardAction = jest.fn().mockResolvedValue();

      wrapper = createComponent(
        {},
        {
          directives: {
            GlModal: modalDirective,
          },
          methods: {
            // Mock vuex actions
            duplicateSystemDashboard: duplicateDashboardAction,
          },
        },
      );

      wrapper.vm.$refs.duplicateDashboardModal.hide = jest.fn();
    });

    it('displays an item for each dashboard plus a "duplicate dashboard" item', () => {
      const item = wrapper.findAll({ ref: 'duplicateDashboardItem' });

      expect(findItems().length).toEqual(dashboardGitResponse.length + 1);
      expect(item.length).toBe(1);
    });

    describe('modal form', () => {
      let okEvent;

      const findModal = () => wrapper.find(GlModal);
      const findAlert = () => wrapper.find(GlAlert);

      beforeEach(() => {
        okEvent = {
          preventDefault: jest.fn(),
        };
      });

      it('exists and contains a form to duplicate a dashboard', () => {
        expect(findModal().exists()).toBe(true);
        expect(findModal().contains(DuplicateDashboardForm)).toBe(true);
      });

      it('saves a new dashboard', () => {
        findModal().vm.$emit('ok', okEvent);

        return waitForPromises().then(() => {
          expect(okEvent.preventDefault).toHaveBeenCalled();

          expect(wrapper.find(GlLoadingIcon).exists()).toBe(false);
          expect(wrapper.vm.$refs.duplicateDashboardModal.hide).toHaveBeenCalled();
          expect(wrapper.emitted().selectDashboard).toBeTruthy();
          expect(findAlert().exists()).toBe(false);
        });
      });

      describe('when a new dashboard is saved succesfully', () => {
        const newDashboard = {
          can_edit: true,
          default: false,
          display_name: 'A new dashboard',
          system_dashboard: false,
        };

        const submitForm = formVals => {
          duplicateDashboardAction.mockResolvedValueOnce(newDashboard);
          findModal()
            .find(DuplicateDashboardForm)
            .vm.$emit('change', {
              dashboard: 'common_metrics.yml',
              commitMessage: 'A commit message',
              ...formVals,
            });
          findModal().vm.$emit('ok', okEvent);
        };

        it('to the default branch, redirects to the new dashboard', () => {
          submitForm({
            branch: defaultBranch,
          });

          return waitForPromises().then(() => {
            expect(wrapper.emitted().selectDashboard[0][0]).toEqual(newDashboard);
          });
        });

        it('to a new branch refreshes in the current dashboard', () => {
          submitForm({
            branch: 'another-branch',
          });

          return waitForPromises().then(() => {
            expect(wrapper.emitted().selectDashboard[0][0]).toEqual(dashboardGitResponse[0]);
          });
        });
      });

      it('handles error when a new dashboard is not saved', () => {
        const errMsg = 'An error occurred';

        duplicateDashboardAction.mockRejectedValueOnce(errMsg);
        findModal().vm.$emit('ok', okEvent);

        return waitForPromises().then(() => {
          expect(okEvent.preventDefault).toHaveBeenCalled();

          expect(findAlert().exists()).toBe(true);
          expect(findAlert().text()).toBe(errMsg);

          expect(wrapper.find(GlLoadingIcon).exists()).toBe(false);
          expect(wrapper.vm.$refs.duplicateDashboardModal.hide).not.toHaveBeenCalled();
        });
      });

      it('id is correct, as the value of modal directive binding matches modal id', () => {
        expect(modalDirective).toHaveBeenCalledTimes(1);

        // Binding's second argument contains the modal id
        expect(modalDirective.mock.calls[0][1]).toEqual(
          expect.objectContaining({
            value: findModal().props('modalId'),
          }),
        );
      });

      it('updates the form on changes', () => {
        const formVals = {
          dashboard: 'common_metrics.yml',
          commitMessage: 'A commit message',
        };

        findModal()
          .find(DuplicateDashboardForm)
          .vm.$emit('change', formVals);

        // Binding's second argument contains the modal id
        expect(wrapper.vm.form).toEqual(formVals);
      });
    });
  });

  describe('when a custom dashboard is selected', () => {
    const findModal = () => wrapper.find(GlModal);

    beforeEach(() => {
      wrapper = createComponent({
        selectedDashboard: dashboardGitResponse[1],
      });
    });

    it('displays an item for each dashboard', () => {
      const item = wrapper.findAll({ ref: 'duplicateDashboardItem' });

      expect(findItems()).toHaveLength(dashboardGitResponse.length);
      expect(item.length).toBe(0);
    });

    it('modal form does not exist and contains a form to duplicate a dashboard', () => {
      expect(findModal().exists()).toBe(false);
    });
  });

  describe('when a dashboard gets selected by the user', () => {
    beforeEach(() => {
      wrapper = createComponent();
      findItemAt(1).vm.$emit('click');
    });

    it('emits a "selectDashboard" event', () => {
      expect(wrapper.emitted().selectDashboard).toBeTruthy();
    });
    it('emits a "selectDashboard" event with dashboard information', () => {
      expect(wrapper.emitted().selectDashboard[0]).toEqual([dashboardGitResponse[0]]);
    });
  });
});