dashboard_spec.js 25.9 KB
Newer Older
1
import { shallowMount, mount } from '@vue/test-utils';
2
import { ESC_KEY, ESC_KEY_IE11 } from '~/lib/utils/keys';
3
import { objectToQuery } from '~/lib/utils/url_utility';
4 5 6
import VueDraggable from 'vuedraggable';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
7
import { dashboardEmptyStates, metricStates } from '~/monitoring/constants';
8
import Dashboard from '~/monitoring/components/dashboard.vue';
9

10
import DashboardHeader from '~/monitoring/components/dashboard_header.vue';
11
import EmptyState from '~/monitoring/components/empty_state.vue';
12
import GroupEmptyState from '~/monitoring/components/group_empty_state.vue';
13
import DashboardPanel from '~/monitoring/components/dashboard_panel.vue';
14
import GraphGroup from '~/monitoring/components/graph_group.vue';
15
import LinksSection from '~/monitoring/components/links_section.vue';
16 17
import { createStore } from '~/monitoring/stores';
import * as types from '~/monitoring/stores/mutation_types';
18
import {
19
  setupAllDashboards,
20 21 22
  setupStoreWithDashboard,
  setMetricResult,
  setupStoreWithData,
23
  setupStoreWithDataForPanelCount,
24
  setupStoreWithLinks,
25
} from '../store_utils';
26
import { dashboardGitResponse, storeVariables } from '../mock_data';
27 28 29 30 31
import {
  metricsDashboardViewModel,
  metricsDashboardPanelCount,
  dashboardProps,
} from '../fixture_data';
32
import createFlash from '~/flash';
33
import { TEST_HOST } from 'helpers/test_constants';
34 35

jest.mock('~/flash');
36

