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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

409 lines
13 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { GlDropdownItem, GlSearchBoxByType, GlLoadingIcon, GlButton } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2021-03-11 19:13:27 +05:30
import { redirectTo } from '~/lib/utils/url_utility';
import ActionsMenu from '~/monitoring/components/dashboard_actions_menu.vue';
import DashboardHeader from '~/monitoring/components/dashboard_header.vue';
import DashboardsDropdown from '~/monitoring/components/dashboards_dropdown.vue';
import RefreshButton from '~/monitoring/components/refresh_button.vue';
2020-07-28 23:09:34 +05:30
import { createStore } from '~/monitoring/stores';
2020-10-24 23:57:45 +05:30
import * as types from '~/monitoring/stores/mutation_types';
import DateTimePicker from '~/vue_shared/components/date_time_picker/date_time_picker.vue';
2020-07-28 23:09:34 +05:30
import {
2020-10-24 23:57:45 +05:30
environmentData,
2020-07-28 23:09:34 +05:30
dashboardGitResponse,
selfMonitoringDashboardGitResponse,
dashboardHeaderProps,
} from '../mock_data';
2021-03-11 19:13:27 +05:30
import { setupAllDashboards, setupStoreWithDashboard, setupStoreWithData } from '../store_utils';
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
const mockProjectPath = 'https://path/to/project';
2020-07-28 23:09:34 +05:30
jest.mock('~/lib/utils/url_utility', () => ({
redirectTo: jest.fn(),
queryToObject: jest.fn(),
mergeUrlParams: jest.requireActual('~/lib/utils/url_utility').mergeUrlParams,
}));
describe('Dashboard header', () => {
let store;
let wrapper;
2022-08-27 11:52:29 +05:30
const findDashboardDropdown = () => wrapper.findComponent(DashboardsDropdown);
2020-10-24 23:57:45 +05:30
2022-08-27 11:52:29 +05:30
const findEnvsDropdown = () => wrapper.findComponent({ ref: 'monitorEnvironmentsDropdown' });
const findEnvsDropdownItems = () => findEnvsDropdown().findAllComponents(GlDropdownItem);
const findEnvsDropdownSearch = () => findEnvsDropdown().findComponent(GlSearchBoxByType);
const findEnvsDropdownSearchMsg = () =>
wrapper.findComponent({ ref: 'monitorEnvironmentsDropdownMsg' });
const findEnvsDropdownLoadingIcon = () => findEnvsDropdown().findComponent(GlLoadingIcon);
2020-10-24 23:57:45 +05:30
2022-08-27 11:52:29 +05:30
const findDateTimePicker = () => wrapper.findComponent(DateTimePicker);
const findRefreshButton = () => wrapper.findComponent(RefreshButton);
2020-10-24 23:57:45 +05:30
2022-08-27 11:52:29 +05:30
const findActionsMenu = () => wrapper.findComponent(ActionsMenu);
2020-10-24 23:57:45 +05:30
2021-03-08 18:12:59 +05:30
const setSearchTerm = (searchTerm) => {
2020-10-24 23:57:45 +05:30
store.commit(`monitoringDashboard/${types.SET_ENVIRONMENTS_FILTER}`, searchTerm);
};
2020-07-28 23:09:34 +05:30
const createShallowWrapper = (props = {}, options = {}) => {
wrapper = shallowMount(DashboardHeader, {
propsData: { ...dashboardHeaderProps, ...props },
store,
...options,
});
};
beforeEach(() => {
store = createStore();
});
afterEach(() => {
wrapper.destroy();
});
2020-10-24 23:57:45 +05:30
describe('dashboards dropdown', () => {
2020-07-28 23:09:34 +05:30
beforeEach(() => {
2020-10-24 23:57:45 +05:30
store.commit(`monitoringDashboard/${types.SET_INITIAL_STATE}`, {
projectPath: mockProjectPath,
});
createShallowWrapper();
2020-07-28 23:09:34 +05:30
});
2020-10-24 23:57:45 +05:30
it('shows the dashboard dropdown', () => {
expect(findDashboardDropdown().exists()).toBe(true);
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it('when an out of the box dashboard is selected, encodes dashboard path', () => {
findDashboardDropdown().vm.$emit('selectDashboard', {
path: '.gitlab/dashboards/dashboard&copy.yml',
out_of_the_box_dashboard: true,
display_name: 'A display name',
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
expect(redirectTo).toHaveBeenCalledWith(
`${mockProjectPath}/-/metrics/.gitlab%2Fdashboards%2Fdashboard%26copy.yml`,
);
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it('when a custom dashboard is selected, encodes dashboard display name', () => {
findDashboardDropdown().vm.$emit('selectDashboard', {
path: '.gitlab/dashboards/file&path.yml',
display_name: 'dashboard&copy.yml',
2020-07-28 23:09:34 +05:30
});
2020-10-24 23:57:45 +05:30
expect(redirectTo).toHaveBeenCalledWith(`${mockProjectPath}/-/metrics/dashboard%26copy.yml`);
2020-07-28 23:09:34 +05:30
});
});
2020-10-24 23:57:45 +05:30
describe('environments dropdown', () => {
2020-07-28 23:09:34 +05:30
beforeEach(() => {
createShallowWrapper();
});
2020-10-24 23:57:45 +05:30
it('shows the environments dropdown', () => {
expect(findEnvsDropdown().exists()).toBe(true);
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it('renders a search input', () => {
expect(findEnvsDropdownSearch().exists()).toBe(true);
});
describe('when environments data is not loaded', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2020-10-24 23:57:45 +05:30
setupStoreWithDashboard(store);
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
});
it('there are no environments listed', () => {
expect(findEnvsDropdownItems()).toHaveLength(0);
});
});
describe('when environments data is loaded', () => {
const currentDashboard = dashboardGitResponse[0].path;
const currentEnvironmentName = environmentData[0].name;
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2020-10-24 23:57:45 +05:30
setupStoreWithData(store);
store.state.monitoringDashboard.projectPath = mockProjectPath;
store.state.monitoringDashboard.currentDashboard = currentDashboard;
store.state.monitoringDashboard.currentEnvironmentName = currentEnvironmentName;
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
});
it('renders dropdown items with the environment name', () => {
const path = `${mockProjectPath}/-/metrics/${encodeURIComponent(currentDashboard)}`;
findEnvsDropdownItems().wrappers.forEach((itemWrapper, index) => {
const { name, id } = environmentData[index];
const idParam = encodeURIComponent(id);
expect(itemWrapper.text()).toBe(name);
expect(itemWrapper.attributes('href')).toBe(`${path}?environment=${idParam}`);
});
});
it('environments dropdown items can be checked', () => {
const items = findEnvsDropdownItems();
2021-03-08 18:12:59 +05:30
const checkItems = findEnvsDropdownItems().filter((item) => item.props('isCheckItem'));
2020-10-24 23:57:45 +05:30
expect(items).toHaveLength(checkItems.length);
});
it('checks the currently selected environment', () => {
2021-03-08 18:12:59 +05:30
const selectedItems = findEnvsDropdownItems().filter((item) => item.props('isChecked'));
2020-10-24 23:57:45 +05:30
expect(selectedItems).toHaveLength(1);
expect(selectedItems.at(0).text()).toBe(currentEnvironmentName);
});
2022-04-04 11:22:00 +05:30
it('filters rendered dropdown items', async () => {
2020-10-24 23:57:45 +05:30
const searchTerm = 'production';
const resultEnvs = environmentData.filter(({ name }) => name.indexOf(searchTerm) !== -1);
setSearchTerm(searchTerm);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findEnvsDropdownItems()).toHaveLength(resultEnvs.length);
2020-10-24 23:57:45 +05:30
});
2022-04-04 11:22:00 +05:30
it('does not filter dropdown items if search term is empty string', async () => {
2020-10-24 23:57:45 +05:30
const searchTerm = '';
setSearchTerm(searchTerm);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findEnvsDropdownItems()).toHaveLength(environmentData.length);
2020-10-24 23:57:45 +05:30
});
2022-04-04 11:22:00 +05:30
it("shows error message if search term doesn't match", async () => {
2020-10-24 23:57:45 +05:30
const searchTerm = 'does-not-exist';
setSearchTerm(searchTerm);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findEnvsDropdownSearchMsg().isVisible()).toBe(true);
2020-10-24 23:57:45 +05:30
});
2022-04-04 11:22:00 +05:30
it('shows loading element when environments fetch is still loading', async () => {
2020-10-24 23:57:45 +05:30
store.commit(`monitoringDashboard/${types.REQUEST_ENVIRONMENTS_DATA}`);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findEnvsDropdownLoadingIcon().exists()).toBe(true);
await store.commit(
`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`,
environmentData,
);
expect(findEnvsDropdownLoadingIcon().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
});
});
2020-10-24 23:57:45 +05:30
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
describe('date time picker', () => {
beforeEach(() => {
createShallowWrapper();
2020-07-28 23:09:34 +05:30
});
2020-10-24 23:57:45 +05:30
it('is rendered', () => {
expect(findDateTimePicker().exists()).toBe(true);
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
describe('timezone setting', () => {
2021-03-08 18:12:59 +05:30
const setupWithTimezone = (value) => {
2020-10-24 23:57:45 +05:30
store = createStore({ dashboardTimezone: value });
createShallowWrapper();
};
describe('local timezone is enabled by default', () => {
it('shows the data time picker in local timezone', () => {
expect(findDateTimePicker().props('utc')).toBe(false);
});
});
describe('when LOCAL timezone is enabled', () => {
beforeEach(() => {
setupWithTimezone('LOCAL');
});
it('shows the data time picker in local timezone', () => {
expect(findDateTimePicker().props('utc')).toBe(false);
});
});
describe('when UTC timezone is enabled', () => {
beforeEach(() => {
setupWithTimezone('UTC');
});
it('shows the data time picker in UTC format', () => {
expect(findDateTimePicker().props('utc')).toBe(true);
});
2020-07-28 23:09:34 +05:30
});
});
2020-10-24 23:57:45 +05:30
});
describe('refresh button', () => {
beforeEach(() => {
createShallowWrapper();
});
it('is rendered', () => {
expect(findRefreshButton().exists()).toBe(true);
});
});
describe('external dashboard link', () => {
2022-04-04 11:22:00 +05:30
beforeEach(async () => {
2020-10-24 23:57:45 +05:30
store.state.monitoringDashboard.externalDashboardUrl = '/mockUrl';
createShallowWrapper();
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
});
it('shows the link', () => {
const externalDashboardButton = wrapper.find('.js-external-dashboard-link');
expect(externalDashboardButton.exists()).toBe(true);
expect(externalDashboardButton.is(GlButton)).toBe(true);
expect(externalDashboardButton.text()).toContain('View full dashboard');
});
});
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
describe('actions menu', () => {
const ootbDashboards = [
2020-07-28 23:09:34 +05:30
dashboardGitResponse[0].path,
selfMonitoringDashboardGitResponse[0].path,
];
2020-10-24 23:57:45 +05:30
const customDashboards = [
dashboardGitResponse[1].path,
selfMonitoringDashboardGitResponse[1].path,
];
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it('is rendered', () => {
createShallowWrapper();
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
expect(findActionsMenu().exists()).toBe(true);
});
describe('adding metrics prop', () => {
2022-04-04 11:22:00 +05:30
it.each(ootbDashboards)(
'gets passed true if current dashboard is OOTB',
async (dashboardPath) => {
createShallowWrapper({ customMetricsAvailable: true });
2020-10-24 23:57:45 +05:30
2022-04-04 11:22:00 +05:30
store.state.monitoringDashboard.emptyState = false;
setupAllDashboards(store, dashboardPath);
2020-10-24 23:57:45 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
2020-10-24 23:57:45 +05:30
expect(findActionsMenu().props('addingMetricsAvailable')).toBe(true);
2022-04-04 11:22:00 +05:30
},
);
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
it.each(customDashboards)(
'gets passed false if current dashboard is custom',
2022-04-04 11:22:00 +05:30
async (dashboardPath) => {
2020-10-24 23:57:45 +05:30
createShallowWrapper({ customMetricsAvailable: true });
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
store.state.monitoringDashboard.emptyState = false;
2020-07-28 23:09:34 +05:30
setupAllDashboards(store, dashboardPath);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('addingMetricsAvailable')).toBe(false);
2020-10-24 23:57:45 +05:30
},
);
2022-04-04 11:22:00 +05:30
it('gets passed false if empty state is shown', async () => {
2020-10-24 23:57:45 +05:30
createShallowWrapper({ customMetricsAvailable: true });
store.state.monitoringDashboard.emptyState = true;
setupAllDashboards(store, ootbDashboards[0]);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('addingMetricsAvailable')).toBe(false);
2020-10-24 23:57:45 +05:30
});
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
it('gets passed false if custom metrics are not available', async () => {
2020-10-24 23:57:45 +05:30
createShallowWrapper({ customMetricsAvailable: false });
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
store.state.monitoringDashboard.emptyState = false;
setupAllDashboards(store, ootbDashboards[0]);
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('addingMetricsAvailable')).toBe(false);
2020-10-24 23:57:45 +05:30
});
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('custom metrics path gets passed', async () => {
2020-10-24 23:57:45 +05:30
const path = 'https://path/to/customMetrics';
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
createShallowWrapper({ customMetricsPath: path });
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('customMetricsPath')).toBe(path);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('validate query path gets passed', async () => {
2020-10-24 23:57:45 +05:30
const path = 'https://path/to/validateQuery';
createShallowWrapper({ validateQueryPath: path });
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('validateQueryPath')).toBe(path);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('default branch gets passed', async () => {
2020-10-24 23:57:45 +05:30
const branch = 'branchName';
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
createShallowWrapper({ defaultBranch: branch });
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findActionsMenu().props('defaultBranch')).toBe(branch);
2020-07-28 23:09:34 +05:30
});
});
describe('metrics settings button', () => {
const findSettingsButton = () => wrapper.find('[data-testid="metrics-settings-button"]');
const url = 'https://path/to/project/settings';
beforeEach(() => {
createShallowWrapper();
store.state.monitoringDashboard.canAccessOperationsSettings = false;
store.state.monitoringDashboard.operationsSettingsPath = '';
});
2022-04-04 11:22:00 +05:30
it('is rendered when the user can access the project settings and path to settings is available', async () => {
2020-07-28 23:09:34 +05:30
store.state.monitoringDashboard.canAccessOperationsSettings = true;
store.state.monitoringDashboard.operationsSettingsPath = url;
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findSettingsButton().exists()).toBe(true);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('is not rendered when the user can not access the project settings', async () => {
2020-07-28 23:09:34 +05:30
store.state.monitoringDashboard.canAccessOperationsSettings = false;
store.state.monitoringDashboard.operationsSettingsPath = url;
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findSettingsButton().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('is not rendered when the path to settings is unavailable', async () => {
2020-07-28 23:09:34 +05:30
store.state.monitoringDashboard.canAccessOperationsSettings = false;
store.state.monitoringDashboard.operationsSettingsPath = '';
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findSettingsButton().exists()).toBe(false);
2020-07-28 23:09:34 +05:30
});
2022-04-04 11:22:00 +05:30
it('leads to the project settings page', async () => {
2020-07-28 23:09:34 +05:30
store.state.monitoringDashboard.canAccessOperationsSettings = true;
store.state.monitoringDashboard.operationsSettingsPath = url;
2022-04-04 11:22:00 +05:30
await nextTick();
expect(findSettingsButton().attributes('href')).toBe(url);
2020-07-28 23:09:34 +05:30
});
});
});