debian-mirror-gitlab/spec/javascripts/monitoring/dashboard_spec.js

589 lines
17 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Vue from 'vue';
2019-10-12 21:52:04 +05:30
import { shallowMount, createLocalVue } from '@vue/test-utils';
import { GlToast } from '@gitlab/ui';
2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
import Dashboard from '~/monitoring/components/dashboard.vue';
2019-07-31 22:56:46 +05:30
import { timeWindows, timeWindowsKeyNames } from '~/monitoring/constants';
2019-09-04 21:01:54 +05:30
import * as types from '~/monitoring/stores/mutation_types';
import { createStore } from '~/monitoring/stores';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
2019-10-12 21:52:04 +05:30
import MonitoringMock, {
2019-09-04 21:01:54 +05:30
metricsGroupsAPIResponse,
mockApiEndpoint,
environmentData,
singleGroupResponse,
2019-09-30 21:07:59 +05:30
dashboardGitResponse,
2019-09-04 21:01:54 +05:30
} from './mock_data';
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
const localVue = createLocalVue();
2018-12-05 23:21:45 +05:30
const propsData = {
hasMetrics: false,
documentationPath: '/path/to/docs',
settingsPath: '/path/to/settings',
clustersPath: '/path/to/clusters',
tagsPath: '/path/to/tags',
projectPath: '/path/to/project',
metricsEndpoint: mockApiEndpoint,
2019-09-04 21:01:54 +05:30
deploymentsEndpoint: null,
2018-12-05 23:21:45 +05:30
emptyGettingStartedSvgPath: '/path/to/getting-started.svg',
emptyLoadingSvgPath: '/path/to/loading.svg',
emptyNoDataSvgPath: '/path/to/no-data.svg',
emptyUnableToConnectSvgPath: '/path/to/unable-to-connect.svg',
environmentsEndpoint: '/root/hello-prometheus/environments/35',
currentEnvironmentName: 'production',
2019-07-31 22:56:46 +05:30
customMetricsAvailable: false,
customMetricsPath: '',
validateQueryPath: '',
2018-12-05 23:21:45 +05:30
};
export default propsData;
2018-03-17 18:26:18 +05:30
describe('Dashboard', () => {
let DashboardComponent;
2019-03-02 22:35:43 +05:30
let mock;
2019-09-04 21:01:54 +05:30
let store;
let component;
2019-10-12 21:52:04 +05:30
let mockGraphData;
2018-03-27 19:54:05 +05:30
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2018-12-05 23:21:45 +05:30
setFixtures(`
<div class="prometheus-graphs"></div>
2019-03-02 22:35:43 +05:30
<div class="layout-page"></div>
2018-12-05 23:21:45 +05:30
`);
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
window.gon = {
...window.gon,
ee: false,
};
2019-09-04 21:01:54 +05:30
store = createStore();
2019-03-02 22:35:43 +05:30
mock = new MockAdapter(axios);
2018-03-17 18:26:18 +05:30
DashboardComponent = Vue.extend(Dashboard);
2017-09-10 17:25:29 +05:30
});
2019-03-02 22:35:43 +05:30
afterEach(() => {
2019-10-12 21:52:04 +05:30
if (component) {
component.$destroy();
}
2019-03-02 22:35:43 +05:30
mock.restore();
});
2017-09-10 17:25:29 +05:30
describe('no metrics are available yet', () => {
2019-09-30 21:07:59 +05:30
beforeEach(() => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-03-27 19:54:05 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-09-04 21:01:54 +05:30
propsData: { ...propsData },
store,
2017-09-10 17:25:29 +05:30
});
2019-09-30 21:07:59 +05:30
});
2017-09-10 17:25:29 +05:30
2019-09-30 21:07:59 +05:30
it('shows a getting started empty state when no metrics are present', () => {
2018-03-27 19:54:05 +05:30
expect(component.$el.querySelector('.prometheus-graphs')).toBe(null);
2019-09-04 21:01:54 +05:30
expect(component.emptyState).toEqual('gettingStarted');
2017-09-10 17:25:29 +05:30
});
2019-09-30 21:07:59 +05:30
it('shows the environment selector', () => {
expect(component.$el.querySelector('.js-environments-dropdown')).toBeTruthy();
});
});
describe('no data found', () => {
it('shows the environment selector dropdown', () => {
component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: { ...propsData, showEmptyState: true },
store,
});
expect(component.$el.querySelector('.js-environments-dropdown')).toBeTruthy();
});
2017-09-10 17:25:29 +05:30
});
describe('requests information to the server', () => {
beforeEach(() => {
2018-03-27 19:54:05 +05:30
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
2017-09-10 17:25:29 +05:30
});
2018-11-08 19:23:39 +05:30
it('shows up a loading state', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-03-27 19:54:05 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-09-04 21:01:54 +05:30
propsData: { ...propsData, hasMetrics: true },
store,
2017-09-10 17:25:29 +05:30
});
2018-03-27 19:54:05 +05:30
2017-09-10 17:25:29 +05:30
Vue.nextTick(() => {
2019-09-04 21:01:54 +05:30
expect(component.emptyState).toEqual('loading');
2017-09-10 17:25:29 +05:30
done();
});
});
2018-03-27 19:54:05 +05:30
2018-11-08 19:23:39 +05:30
it('hides the legend when showLegend is false', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-03-27 19:54:05 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showLegend: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-03-27 19:54:05 +05:30
});
setTimeout(() => {
expect(component.showEmptyState).toEqual(false);
expect(component.$el.querySelector('.legend-group')).toEqual(null);
expect(component.$el.querySelector('.prometheus-graph-group')).toBeTruthy();
done();
});
});
2018-11-08 19:23:39 +05:30
it('hides the group panels when showPanels is false', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-03-27 19:54:05 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-03-27 19:54:05 +05:30
});
setTimeout(() => {
expect(component.showEmptyState).toEqual(false);
expect(component.$el.querySelector('.prometheus-panel')).toEqual(null);
expect(component.$el.querySelector('.prometheus-graph-group')).toBeTruthy();
done();
});
});
2018-11-08 19:23:39 +05:30
2019-07-07 11:18:12 +05:30
it('renders the environments dropdown with a number of environments', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-11-08 19:23:39 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-11-08 19:23:39 +05:30
});
2019-09-04 21:01:54 +05:30
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`,
environmentData,
);
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_METRICS_DATA_SUCCESS}`,
singleGroupResponse,
);
2018-11-08 19:23:39 +05:30
2019-09-30 21:07:59 +05:30
Vue.nextTick()
.then(() => {
const dropdownMenuEnvironments = component.$el.querySelectorAll(
'.js-environments-dropdown .dropdown-item',
);
2018-12-13 13:39:08 +05:30
2019-09-30 21:07:59 +05:30
expect(component.environments.length).toEqual(environmentData.length);
expect(dropdownMenuEnvironments.length).toEqual(component.environments.length);
Array.from(dropdownMenuEnvironments).forEach((value, index) => {
if (environmentData[index].metrics_path) {
expect(value).toHaveAttr('href', environmentData[index].metrics_path);
}
});
done();
})
.catch(done.fail);
2018-11-08 19:23:39 +05:30
});
2019-07-07 11:18:12 +05:30
it('hides the environments dropdown list when there is no environments', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-12-13 13:39:08 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-12-13 13:39:08 +05:30
});
2019-09-04 21:01:54 +05:30
component.$store.commit(`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`, []);
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_METRICS_DATA_SUCCESS}`,
singleGroupResponse,
);
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
Vue.nextTick()
.then(() => {
const dropdownMenuEnvironments = component.$el.querySelectorAll(
'.js-environments-dropdown .dropdown-item',
);
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
expect(dropdownMenuEnvironments.length).toEqual(0);
done();
})
.catch(done.fail);
2018-12-13 13:39:08 +05:30
});
2019-07-31 22:56:46 +05:30
it('renders the environments dropdown with a single active element', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-11-08 19:23:39 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-11-08 19:23:39 +05:30
});
2019-09-04 21:01:54 +05:30
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`,
environmentData,
);
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_METRICS_DATA_SUCCESS}`,
singleGroupResponse,
);
2018-11-08 19:23:39 +05:30
2019-09-04 21:01:54 +05:30
Vue.nextTick()
.then(() => {
const dropdownItems = component.$el.querySelectorAll(
2019-09-30 21:07:59 +05:30
'.js-environments-dropdown .dropdown-item.active',
2019-09-04 21:01:54 +05:30
);
2018-12-13 13:39:08 +05:30
2019-09-04 21:01:54 +05:30
expect(dropdownItems.length).toEqual(1);
done();
})
.catch(done.fail);
2019-07-07 11:18:12 +05:30
});
it('hides the dropdown', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2019-07-07 11:18:12 +05:30
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
environmentsEndpoint: '',
},
2019-09-04 21:01:54 +05:30
store,
2019-07-07 11:18:12 +05:30
});
Vue.nextTick(() => {
const dropdownIsActiveElement = component.$el.querySelectorAll('.environments');
expect(dropdownIsActiveElement.length).toEqual(0);
done();
});
});
2019-09-04 21:01:54 +05:30
it('renders the time window dropdown with a set of options', done => {
component = new DashboardComponent({
2019-07-07 11:18:12 +05:30
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2019-07-07 11:18:12 +05:30
});
2019-09-04 21:01:54 +05:30
const numberOfTimeWindows = Object.keys(timeWindows).length;
2019-07-07 11:18:12 +05:30
setTimeout(() => {
const timeWindowDropdown = component.$el.querySelector('.js-time-window-dropdown');
2019-09-04 21:01:54 +05:30
const timeWindowDropdownEls = component.$el.querySelectorAll(
'.js-time-window-dropdown .dropdown-item',
);
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
expect(timeWindowDropdown).not.toBeNull();
expect(timeWindowDropdownEls.length).toEqual(numberOfTimeWindows);
2019-07-07 11:18:12 +05:30
done();
});
});
2019-09-04 21:01:54 +05:30
it('fetches the metrics data with proper time window', done => {
component = new DashboardComponent({
2019-07-07 11:18:12 +05:30
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2019-07-07 11:18:12 +05:30
});
2019-09-04 21:01:54 +05:30
spyOn(component.$store, 'dispatch').and.stub();
2019-10-12 21:52:04 +05:30
const getTimeDiffSpy = spyOnDependency(Dashboard, 'getTimeDiff').and.callThrough();
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`,
environmentData,
);
2019-07-07 11:18:12 +05:30
2019-09-04 21:01:54 +05:30
component.$mount();
Vue.nextTick()
.then(() => {
expect(component.$store.dispatch).toHaveBeenCalled();
2019-10-12 21:52:04 +05:30
expect(getTimeDiffSpy).toHaveBeenCalled();
2019-09-04 21:01:54 +05:30
done();
})
.catch(done.fail);
2018-11-08 19:23:39 +05:30
});
2019-07-31 22:56:46 +05:30
it('shows a specific time window selected from the url params', done => {
2019-10-12 21:52:04 +05:30
const start = 1564439536;
const end = 1564441336;
spyOnDependency(Dashboard, 'getTimeDiff').and.returnValue({
start,
end,
});
spyOnDependency(Dashboard, 'getParameterValues').and.callFake(param => {
if (param === 'start') return [start];
if (param === 'end') return [end];
return [];
});
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2019-07-31 22:56:46 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-09-04 21:01:54 +05:30
propsData: { ...propsData, hasMetrics: true },
store,
2019-07-31 22:56:46 +05:30
});
setTimeout(() => {
2019-09-04 21:01:54 +05:30
const selectedTimeWindow = component.$el.querySelector('.js-time-window-dropdown .active');
2019-07-31 22:56:46 +05:30
expect(selectedTimeWindow.textContent.trim()).toEqual('30 minutes');
done();
});
});
it('defaults to the eight hours time window for non valid url parameters', done => {
spyOnDependency(Dashboard, 'getParameterValues').and.returnValue([
'<script>alert("XSS")</script>',
]);
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2019-07-31 22:56:46 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-09-04 21:01:54 +05:30
propsData: { ...propsData, hasMetrics: true },
store,
2019-07-31 22:56:46 +05:30
});
Vue.nextTick(() => {
expect(component.selectedTimeWindowKey).toEqual(timeWindowsKeyNames.eightHours);
done();
});
});
2017-09-10 17:25:29 +05:30
});
2018-12-05 23:21:45 +05:30
2019-10-12 21:52:04 +05:30
describe('link to chart', () => {
let wrapper;
const currentDashboard = 'TEST_DASHBOARD';
localVue.use(GlToast);
const link = () => wrapper.find('.js-chart-link');
const clipboardText = () => link().element.dataset.clipboardText;
beforeEach(done => {
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
wrapper = shallowMount(DashboardComponent, {
localVue,
sync: false,
attachToDocument: true,
propsData: { ...propsData, hasMetrics: true, currentDashboard },
store,
});
setTimeout(done);
});
afterEach(() => {
wrapper.destroy();
});
it('adds a copy button to the dropdown', () => {
expect(link().text()).toContain('Generate link to chart');
});
it('contains a link to the dashboard', () => {
expect(clipboardText()).toContain(`dashboard=${currentDashboard}`);
expect(clipboardText()).toContain(`group=`);
expect(clipboardText()).toContain(`title=`);
expect(clipboardText()).toContain(`y_label=`);
});
it('undefined parameter is stripped', done => {
wrapper.setProps({ currentDashboard: undefined });
wrapper.vm.$nextTick(() => {
expect(clipboardText()).not.toContain(`dashboard=`);
expect(clipboardText()).toContain(`y_label=`);
done();
});
});
it('null parameter is stripped', done => {
wrapper.setProps({ currentDashboard: null });
wrapper.vm.$nextTick(() => {
expect(clipboardText()).not.toContain(`dashboard=`);
expect(clipboardText()).toContain(`y_label=`);
done();
});
});
it('creates a toast when clicked', () => {
spyOn(wrapper.vm.$toast, 'show').and.stub();
link().vm.$emit('click');
expect(wrapper.vm.$toast.show).toHaveBeenCalled();
});
});
2018-12-05 23:21:45 +05:30
describe('when the window resizes', () => {
beforeEach(() => {
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
jasmine.clock().install();
});
afterEach(() => {
jasmine.clock().uninstall();
});
2019-03-02 22:35:43 +05:30
it('sets elWidth to page width when the sidebar is resized', done => {
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
2018-12-05 23:21:45 +05:30
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
2019-09-04 21:01:54 +05:30
store,
2018-12-05 23:21:45 +05:30
});
2019-03-02 22:35:43 +05:30
expect(component.elWidth).toEqual(0);
2018-12-05 23:21:45 +05:30
2019-03-02 22:35:43 +05:30
const pageLayoutEl = document.querySelector('.layout-page');
pageLayoutEl.classList.add('page-with-icon-sidebar');
2018-12-05 23:21:45 +05:30
Vue.nextTick()
.then(() => {
jasmine.clock().tick(1000);
return Vue.nextTick();
})
.then(() => {
2019-03-02 22:35:43 +05:30
expect(component.elWidth).toEqual(pageLayoutEl.clientWidth);
2018-12-05 23:21:45 +05:30
done();
})
.catch(done.fail);
});
});
2019-07-31 22:56:46 +05:30
describe('external dashboard link', () => {
beforeEach(() => {
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
2019-09-04 21:01:54 +05:30
component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
externalDashboardUrl: '/mockUrl',
},
store,
2019-07-31 22:56:46 +05:30
});
});
2019-09-04 21:01:54 +05:30
it('shows the link', done => {
setTimeout(() => {
expect(component.$el.querySelector('.js-external-dashboard-link').innerText).toContain(
'View full dashboard',
);
done();
2019-07-31 22:56:46 +05:30
});
});
});
2019-09-30 21:07:59 +05:30
describe('Dashboard dropdown', () => {
beforeEach(() => {
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
},
store,
});
component.$store.dispatch('monitoringDashboard/setFeatureFlags', {
prometheusEndpoint: false,
multipleDashboardsEnabled: true,
});
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS}`,
environmentData,
);
component.$store.commit(
`monitoringDashboard/${types.RECEIVE_METRICS_DATA_SUCCESS}`,
singleGroupResponse,
);
component.$store.commit(
`monitoringDashboard/${types.SET_ALL_DASHBOARDS}`,
dashboardGitResponse,
);
});
it('shows the dashboard dropdown', done => {
setTimeout(() => {
const dashboardDropdown = component.$el.querySelector('.js-dashboards-dropdown');
expect(dashboardDropdown).not.toEqual(null);
done();
});
});
});
2019-10-12 21:52:04 +05:30
describe('when downloading metrics data as CSV', () => {
beforeEach(() => {
component = new DashboardComponent({
propsData: {
...propsData,
},
store,
});
store.commit(
`monitoringDashboard/${types.RECEIVE_METRICS_DATA_SUCCESS}`,
MonitoringMock.data,
);
[mockGraphData] = component.$store.state.monitoringDashboard.groups[0].metrics;
});
describe('csvText', () => {
it('converts metrics data from json to csv', () => {
const header = `timestamp,${mockGraphData.y_label}`;
const data = mockGraphData.queries[0].result[0].values;
const firstRow = `${data[0][0]},${data[0][1]}`;
expect(component.csvText(mockGraphData)).toMatch(`^${header}\r\n${firstRow}`);
});
});
describe('downloadCsv', () => {
it('produces a link with a Blob', () => {
expect(component.downloadCsv(mockGraphData)).toContain(`blob:`);
});
});
});
2017-09-10 17:25:29 +05:30
});