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

392 lines
11 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';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
2018-11-08 19:23:39 +05:30
import { metricsGroupsAPIResponse, mockApiEndpoint, environmentData } 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,
deploymentEndpoint: null,
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;
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-07-31 22:56:46 +05:30
features: {
grafanaDashboardLink: true,
},
2019-07-07 11:18:12 +05:30
};
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(() => {
mock.restore();
});
2017-09-10 17:25:29 +05:30
describe('no metrics are available yet', () => {
it('shows a getting started empty state when no metrics are present', () => {
2018-03-27 19:54:05 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: { ...propsData, showTimeWindowDropdown: false },
2017-09-10 17:25:29 +05:30
});
2018-03-27 19:54:05 +05:30
expect(component.$el.querySelector('.prometheus-graphs')).toBe(null);
2017-09-10 17:25:29 +05:30
expect(component.state).toEqual('gettingStarted');
});
});
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 => {
2018-03-27 19:54:05 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: { ...propsData, hasMetrics: true, showTimeWindowDropdown: false },
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(() => {
expect(component.state).toEqual('loading');
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 => {
2018-03-27 19:54:05 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showLegend: false,
showTimeWindowDropdown: false,
},
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 => {
2018-03-27 19:54:05 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
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 => {
2018-11-08 19:23:39 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
2018-11-08 19:23:39 +05:30
});
component.store.storeEnvironmentsData(environmentData);
setTimeout(() => {
2019-07-07 11:18:12 +05:30
const dropdownMenuEnvironments = component.$el.querySelectorAll(
'.js-environments-dropdown .dropdown-item',
);
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(dropdownMenuEnvironments.length).toEqual(component.store.environmentsData.length);
done();
});
});
2019-07-07 11:18:12 +05:30
it('hides the environments dropdown list when there is no environments', done => {
2018-12-13 13:39:08 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
2018-12-13 13:39:08 +05:30
});
component.store.storeEnvironmentsData([]);
setTimeout(() => {
2019-07-07 11:18:12 +05:30
const dropdownMenuEnvironments = component.$el.querySelectorAll(
'.js-environments-dropdown .dropdown-item',
);
2018-12-13 13:39:08 +05:30
expect(dropdownMenuEnvironments.length).toEqual(0);
done();
});
});
2019-07-31 22:56:46 +05:30
it('renders the environments dropdown with a single active element', done => {
2018-11-08 19:23:39 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
2018-11-08 19:23:39 +05:30
});
component.store.storeEnvironmentsData(environmentData);
setTimeout(() => {
2019-07-07 11:18:12 +05:30
const dropdownItems = component.$el.querySelectorAll(
2019-07-31 22:56:46 +05:30
'.js-environments-dropdown .dropdown-item[active="true"]',
2018-11-08 19:23:39 +05:30
);
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
expect(dropdownItems.length).toEqual(1);
expect(dropdownItems[0].textContent.trim()).toEqual(component.currentEnvironmentName);
done();
});
});
it('hides the dropdown', done => {
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
environmentsEndpoint: '',
showTimeWindowDropdown: false,
},
});
Vue.nextTick(() => {
const dropdownIsActiveElement = component.$el.querySelectorAll('.environments');
expect(dropdownIsActiveElement.length).toEqual(0);
done();
});
});
it('does not show the time window dropdown when the feature flag is not set', done => {
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
});
setTimeout(() => {
const timeWindowDropdown = component.$el.querySelector('.js-time-window-dropdown');
expect(timeWindowDropdown).toBeNull();
done();
});
});
it('renders the time window dropdown with a set of options', done => {
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: true,
},
});
const numberOfTimeWindows = Object.keys(timeWindows).length;
setTimeout(() => {
const timeWindowDropdown = component.$el.querySelector('.js-time-window-dropdown');
const timeWindowDropdownEls = component.$el.querySelectorAll(
'.js-time-window-dropdown .dropdown-item',
2018-11-08 19:23:39 +05:30
);
2019-07-07 11:18:12 +05:30
expect(timeWindowDropdown).not.toBeNull();
expect(timeWindowDropdownEls.length).toEqual(numberOfTimeWindows);
2018-11-08 19:23:39 +05:30
done();
});
});
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']);
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: { ...propsData, hasMetrics: true, showTimeWindowDropdown: true },
});
setTimeout(() => {
const selectedTimeWindow = component.$el.querySelector(
'.js-time-window-dropdown [active="true"]',
);
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>',
]);
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: { ...propsData, hasMetrics: true, showTimeWindowDropdown: true },
});
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 => {
2018-12-05 23:21:45 +05:30
const component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
2019-07-07 11:18:12 +05:30
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
},
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', () => {
let component;
beforeEach(() => {
mock.onGet(mockApiEndpoint).reply(200, metricsGroupsAPIResponse);
});
afterEach(() => {
component.$destroy();
});
describe('with feature flag enabled', () => {
beforeEach(() => {
component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
externalDashboardPath: '/mockPath',
},
});
});
it('shows the link', done => {
setTimeout(() => {
expect(component.$el.querySelector('.js-external-dashboard-link').innerText).toContain(
'View full dashboard',
);
done();
});
});
});
describe('without feature flage enabled', () => {
beforeEach(() => {
window.gon.features.grafanaDashboardLink = false;
component = new DashboardComponent({
el: document.querySelector('.prometheus-graphs'),
propsData: {
...propsData,
hasMetrics: true,
showPanels: false,
showTimeWindowDropdown: false,
externalDashboardPath: '',
},
});
});
it('does not show the link', done => {
setTimeout(() => {
expect(component.$el.querySelector('.js-external-dashboard-link')).toBe(null);
done();
});
});
});
});
2017-09-10 17:25:29 +05:30
});