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

476 lines
13 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Vue from 'vue';
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-09-04 21:01:54 +05:30
import {
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
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;
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-09-04 21:01:54 +05:30
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();
const getTimeDiffSpy = spyOnDependency(Dashboard, 'getTimeDiff');
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();
expect(getTimeDiffSpy).toHaveBeenCalledWith(component.selectedTimeWindow);
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 => {
spyOnDependency(Dashboard, 'getParameterValues').and.returnValue(['thirtyMinutes']);
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
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();
});
});
});
2017-09-10 17:25:29 +05:30
});