Welcome to mirror list, hosted at ThFree Co, Russian Federation.

embed_group_spec.js « embeds « components « monitoring « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 010897529333fec33bc9a2f455dc7fffb287fc71 (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
import { createLocalVue, mount, shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
import { GlButton, GlCard } from '@gitlab/ui';
import { TEST_HOST } from 'helpers/test_constants';
import EmbedGroup from '~/monitoring/components/embeds/embed_group.vue';
import MetricEmbed from '~/monitoring/components/embeds/metric_embed.vue';
import {
  addModuleAction,
  initialEmbedGroupState,
  singleEmbedProps,
  dashboardEmbedProps,
  multipleEmbedProps,
} from './mock_data';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Embed Group', () => {
  let wrapper;
  let store;
  const metricsWithDataGetter = jest.fn();

  function mountComponent({ urls = [TEST_HOST], shallow = true, stubs } = {}) {
    const mountMethod = shallow ? shallowMount : mount;
    wrapper = mountMethod(EmbedGroup, {
      localVue,
      store,
      propsData: {
        urls,
      },
      stubs,
    });
  }

  beforeEach(() => {
    store = new Vuex.Store({
      modules: {
        embedGroup: {
          namespaced: true,
          actions: { addModule: jest.fn() },
          getters: { metricsWithData: metricsWithDataGetter },
          state: initialEmbedGroupState,
        },
      },
    });
    store.registerModule = jest.fn();
    jest.spyOn(store, 'dispatch');
  });

  afterEach(() => {
    metricsWithDataGetter.mockReset();
    if (wrapper) {
      wrapper.destroy();
    }
  });

  describe('interactivity', () => {
    it('hides the component when no chart data is loaded', () => {
      metricsWithDataGetter.mockReturnValue([]);
      mountComponent();

      expect(wrapper.find(GlCard).isVisible()).toBe(false);
    });

    it('shows the component when chart data is loaded', () => {
      metricsWithDataGetter.mockReturnValue([1]);
      mountComponent();

      expect(wrapper.find(GlCard).isVisible()).toBe(true);
    });

    it('is expanded by default', () => {
      metricsWithDataGetter.mockReturnValue([1]);
      mountComponent({ shallow: false, stubs: { MetricEmbed: true } });

      expect(wrapper.find('.gl-card-body').classes()).not.toContain('d-none');
    });

    it('collapses when clicked', done => {
      metricsWithDataGetter.mockReturnValue([1]);
      mountComponent({ shallow: false, stubs: { MetricEmbed: true } });

      wrapper.find(GlButton).trigger('click');

      wrapper.vm.$nextTick(() => {
        expect(wrapper.find('.gl-card-body').classes()).toContain('d-none');
        done();
      });
    });
  });

  describe('single metrics', () => {
    beforeEach(() => {
      metricsWithDataGetter.mockReturnValue([1]);
      mountComponent();
    });

    it('renders an Embed component', () => {
      expect(wrapper.find(MetricEmbed).exists()).toBe(true);
    });

    it('passes the correct props to the Embed component', () => {
      expect(wrapper.find(MetricEmbed).props()).toEqual(singleEmbedProps());
    });

    it('adds the monitoring dashboard module', () => {
      expect(store.dispatch).toHaveBeenCalledWith(addModuleAction, 'monitoringDashboard/0');
    });
  });

  describe('dashboard metrics', () => {
    beforeEach(() => {
      metricsWithDataGetter.mockReturnValue([2]);
      mountComponent();
    });

    it('passes the correct props to the dashboard Embed component', () => {
      expect(wrapper.find(MetricEmbed).props()).toEqual(dashboardEmbedProps());
    });

    it('adds the monitoring dashboard module', () => {
      expect(store.dispatch).toHaveBeenCalledWith(addModuleAction, 'monitoringDashboard/0');
    });
  });

  describe('multiple metrics', () => {
    beforeEach(() => {
      metricsWithDataGetter.mockReturnValue([1, 1]);
      mountComponent({ urls: [TEST_HOST, TEST_HOST] });
    });

    it('creates Embed components', () => {
      expect(wrapper.findAll(MetricEmbed)).toHaveLength(2);
    });

    it('passes the correct props to the Embed components', () => {
      expect(wrapper.findAll(MetricEmbed).wrappers.map(item => item.props())).toEqual(
        multipleEmbedProps(),
      );
    });

    it('adds multiple monitoring dashboard modules', () => {
      expect(store.dispatch).toHaveBeenCalledWith(addModuleAction, 'monitoringDashboard/0');
      expect(store.dispatch).toHaveBeenCalledWith(addModuleAction, 'monitoringDashboard/1');
    });
  });

  describe('button text', () => {
    it('has a singular label when there is one embed', () => {
      metricsWithDataGetter.mockReturnValue([1]);
      mountComponent({ shallow: false, stubs: { MetricEmbed: true } });

      expect(wrapper.find(GlButton).text()).toBe('Hide chart');
    });

    it('has a plural label when there are multiple embeds', () => {
      metricsWithDataGetter.mockReturnValue([2]);
      mountComponent({ shallow: false, stubs: { MetricEmbed: true } });

      expect(wrapper.find(GlButton).text()).toBe('Hide charts');
    });
  });
});