Commit 17a1e787 by Tiago Mota Santos Committed by GitHub

Dashboard: Honour root_url for Explore link (#27654)

* Dashboard: Add subUrl to explore url

Honour subUrl when opening explore in new window

* Dashboard: Add tests to onNavigateToExplore

* Dashboard: Remove condition from tests
parent ed185ea0
import { PanelMenuItem } from '@grafana/data';
import { DashboardModel, PanelModel } from '../state'; import { DashboardModel, PanelModel } from '../state';
import { getPanelMenu } from './getPanelMenu'; import { getPanelMenu } from './getPanelMenu';
import { describe } from '../../../../test/lib/common'; import { describe } from '../../../../test/lib/common';
import { setStore } from 'app/store/store';
import config from 'app/core/config';
import * as actions from 'app/features/explore/state/actions';
jest.mock('app/core/services/context_srv', () => ({
contextSrv: {
hasAccessToExplore: () => true,
},
}));
describe('getPanelMenu', () => { describe('getPanelMenu', () => {
it('should return the correct panel menu items', () => { it('should return the correct panel menu items', () => {
...@@ -29,6 +39,12 @@ describe('getPanelMenu', () => { ...@@ -29,6 +39,12 @@ describe('getPanelMenu', () => {
"text": "Share", "text": "Share",
}, },
Object { Object {
"iconClassName": "compass",
"onClick": [Function],
"shortcut": "x",
"text": "Explore",
},
Object {
"iconClassName": "info-circle", "iconClassName": "info-circle",
"onClick": [Function], "onClick": [Function],
"shortcut": "i", "shortcut": "i",
...@@ -103,6 +119,12 @@ describe('getPanelMenu', () => { ...@@ -103,6 +119,12 @@ describe('getPanelMenu', () => {
"text": "Share", "text": "Share",
}, },
Object { Object {
"iconClassName": "compass",
"onClick": [Function],
"shortcut": "x",
"text": "Explore",
},
Object {
"iconClassName": "info-circle", "iconClassName": "info-circle",
"onClick": [Function], "onClick": [Function],
"shortcut": "i", "shortcut": "i",
...@@ -143,4 +165,50 @@ describe('getPanelMenu', () => { ...@@ -143,4 +165,50 @@ describe('getPanelMenu', () => {
`); `);
}); });
}); });
describe('onNavigateToExplore', () => {
const testSubUrl = '/testSubUrl';
const testUrl = '/testUrl';
const windowOpen = jest.fn();
let event: any;
let explore: PanelMenuItem;
let navigateSpy: any;
beforeAll(() => {
const panel = new PanelModel({});
const dashboard = new DashboardModel({});
const menuItems = getPanelMenu(dashboard, panel);
explore = menuItems.find(item => item.text === 'Explore') as PanelMenuItem;
navigateSpy = jest.spyOn(actions, 'navigateToExplore');
window.open = windowOpen;
event = {
ctrlKey: true,
preventDefault: jest.fn(),
};
setStore({ dispatch: jest.fn() } as any);
});
it('should navigate to url without subUrl', () => {
explore.onClick!(event);
const openInNewWindow = navigateSpy.mock.calls[0][1].openInNewWindow;
openInNewWindow(testUrl);
expect(windowOpen).toHaveBeenLastCalledWith(testUrl);
});
it('should navigate to url with subUrl', () => {
config.appSubUrl = testSubUrl;
explore.onClick!(event);
const openInNewWindow = navigateSpy.mock.calls[0][1].openInNewWindow;
openInNewWindow(testUrl);
expect(windowOpen).toHaveBeenLastCalledWith(`${testSubUrl}${testUrl}`);
});
});
}); });
...@@ -10,6 +10,7 @@ import { navigateToExplore } from '../../explore/state/actions'; ...@@ -10,6 +10,7 @@ import { navigateToExplore } from '../../explore/state/actions';
import { getExploreUrl } from '../../../core/utils/explore'; import { getExploreUrl } from '../../../core/utils/explore';
import { getTimeSrv } from '../services/TimeSrv'; import { getTimeSrv } from '../services/TimeSrv';
import { PanelCtrl } from '../../panel/panel_ctrl'; import { PanelCtrl } from '../../panel/panel_ctrl';
import config from 'app/core/config';
export function getPanelMenu( export function getPanelMenu(
dashboard: DashboardModel, dashboard: DashboardModel,
...@@ -76,7 +77,8 @@ export function getPanelMenu( ...@@ -76,7 +77,8 @@ export function getPanelMenu(
const onNavigateToExplore = (event: React.MouseEvent<any>) => { const onNavigateToExplore = (event: React.MouseEvent<any>) => {
event.preventDefault(); event.preventDefault();
const openInNewWindow = event.ctrlKey || event.metaKey ? (url: string) => window.open(url) : undefined; const openInNewWindow =
event.ctrlKey || event.metaKey ? (url: string) => window.open(`${config.appSubUrl}${url}`) : undefined;
store.dispatch(navigateToExplore(panel, { getDataSourceSrv, getTimeSrv, getExploreUrl, openInNewWindow }) as any); store.dispatch(navigateToExplore(panel, { getDataSourceSrv, getTimeSrv, getExploreUrl, openInNewWindow }) as any);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment