debian-mirror-gitlab/spec/frontend/monitoring/components/variables_section_spec.js

127 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
2021-03-11 19:13:27 +05:30
import { updateHistory, mergeUrlParams } from '~/lib/utils/url_utility';
2020-07-28 23:09:34 +05:30
import DropdownField from '~/monitoring/components/variables/dropdown_field.vue';
import TextField from '~/monitoring/components/variables/text_field.vue';
2021-03-11 19:13:27 +05:30
import VariablesSection from '~/monitoring/components/variables_section.vue';
2020-05-24 23:13:21 +05:30
import { createStore } from '~/monitoring/stores';
import { convertVariablesForURL } from '~/monitoring/utils';
2020-07-28 23:09:34 +05:30
import { storeVariables } from '../mock_data';
2020-05-24 23:13:21 +05:30
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,
});
};
2020-07-28 23:09:34 +05:30
const findTextInputs = () => wrapper.findAll(TextField);
const findCustomInputs = () => wrapper.findAll(DropdownField);
2020-05-24 23:13:21 +05:30
beforeEach(() => {
store = createStore();
2020-07-28 23:09:34 +05:30
store.state.monitoringDashboard.emptyState = null;
2020-05-24 23:13:21 +05:30
});
it('does not show the variables section', () => {
createShallowWrapper();
2020-07-28 23:09:34 +05:30
const allInputs = findTextInputs().length + findCustomInputs().length;
2020-05-24 23:13:21 +05:30
expect(allInputs).toBe(0);
});
2020-07-28 23:09:34 +05:30
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);
});
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
it('shows the right custom variable inputs', () => {
const customInputs = findCustomInputs();
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
expect(customInputs.at(0).props('name')).toBe('customSimple');
expect(customInputs.at(1).props('name')).toBe('customAdvanced');
2020-05-24 23:13:21 +05:30
});
});
describe('when changing the variable inputs', () => {
2020-06-23 00:09:42 +05:30
const updateVariablesAndFetchData = jest.fn();
2020-05-24 23:13:21 +05:30
beforeEach(() => {
store = new Vuex.Store({
modules: {
monitoringDashboard: {
namespaced: true,
state: {
2020-07-28 23:09:34 +05:30
emptyState: null,
variables: storeVariables,
2020-05-24 23:13:21 +05:30
},
actions: {
2020-06-23 00:09:42 +05:30
updateVariablesAndFetchData,
2020-05-24 23:13:21 +05:30
},
},
},
});
createShallowWrapper();
});
it('merges the url params and refreshes the dashboard when a text-based variables inputs are updated', () => {
2020-07-28 23:09:34 +05:30
const firstInput = findTextInputs().at(0);
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
firstInput.vm.$emit('input', 'test');
2020-05-24 23:13:21 +05:30
return wrapper.vm.$nextTick(() => {
2020-06-23 00:09:42 +05:30
expect(updateVariablesAndFetchData).toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
expect(mergeUrlParams).toHaveBeenCalledWith(
2020-07-28 23:09:34 +05:30
convertVariablesForURL(storeVariables),
2020-05-24 23:13:21 +05:30
window.location.href,
);
expect(updateHistory).toHaveBeenCalled();
});
});
it('merges the url params and refreshes the dashboard when a custom-based variables inputs are updated', () => {
2020-07-28 23:09:34 +05:30
const firstInput = findCustomInputs().at(0);
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
firstInput.vm.$emit('input', 'test');
2020-05-24 23:13:21 +05:30
return wrapper.vm.$nextTick(() => {
2020-06-23 00:09:42 +05:30
expect(updateVariablesAndFetchData).toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
expect(mergeUrlParams).toHaveBeenCalledWith(
2020-07-28 23:09:34 +05:30
convertVariablesForURL(storeVariables),
2020-05-24 23:13:21 +05:30
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', () => {
2020-07-28 23:09:34 +05:30
const firstInput = findTextInputs().at(0);
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
firstInput.vm.$emit('input', 'My default value');
2020-05-24 23:13:21 +05:30
2020-06-23 00:09:42 +05:30
expect(updateVariablesAndFetchData).not.toHaveBeenCalled();
2020-05-24 23:13:21 +05:30
expect(mergeUrlParams).not.toHaveBeenCalled();
expect(updateHistory).not.toHaveBeenCalled();
});
});
});