summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/components/variables_section_spec.js
blob: 095d89c9231de9c1468ce8333ec488c2e52bfaed (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 CustomVariable from '~/monitoring/components/variables/custom_variable.vue';
import TextVariable from '~/monitoring/components/variables/text_variable.vue';
import { updateHistory, mergeUrlParams } from '~/lib/utils/url_utility';
import { createStore } from '~/monitoring/stores';
import { convertVariablesForURL } from '~/monitoring/utils';
import * as types from '~/monitoring/stores/mutation_types';
import { mockTemplatingDataResponses } 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 sampleVariables = {
    label1: mockTemplatingDataResponses.simpleText.simpleText,
    label2: mockTemplatingDataResponses.advText.advText,
    label3: mockTemplatingDataResponses.simpleCustom.simpleCustom,
  };

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

  const findTextInput = () => wrapper.findAll(TextVariable);
  const findCustomInput = () => wrapper.findAll(CustomVariable);

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

    store.state.monitoringDashboard.showEmptyState = false;
  });

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

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

  it('shows the variables section', () => {
    createShallowWrapper();
    store.commit(`monitoringDashboard/${types.SET_VARIABLES}`, sampleVariables);

    return wrapper.vm.$nextTick(() => {
      const allInputs = findTextInput().length + findCustomInput().length;

      expect(allInputs).toBe(Object.keys(sampleVariables).length);
    });
  });

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

    beforeEach(() => {
      store = new Vuex.Store({
        modules: {
          monitoringDashboard: {
            namespaced: true,
            state: {
              showEmptyState: false,
              promVariables: sampleVariables,
            },
            actions: {
              fetchDashboardData,
              updateVariableValues,
            },
          },
        },
      });

      createShallowWrapper();
    });

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

      firstInput.vm.$emit('onUpdate', 'label1', 'test');

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

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

      firstInput.vm.$emit('onUpdate', 'label1', 'test');

      return wrapper.vm.$nextTick(() => {
        expect(updateVariableValues).toHaveBeenCalled();
        expect(mergeUrlParams).toHaveBeenCalledWith(
          convertVariablesForURL(sampleVariables),
          window.location.href,
        );
        expect(updateHistory).toHaveBeenCalled();
        expect(fetchDashboardData).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 = findTextInput().at(0);

      firstInput.vm.$emit('onUpdate', 'label1', 'Simple text');

      expect(updateVariableValues).not.toHaveBeenCalled();
      expect(mergeUrlParams).not.toHaveBeenCalled();
      expect(updateHistory).not.toHaveBeenCalled();
      expect(fetchDashboardData).not.toHaveBeenCalled();
    });
  });
});