mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Scenes: Setting default_home_dashboard_path returns blank page and no controls (#89304)
This commit is contained in:
@@ -11,9 +11,11 @@ import { config, getPluginLinkExtensions, locationService, setPluginImportUtils
|
|||||||
import { VizPanel } from '@grafana/scenes';
|
import { VizPanel } from '@grafana/scenes';
|
||||||
import { Dashboard } from '@grafana/schema';
|
import { Dashboard } from '@grafana/schema';
|
||||||
import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
|
import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
|
||||||
|
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
||||||
import store from 'app/core/store';
|
import store from 'app/core/store';
|
||||||
import { DashboardLoaderSrv, setDashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
|
import { DashboardLoaderSrv, setDashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
|
||||||
import { DASHBOARD_FROM_LS_KEY } from 'app/features/dashboard/state/initDashboard';
|
import { DASHBOARD_FROM_LS_KEY } from 'app/features/dashboard/state/initDashboard';
|
||||||
|
import { DashboardRoutes } from 'app/types';
|
||||||
|
|
||||||
import { dashboardSceneGraph } from '../utils/dashboardSceneGraph';
|
import { dashboardSceneGraph } from '../utils/dashboardSceneGraph';
|
||||||
|
|
||||||
@@ -24,6 +26,11 @@ jest.mock('@grafana/runtime', () => ({
|
|||||||
...jest.requireActual('@grafana/runtime'),
|
...jest.requireActual('@grafana/runtime'),
|
||||||
setPluginExtensionGetter: jest.fn(),
|
setPluginExtensionGetter: jest.fn(),
|
||||||
getPluginLinkExtensions: jest.fn(),
|
getPluginLinkExtensions: jest.fn(),
|
||||||
|
getBackendSrv: () => {
|
||||||
|
return {
|
||||||
|
get: jest.fn().mockResolvedValue({ dashboard: simpleDashboard, meta: { url: '' } }),
|
||||||
|
};
|
||||||
|
},
|
||||||
getDataSourceSrv: () => {
|
getDataSourceSrv: () => {
|
||||||
return {
|
return {
|
||||||
get: jest.fn().mockResolvedValue({}),
|
get: jest.fn().mockResolvedValue({}),
|
||||||
@@ -37,12 +44,19 @@ jest.mock('@grafana/runtime', () => ({
|
|||||||
|
|
||||||
const getPluginLinkExtensionsMock = jest.mocked(getPluginLinkExtensions);
|
const getPluginLinkExtensionsMock = jest.mocked(getPluginLinkExtensions);
|
||||||
|
|
||||||
function setup() {
|
function setup({ routeProps }: { routeProps?: Partial<GrafanaRouteComponentProps> } = {}) {
|
||||||
const context = getGrafanaContextMock();
|
const context = getGrafanaContextMock();
|
||||||
|
const defaultRouteProps = getRouteComponentProps();
|
||||||
const props: Props = {
|
const props: Props = {
|
||||||
...getRouteComponentProps(),
|
...defaultRouteProps,
|
||||||
|
match: {
|
||||||
|
...defaultRouteProps.match,
|
||||||
|
params: {
|
||||||
|
uid: 'my-dash-uid',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
...routeProps,
|
||||||
};
|
};
|
||||||
props.match.params.uid = 'my-dash-uid';
|
|
||||||
|
|
||||||
const renderResult = render(
|
const renderResult = render(
|
||||||
<TestProvider grafanaContext={context}>
|
<TestProvider grafanaContext={context}>
|
||||||
@@ -258,14 +272,39 @@ describe('DashboardScenePage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('home page', () => {
|
describe('home page', () => {
|
||||||
it('should not show controls', async () => {
|
it('should render the dashboard when the route is home', async () => {
|
||||||
|
setup({
|
||||||
|
routeProps: {
|
||||||
|
route: {
|
||||||
|
...getRouteComponentProps().route,
|
||||||
|
routeName: DashboardRoutes.Home,
|
||||||
|
},
|
||||||
|
match: {
|
||||||
|
...getRouteComponentProps().match,
|
||||||
|
path: '/',
|
||||||
|
params: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitForDashbordToRender();
|
||||||
|
|
||||||
|
expect(await screen.findByTitle('Panel A')).toBeInTheDocument();
|
||||||
|
expect(await screen.findByText('Content A')).toBeInTheDocument();
|
||||||
|
|
||||||
|
expect(await screen.findByTitle('Panel B')).toBeInTheDocument();
|
||||||
|
expect(await screen.findByText('Content B')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show controls', async () => {
|
||||||
getDashboardScenePageStateManager().clearDashboardCache();
|
getDashboardScenePageStateManager().clearDashboardCache();
|
||||||
loadDashboardMock.mockClear();
|
loadDashboardMock.mockClear();
|
||||||
loadDashboardMock.mockResolvedValue({ dashboard: { panels: [] }, meta: {} });
|
loadDashboardMock.mockResolvedValue({ dashboard: { panels: [] }, meta: {} });
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
await waitFor(() => expect(screen.queryByText('Refresh')).not.toBeInTheDocument());
|
await waitFor(() => expect(screen.queryByText('Refresh')).toBeInTheDocument());
|
||||||
|
await waitFor(() => expect(screen.queryByText('Last 6 hours')).toBeInTheDocument());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -76,7 +76,12 @@ export function DashboardScenePage({ match, route, queryParams, history }: Props
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Do not render anything when transitioning from one dashboard to another
|
// Do not render anything when transitioning from one dashboard to another
|
||||||
if (match.params.type !== 'snapshot' && dashboard.state.uid && dashboard.state.uid !== match.params.uid) {
|
if (
|
||||||
|
match.params.type !== 'snapshot' &&
|
||||||
|
dashboard.state.uid &&
|
||||||
|
dashboard.state.uid !== match.params.uid &&
|
||||||
|
route.routeName !== DashboardRoutes.Home
|
||||||
|
) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -104,17 +104,29 @@ describe('DashboardControls', () => {
|
|||||||
expect(scene.state.hideTimeControls).toBeTruthy();
|
expect(scene.state.hideTimeControls).toBeTruthy();
|
||||||
expect(scene.state.hideVariableControls).toBeTruthy();
|
expect(scene.state.hideVariableControls).toBeTruthy();
|
||||||
expect(scene.state.hideLinksControls).toBeTruthy();
|
expect(scene.state.hideLinksControls).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not override state if no new state comes from url', () => {
|
||||||
|
const scene = buildTestScene({ hideTimeControls: true, hideVariableControls: true, hideLinksControls: true });
|
||||||
scene.updateFromUrl({});
|
scene.updateFromUrl({});
|
||||||
expect(scene.state.hideTimeControls).toBeFalsy();
|
expect(scene.state.hideTimeControls).toBeTruthy();
|
||||||
expect(scene.state.hideVariableControls).toBeFalsy();
|
expect(scene.state.hideVariableControls).toBeTruthy();
|
||||||
expect(scene.state.hideLinksControls).toBeFalsy();
|
expect(scene.state.hideLinksControls).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not call setState if no changes', () => {
|
it('should not call setState if no changes', () => {
|
||||||
const scene = buildTestScene();
|
const scene = buildTestScene();
|
||||||
const setState = jest.spyOn(scene, 'setState');
|
const setState = jest.spyOn(scene, 'setState');
|
||||||
scene.updateFromUrl({});
|
scene.updateFromUrl({
|
||||||
scene.updateFromUrl({});
|
'_dash.hideTimePicker': 'true',
|
||||||
|
'_dash.hideVariables': 'true',
|
||||||
|
'_dash.hideLinks': 'true',
|
||||||
|
});
|
||||||
|
scene.updateFromUrl({
|
||||||
|
'_dash.hideTimePicker': 'true',
|
||||||
|
'_dash.hideVariables': 'true',
|
||||||
|
'_dash.hideLinks': 'true',
|
||||||
|
});
|
||||||
expect(setState).toHaveBeenCalledTimes(1);
|
expect(setState).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -54,9 +54,14 @@ export class DashboardControls extends SceneObjectBase<DashboardControlsState> {
|
|||||||
updateFromUrl(values: SceneObjectUrlValues) {
|
updateFromUrl(values: SceneObjectUrlValues) {
|
||||||
const update: Partial<DashboardControlsState> = {};
|
const update: Partial<DashboardControlsState> = {};
|
||||||
|
|
||||||
update.hideTimeControls = values['_dash.hideTimePicker'] === 'true' || values['_dash.hideTimePicker'] === '';
|
update.hideTimeControls =
|
||||||
update.hideVariableControls = values['_dash.hideVariables'] === 'true' || values['_dash.hideVariables'] === '';
|
values['_dash.hideTimePicker'] === 'true' || values['_dash.hideTimePicker'] === '' || this.state.hideTimeControls;
|
||||||
update.hideLinksControls = values['_dash.hideLinks'] === 'true' || values['_dash.hideLinks'] === '';
|
update.hideVariableControls =
|
||||||
|
values['_dash.hideVariables'] === 'true' ||
|
||||||
|
values['_dash.hideVariables'] === '' ||
|
||||||
|
this.state.hideVariableControls;
|
||||||
|
update.hideLinksControls =
|
||||||
|
values['_dash.hideLinks'] === 'true' || values['_dash.hideLinks'] === '' || this.state.hideLinksControls;
|
||||||
|
|
||||||
if (Object.entries(update).some(([k, v]) => v !== this.state[k as keyof DashboardControlsState])) {
|
if (Object.entries(update).some(([k, v]) => v !== this.state[k as keyof DashboardControlsState])) {
|
||||||
this.setState(update);
|
this.setState(update);
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export function DashboardSceneRenderer({ model }: SceneComponentProps<DashboardS
|
|||||||
>
|
>
|
||||||
{scopes && <scopes.Component model={scopes} />}
|
{scopes && <scopes.Component model={scopes} />}
|
||||||
<NavToolbarActions dashboard={model} />
|
<NavToolbarActions dashboard={model} />
|
||||||
{!isHomePage && controls && hasControls && (
|
{controls && hasControls && (
|
||||||
<div
|
<div
|
||||||
className={cx(styles.controlsWrapper, scopes && !isScopesExpanded && styles.controlsWrapperWithScopes)}
|
className={cx(styles.controlsWrapper, scopes && !isScopesExpanded && styles.controlsWrapperWithScopes)}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user