37 38 39 40 41 42
describe('Dashboard', () => {
  let store;
  let wrapper;
  let mock;

  const createShallowWrapper = (props = {}, options = {}) => {
43
    wrapper = shallowMount(Dashboard, {
44
      propsData: { ...dashboardProps, ...props },
45
      store,
46 47 48
      stubs: {
        DashboardHeader,
      },
49 50 51 52 53
      ...options,
    });
  };

  const createMountedWrapper = (props = {}, options = {}) => {
54
    wrapper = mount(Dashboard, {
55
      propsData: { ...dashboardProps, ...props },
56
      store,
57 58 59 60 61
      stubs: {
        'graph-group': true,
        'dashboard-panel': true,
        'dashboard-header': DashboardHeader,
      },
62 63 64 65 66 67 68
      ...options,
    });
  };

  beforeEach(() => {
    store = createStore();
    mock = new MockAdapter(axios);
69
    jest.spyOn(store, 'dispatch').mockResolvedValue();
70 71 72 73
  });

  afterEach(() => {
    mock.restore();
74 75 76
    if (store.dispatch.mockReset) {
      store.dispatch.mockReset();
    }
77 78 79
  });

  describe('request information to the server', () => {
Jose Vargas's avatar
Jose Vargas committed
80
    it('calls to set time range and fetch data', () => {
81
      createShallowWrapper({ hasMetrics: true });
82 83 84 85 86 87 88 89 90 91 92

      return wrapper.vm.$nextTick().then(() => {
        expect(store.dispatch).toHaveBeenCalledWith(
          'monitoringDashboard/setTimeRange',
          expect.any(Object),
        );

        expect(store.dispatch).toHaveBeenCalledWith('monitoringDashboard/fetchData', undefined);
      });
    });

93
    it('shows up a loading state', () => {
94
      store.state.monitoringDashboard.emptyState = dashboardEmptyStates.LOADING;
95 96

      createShallowWrapper({ hasMetrics: true });
97

98
      return wrapper.vm.$nextTick().then(() => {
99
        expect(wrapper.find(EmptyState).exists()).toBe(true);
100
        expect(wrapper.find(EmptyState).props('selectedState')).toBe(dashboardEmptyStates.LOADING);
101
      });
102 103
    });

104
    it('hides the group panels when showPanels is false', () => {
105
      createMountedWrapper({ hasMetrics: true, showPanels: false });
106

107
      setupStoreWithData(store);
108

109
      return wrapper.vm.$nextTick().then(() => {
110
        expect(wrapper.vm.emptyState).toBeNull();
111 112
        expect(wrapper.findAll('.prometheus-panel')).toHaveLength(0);
      });
113 114
    });

115
    it('fetches the metrics data with proper time window', () => {
116
      createMountedWrapper({ hasMetrics: true });
117

118
      return wrapper.vm.$nextTick().then(() => {
119 120 121 122 123
        expect(store.dispatch).toHaveBeenCalledWith('monitoringDashboard/fetchData', undefined);
        expect(store.dispatch).toHaveBeenCalledWith(
          'monitoringDashboard/setTimeRange',
          expect.objectContaining({ duration: { seconds: 28800 } }),
        );
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
  describe('panel containers layout', () => {
    const findPanelLayoutWrapperAt = index => {
      return wrapper
        .find(GraphGroup)
        .findAll('[data-testid="dashboard-panel-layout-wrapper"]')
        .at(index);
    };

    beforeEach(() => {
      createMountedWrapper({ hasMetrics: true });

      return wrapper.vm.$nextTick();
    });

    describe('when the graph group has an even number of panels', () => {
      it('2 panels - all panel wrappers take half width of their parent', () => {
        setupStoreWithDataForPanelCount(store, 2);

        wrapper.vm.$nextTick(() => {
          expect(findPanelLayoutWrapperAt(0).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(1).classes('col-lg-6')).toBe(true);
        });
      });

      it('4 panels - all panel wrappers take half width of their parent', () => {
        setupStoreWithDataForPanelCount(store, 4);

        wrapper.vm.$nextTick(() => {
          expect(findPanelLayoutWrapperAt(0).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(1).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(2).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(3).classes('col-lg-6')).toBe(true);
        });
      });
    });

    describe('when the graph group has an odd number of panels', () => {
      it('1 panel - panel wrapper does not take half width of its parent', () => {
        setupStoreWithDataForPanelCount(store, 1);

        wrapper.vm.$nextTick(() => {
          expect(findPanelLayoutWrapperAt(0).classes('col-lg-6')).toBe(false);
        });
      });

      it('3 panels - all panels but last take half width of their parents', () => {
        setupStoreWithDataForPanelCount(store, 3);

        wrapper.vm.$nextTick(() => {
          expect(findPanelLayoutWrapperAt(0).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(1).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(2).classes('col-lg-6')).toBe(false);
        });
      });

      it('5 panels - all panels but last take half width of their parents', () => {
        setupStoreWithDataForPanelCount(store, 5);

        wrapper.vm.$nextTick(() => {
          expect(findPanelLayoutWrapperAt(0).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(1).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(2).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(3).classes('col-lg-6')).toBe(true);
          expect(findPanelLayoutWrapperAt(4).classes('col-lg-6')).toBe(false);
        });
      });
    });
  });

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
  describe('dashboard validation warning', () => {
    it('displays a warning if there are validation warnings', () => {
      createMountedWrapper({ hasMetrics: true });

      store.commit(
        `monitoringDashboard/${types.RECEIVE_DASHBOARD_VALIDATION_WARNINGS_SUCCESS}`,
        true,
      );

      return wrapper.vm.$nextTick().then(() => {
        expect(createFlash).toHaveBeenCalled();
      });
    });

    it('does not display a warning if there are no validation warnings', () => {
      createMountedWrapper({ hasMetrics: true });

      store.commit(
        `monitoringDashboard/${types.RECEIVE_DASHBOARD_VALIDATION_WARNINGS_SUCCESS}`,
        false,
      );

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

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
  describe('when the URL contains a reference to a panel', () => {
    let location;

    const setSearch = search => {
      window.location = { ...location, search };
    };

    beforeEach(() => {
      location = window.location;
      delete window.location;
    });

    afterEach(() => {
      window.location = location;
    });

    it('when the URL points to a panel it expands', () => {
      const panelGroup = metricsDashboardViewModel.panelGroups[0];
      const panel = panelGroup.panels[0];

      setSearch(
        objectToQuery({
          group: panelGroup.group,
          title: panel.title,
          y_label: panel.y_label,
        }),
      );

      createMountedWrapper({ hasMetrics: true });
254
      setupStoreWithData(store);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

      return wrapper.vm.$nextTick().then(() => {
        expect(store.dispatch).toHaveBeenCalledWith('monitoringDashboard/setExpandedPanel', {
          group: panelGroup.group,
          panel: expect.objectContaining({
            title: panel.title,
            y_label: panel.y_label,
          }),
        });
      });
    });

    it('when the URL does not link to any panel, no panel is expanded', () => {
      setSearch('');

      createMountedWrapper({ hasMetrics: true });
271
      setupStoreWithData(store);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

      return wrapper.vm.$nextTick().then(() => {
        expect(store.dispatch).not.toHaveBeenCalledWith(
          'monitoringDashboard/setExpandedPanel',
          expect.anything(),
        );
      });
    });

    it('when the URL points to an incorrect panel it shows an error', () => {
      const panelGroup = metricsDashboardViewModel.panelGroups[0];
      const panel = panelGroup.panels[0];

      setSearch(
        objectToQuery({
          group: panelGroup.group,
          title: 'incorrect',
          y_label: panel.y_label,
        }),
      );

      createMountedWrapper({ hasMetrics: true });
294
      setupStoreWithData(store);
295 296 297 298 299 300 301 302 303 304 305

      return wrapper.vm.$nextTick().then(() => {
        expect(createFlash).toHaveBeenCalled();
        expect(store.dispatch).not.toHaveBeenCalledWith(
          'monitoringDashboard/setExpandedPanel',
          expect.anything(),
        );
      });
    });
  });

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 336 337 338 339 340 341 342 343 344 345
  describe('when the panel is expanded', () => {
    let group;
    let panel;

    const expandPanel = (mockGroup, mockPanel) => {
      store.commit(`monitoringDashboard/${types.SET_EXPANDED_PANEL}`, {
        group: mockGroup,
        panel: mockPanel,
      });
    };

    beforeEach(() => {
      setupStoreWithData(store);

      const { panelGroups } = store.state.monitoringDashboard.dashboard;
      group = panelGroups[0].group;
      [panel] = panelGroups[0].panels;

      jest.spyOn(window.history, 'pushState').mockImplementation();
    });

    afterEach(() => {
      window.history.pushState.mockRestore();
    });

    it('URL is updated with panel parameters', () => {
      createMountedWrapper({ hasMetrics: true });
      expandPanel(group, panel);

      const expectedSearch = objectToQuery({
        group,
        title: panel.title,
        y_label: panel.y_label,
      });

      return wrapper.vm.$nextTick(() => {
        expect(window.history.pushState).toHaveBeenCalledTimes(1);
        expect(window.history.pushState).toHaveBeenCalledWith(
          expect.anything(), // state
          expect.any(String), // document title
346
          expect.stringContaining(`${expectedSearch}`),
347 348 349 350 351 352 353
        );
      });
    });

    it('URL is updated with panel parameters and custom dashboard', () => {
      const dashboard = 'dashboard.yml';

354 355 356 357
      store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
        currentDashboard: dashboard,
      });
      createMountedWrapper({ hasMetrics: true });
358 359 360 361 362 363 364 365 366 367 368 369 370 371
      expandPanel(group, panel);

      const expectedSearch = objectToQuery({
        dashboard,
        group,
        title: panel.title,
        y_label: panel.y_label,
      });

      return wrapper.vm.$nextTick(() => {
        expect(window.history.pushState).toHaveBeenCalledTimes(1);
        expect(window.history.pushState).toHaveBeenCalledWith(
          expect.anything(), // state
          expect.any(String), // document title
372
          expect.stringContaining(`${expectedSearch}`),
373 374 375 376 377 378 379 380 381 382 383 384 385 386
        );
      });
    });

    it('URL is updated with no parameters', () => {
      expandPanel(group, panel);
      createMountedWrapper({ hasMetrics: true });
      expandPanel(null, null);

      return wrapper.vm.$nextTick(() => {
        expect(window.history.pushState).toHaveBeenCalledTimes(1);
        expect(window.history.pushState).toHaveBeenCalledWith(
          expect.anything(), // state
          expect.any(String), // document title
387
          expect.not.stringMatching(/group|title|y_label/), // no panel params
388 389 390 391 392
        );
      });
    });
  });

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  describe('when all panels in the first group are loading', () => {
    const findGroupAt = i => wrapper.findAll(GraphGroup).at(i);

    beforeEach(() => {
      setupStoreWithDashboard(store);

      const { panels } = store.state.monitoringDashboard.dashboard.panelGroups[0];
      panels.forEach(({ metrics }) => {
        store.commit(`monitoringDashboard/${types.REQUEST_METRIC_RESULT}`, {
          metricId: metrics[0].metricId,
        });
      });

      createShallowWrapper();

      return wrapper.vm.$nextTick();
    });

    it('a loading icon appears in the first group', () => {
      expect(findGroupAt(0).props('isLoading')).toBe(true);
    });

    it('a loading icon does not appear in the second group', () => {
      expect(findGroupAt(1).props('isLoading')).toBe(false);
    });
  });

420 421
  describe('when all requests have been commited by the store', () => {
    beforeEach(() => {
422 423
      store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
        currentEnvironmentName: 'production',
424 425
        currentDashboard: dashboardGitResponse[0].path,
        projectPath: TEST_HOST,
426
      });
427
      createMountedWrapper({ hasMetrics: true });
428
      setupStoreWithData(store);
429 430

      return wrapper.vm.$nextTick();
431 432
    });

433 434 435 436 437 438 439 440 441
    it('it does not show loading icons in any group', () => {
      setupStoreWithData(store);

      wrapper.vm.$nextTick(() => {
        wrapper.findAll(GraphGroup).wrappers.forEach(groupWrapper => {
          expect(groupWrapper.props('isLoading')).toBe(false);
        });
      });
    });
442 443
  });

444 445 446
  describe('variables section', () => {
    beforeEach(() => {
      createShallowWrapper({ hasMetrics: true });
447
      setupStoreWithData(store);
448
      store.state.monitoringDashboard.variables = storeVariables;
449 450 451 452 453 454 455 456
      return wrapper.vm.$nextTick();
    });

    it('shows the variables section', () => {
      expect(wrapper.vm.shouldShowVariablesSection).toBe(true);
    });
  });

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  describe('links section', () => {
    beforeEach(() => {
      createShallowWrapper({ hasMetrics: true });
      setupStoreWithData(store);
      setupStoreWithLinks(store);

      return wrapper.vm.$nextTick();
    });

    it('shows the links section', () => {
      expect(wrapper.vm.shouldShowLinksSection).toBe(true);
      expect(wrapper.find(LinksSection)).toExist();
    });
  });

472 473 474 475 476 477
  describe('single panel expands to "full screen" mode', () => {
    const findExpandedPanel = () => wrapper.find({ ref: 'expandedPanel' });

    describe('when the panel is not expanded', () => {
      beforeEach(() => {
        createShallowWrapper({ hasMetrics: true });
478
        setupStoreWithData(store);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
        return wrapper.vm.$nextTick();
      });

      it('expanded panel is not visible', () => {
        expect(findExpandedPanel().isVisible()).toBe(false);
      });

      it('can set a panel as expanded', () => {
        const panel = wrapper.findAll(DashboardPanel).at(1);

        jest.spyOn(store, 'dispatch');

        panel.vm.$emit('expand');

        const groupData = metricsDashboardViewModel.panelGroups[0];

        expect(store.dispatch).toHaveBeenCalledWith('monitoringDashboard/setExpandedPanel', {
          group: groupData.group,
          panel: expect.objectContaining({
            id: groupData.panels[0].id,
          }),
        });
      });
    });

    describe('when the panel is expanded', () => {
      let group;
      let panel;

508 509
      const mockKeyup = key => window.dispatchEvent(new KeyboardEvent('keyup', { key }));

510 511 512 513 514 515
      const MockPanel = {
        template: `<div><slot name="topLeft"/></div>`,
      };

      beforeEach(() => {
        createShallowWrapper({ hasMetrics: true }, { stubs: { DashboardPanel: MockPanel } });
516
        setupStoreWithData(store);
517

518
        const { panelGroups } = store.state.monitoringDashboard.dashboard;
519 520 521 522

        group = panelGroups[0].group;
        [panel] = panelGroups[0].panels;

523
        store.commit(`monitoringDashboard/${types.SET_EXPANDED_PANEL}`, {
524 525 526
          group,
          panel,
        });
527 528 529

        jest.spyOn(store, 'dispatch');

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
        return wrapper.vm.$nextTick();
      });

      it('displays a single panel and others are hidden', () => {
        const panels = wrapper.findAll(MockPanel);
        const visiblePanels = panels.filter(w => w.isVisible());

        expect(findExpandedPanel().isVisible()).toBe(true);
        // v-show for hiding panels is more performant than v-if
        // check for panels to be hidden.
        expect(panels.length).toBe(metricsDashboardPanelCount + 1);
        expect(visiblePanels.length).toBe(1);
      });

      it('sets a link to the expanded panel', () => {
        const searchQuery =
546
          '?dashboard=config%2Fprometheus%2Fcommon_metrics.yml&group=System%20metrics%20(Kubernetes)&title=Memory%20Usage%20(Total)&y_label=Total%20Memory%20Used%20(GB)';
547 548 549 550 551 552 553

        expect(findExpandedPanel().attributes('clipboard-text')).toEqual(
          expect.stringContaining(searchQuery),
        );
      });

      it('restores full dashboard by clicking `back`', () => {
554
        wrapper.find({ ref: 'goBackBtn' }).vm.$emit('click');
555 556 557 558 559 560

        expect(store.dispatch).toHaveBeenCalledWith(
          'monitoringDashboard/clearExpandedPanel',
          undefined,
        );
      });
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

      it('restores dashboard from full screen by typing the Escape key', () => {
        mockKeyup(ESC_KEY);
        expect(store.dispatch).toHaveBeenCalledWith(
          `monitoringDashboard/clearExpandedPanel`,
          undefined,
        );
      });

      it('restores dashboard from full screen by typing the Escape key on IE11', () => {
        mockKeyup(ESC_KEY_IE11);

        expect(store.dispatch).toHaveBeenCalledWith(
          `monitoringDashboard/clearExpandedPanel`,
          undefined,
        );
      });
578 579 580
    });
  });

581
  describe('when one of the metrics is missing', () => {
582
    beforeEach(() => {
583
      createShallowWrapper({ hasMetrics: true });
584

585 586
      setupStoreWithDashboard(store);
      setMetricResult({ store, result: [], panel: 2 });
587

588
      return wrapper.vm.$nextTick();
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    });

    it('shows a group empty area', () => {
      const emptyGroup = wrapper.findAll({ ref: 'empty-group' });

      expect(emptyGroup).toHaveLength(1);
      expect(emptyGroup.is(GroupEmptyState)).toBe(true);
    });

    it('group empty area displays a NO_DATA state', () => {
      expect(
        wrapper
          .findAll({ ref: 'empty-group' })
          .at(0)
          .props('selectedState'),
      ).toEqual(metricStates.NO_DATA);
    });
  });

  describe('drag and drop function', () => {
    const findDraggables = () => wrapper.findAll(VueDraggable);
    const findEnabledDraggables = () => findDraggables().filter(f => !f.attributes('disabled'));
    const findDraggablePanels = () => wrapper.findAll('.js-draggable-panel');
    const findRearrangeButton = () => wrapper.find('.js-rearrange-button');

614
    beforeEach(() => {
615 616
      // call original dispatch
      store.dispatch.mockRestore();
617

618
      createShallowWrapper({ hasMetrics: true });
619
      setupStoreWithData(store);
620

621
      return wrapper.vm.$nextTick();
622 623 624 625
    });

    it('wraps vuedraggable', () => {
      expect(findDraggablePanels().exists()).toBe(true);
626
      expect(findDraggablePanels().length).toEqual(metricsDashboardPanelCount);
627 628 629 630 631 632 633 634
    });

    it('is disabled by default', () => {
      expect(findRearrangeButton().exists()).toBe(false);
      expect(findEnabledDraggables().length).toBe(0);
    });

    describe('when rearrange is enabled', () => {
635
      beforeEach(() => {
636
        wrapper.setProps({ rearrangePanelsAvailable: true });
637
        return wrapper.vm.$nextTick();
638 639 640 641 642 643 644 645 646 647 648 649
      });

      it('displays rearrange button', () => {
        expect(findRearrangeButton().exists()).toBe(true);
      });

      describe('when rearrange button is clicked', () => {
        const findFirstDraggableRemoveButton = () =>
          findDraggablePanels()
            .at(0)
            .find('.js-draggable-remove');

650
        beforeEach(() => {
651
          findRearrangeButton().vm.$emit('click');
652
          return wrapper.vm.$nextTick();
653 654 655 656 657 658 659
        });

        it('it enables draggables', () => {
          expect(findRearrangeButton().attributes('pressed')).toBeTruthy();
          expect(findEnabledDraggables()).toEqual(findDraggables());
        });

660
        it('metrics can be swapped', () => {
661
          const firstDraggable = findDraggables().at(0);
662
          const mockMetrics = [...metricsDashboardViewModel.panelGroups[0].panels];
663 664 665 666 667 668 669 670

          const firstTitle = mockMetrics[0].title;
          const secondTitle = mockMetrics[1].title;

          // swap two elements and `input` them
          [mockMetrics[0], mockMetrics[1]] = [mockMetrics[1], mockMetrics[0]];
          firstDraggable.vm.$emit('input', mockMetrics);

671
          return wrapper.vm.$nextTick(() => {
672
            const { panels } = wrapper.vm.dashboard.panelGroups[0];
673 674 675 676 677 678

            expect(panels[1].title).toEqual(firstTitle);
            expect(panels[0].title).toEqual(secondTitle);
          });
        });

679
        it('shows a remove button, which removes a panel', () => {
680 681
          expect(findFirstDraggableRemoveButton().isEmpty()).toBe(false);

682
          expect(findDraggablePanels().length).toEqual(metricsDashboardPanelCount);
683 684
          findFirstDraggableRemoveButton().trigger('click');

685
          return wrapper.vm.$nextTick(() => {
686
            expect(findDraggablePanels().length).toEqual(metricsDashboardPanelCount - 1);
687 688 689
          });
        });

690
        it('it disables draggables when clicked again', () => {
691
          findRearrangeButton().vm.$emit('click');
692
          return wrapper.vm.$nextTick(() => {
693 694 695 696 697 698 699 700 701
            expect(findRearrangeButton().attributes('pressed')).toBeFalsy();
            expect(findEnabledDraggables().length).toBe(0);
          });
        });
      });
    });
  });

  describe('cluster health', () => {
702
    beforeEach(() => {
703
      createShallowWrapper({ hasMetrics: true, showHeader: false });
704 705

      // all_dashboards is not defined in health dashboards
706
      store.commit(`monitoringDashboard/${types.SET_ALL_DASHBOARDS}`, undefined);
707
      return wrapper.vm.$nextTick();
708 709
    });

710 711 712 713
    it('hides dashboard header by default', () => {
      expect(wrapper.find({ ref: 'prometheusGraphsHeader' }).exists()).toEqual(false);
    });

714 715 716 717 718 719
    it('renders correctly', () => {
      expect(wrapper.isVueInstance()).toBe(true);
      expect(wrapper.exists()).toBe(true);
    });
  });

720 721
  describe('document title', () => {
    const originalTitle = 'Original Title';
722
    const overviewDashboardName = dashboardGitResponse[0].display_name;
723 724 725 726 727 728 729 730 731 732

    beforeEach(() => {
      document.title = originalTitle;
      createShallowWrapper({ hasMetrics: true });
    });

    afterAll(() => {
      document.title = '';
    });

733
    it('is prepended with the overview dashboard name by default', () => {
734 735 736
      setupAllDashboards(store);

      return wrapper.vm.$nextTick().then(() => {
737
        expect(document.title.startsWith(`${overviewDashboardName} · `)).toBe(true);
738 739 740 741 742 743 744 745 746 747 748 749 750 751
      });
    });

    it('is prepended with dashboard name if path is known', () => {
      const dashboard = dashboardGitResponse[1];
      const currentDashboard = dashboard.path;

      setupAllDashboards(store, currentDashboard);

      return wrapper.vm.$nextTick().then(() => {
        expect(document.title.startsWith(`${dashboard.display_name} · `)).toBe(true);
      });
    });

752
    it('is prepended with the overview dashboard name if path is not known', () => {
753 754 755
      setupAllDashboards(store, 'unknown/path');

      return wrapper.vm.$nextTick().then(() => {
756
        expect(document.title.startsWith(`${overviewDashboardName} · `)).toBe(true);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
      });
    });

    it('is not modified when dashboard name is not provided', () => {
      const dashboard = { ...dashboardGitResponse[1], display_name: null };
      const currentDashboard = dashboard.path;

      store.commit(`monitoringDashboard/${types.SET_ALL_DASHBOARDS}`, [dashboard]);

      store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
        currentDashboard,
      });

      return wrapper.vm.$nextTick().then(() => {
        expect(document.title).toBe(originalTitle);
      });
    });
774 775
  });

776
  describe('Clipboard text in panels', () => {
777
    const currentDashboard = dashboardGitResponse[1].path;
778
    const panelIndex = 1; // skip expanded panel
779

780
    const getClipboardTextFirstPanel = () =>
781
      wrapper
782
        .findAll(DashboardPanel)
783
        .at(panelIndex)
784
        .props('clipboardText');
785

786
    beforeEach(() => {
787
      setupStoreWithData(store);
788 789 790 791
      store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
        currentDashboard,
      });
      createShallowWrapper({ hasMetrics: true });
792

793
      return wrapper.vm.$nextTick();
794 795 796
    });

    it('contains a link to the dashboard', () => {
797 798 799
      const dashboardParam = `dashboard=${encodeURIComponent(currentDashboard)}`;

      expect(getClipboardTextFirstPanel()).toContain(dashboardParam);
800 801 802
      expect(getClipboardTextFirstPanel()).toContain(`group=`);
      expect(getClipboardTextFirstPanel()).toContain(`title=`);
      expect(getClipboardTextFirstPanel()).toContain(`y_label=`);
803 804
    });
  });
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  describe('keyboard shortcuts', () => {
    const currentDashboard = dashboardGitResponse[1].path;
    const panelRef = 'dashboard-panel-response-metrics-aws-elb-4-1'; // skip expanded panel

    // While the recommendation in the documentation is to test
    // with a data-testid attribute, I want to make sure that
    // the dashboard panels have a ref attribute set.
    const getDashboardPanel = () => wrapper.find({ ref: panelRef });

    beforeEach(() => {
      setupStoreWithData(store);
      store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
        currentDashboard,
      });
      createShallowWrapper({ hasMetrics: true });

      wrapper.setData({ hoveredPanel: panelRef });

      return wrapper.vm.$nextTick();
    });

    it('contains a ref attribute inside a DashboardPanel component', () => {
      const dashboardPanel = getDashboardPanel();

      expect(dashboardPanel.exists()).toBe(true);
    });
  });
833
});