summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/variables_section_spec.js
blob: 3097906ee68f8f82f4f0c6354a7ca04a051b7806 (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
import { shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
import VariablesSection from '~/monitoring/components/variables_section.vue';
import DropdownField from '~/monitoring/components/variables/dropdown_field.vue';
import TextField from '~/monitoring/components/variables/text_field.vue';
import { updateHistory, mergeUrlParams } from '~/lib/utils/url_utility';
import { createStore } from '~/monitoring/stores';
import { convertVariablesForURL } from '~/monitoring/utils';
import { storeVariables } from '../mock_data';

jest.mock('~/lib/utils/url_utility', () => ({
  updateHistory: jest.fn(),
  mergeUrlParams: jest.fn(),
}));

describe('Metrics dashboard/variables section component', () => {
  let store;
  let wrapper;

  const createShallowWrapper = () => {
    wrapper = shallowMount(VariablesSection, {
      store,
    });
  };

  const findTextInputs = () => wrapper.findAll(TextField);
  const findCustomInputs = () => wrapper.findAll(DropdownField);

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

    store.state.monitoringDashboard.emptyState = null;
  });

  it('does not show the variables section', () => {
    createShallowWrapper();
    const allInputs = findTextInputs().length + findCustomInputs().length;

    expect(allInputs).toBe(0);
  });

  describe('when variables are set', () => {
    beforeEach(() => {
      store.state.monitoringDashboard.variables = storeVariables;
      createShallowWrapper();

      return wrapper.vm.$nextTick;
    });

    it('shows the variables section', () => {
      const allInputs = findTextInputs().length + findCustomInputs().length;

      expect(allInputs).toBe(storeVariables.length);
    });

    it('shows the right custom variable inputs', () => {
      const customInputs = findCustomInputs();

      expect(customInputs.at(0).props('name')).toBe('customSimple');
      expect(customInputs.at(1).props('name')).toBe('customAdvanced');
    });
  });

  describe('when changing the variable inputs', () => {
    const updateVariablesAndFetchData = jest.fn();

    beforeEach(() => {
      store = new Vuex.Store({
        modules: {
          monitoringDashboard: {
            namespaced: true,
            state: {
              emptyState: null,
              variables: storeVariables,
            },
            actions: {
              updateVariablesAndFetchData,
            },
          },
        },
      });

      createShallowWrapper();
    });

    it('merges the url params and refreshes the dashboard when a text-based variables inputs are updated', () => {
      const firstInput = findTextInputs().at(0);

      firstInput.vm.$emit('input', 'test');

      return wrapper.vm.$nextTick(() => {
        expect(updateVariablesAndFetchData).toHaveBeenCalled();
        expect(mergeUrlParams).toHaveBeenCalledWith(
          convertVariablesForURL(storeVariables),
          window.location.href,
        );
        expect(updateHistory).toHaveBeenCalled();
      });
    });

    it('merges the url params and refreshes the dashboard when a custom-based variables inputs are updated', () => {
      const firstInput = findCustomInputs().at(0);

      firstInput.vm.$emit('input', 'test');

      return wrapper.vm.$nextTick(() => {
        expect(updateVariablesAndFetchData).toHaveBeenCalled();
        expect(mergeUrlParams).toHaveBeenCalledWith(
          convertVariablesForURL(storeVariables),
          window.location.href,
        );
        expect(updateHistory).toHaveBeenCalled();
      });
    });

    it('does not merge the url params and refreshes the dashboard if the value entered is not different that is what currently stored', () => {
      const firstInput = findTextInputs().at(0);

      firstInput.vm.$emit('input', 'My default value');

      expect(updateVariablesAndFetchData).not.toHaveBeenCalled();
      expect(mergeUrlParams).not.toHaveBeenCalled();
      expect(updateHistory).not.toHaveBeenCalled();
    });
  });
});