Files
grafana/public/app/features/dashboard-scene/scene/DashboardSceneUrlSync.test.ts
Dominik Prokop a4ff2abd9d PanelGridItemRepeater: Refactor (#84710)
* wip

* serialisation tests updates

* VizPanelManager test updates

* ests updates

* Making it green

* Use DashboardGridItem instead of SceneGridItem

* Cleanup tests that unnecessarily depend on dashboard grid items

* Fix row repeater behavior test

* Update public/app/features/dashboard-scene/panel-edit/PanelEditor.tsx

Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com>

* Fix unnecessary library panel changes detection

* Fix test

---------

Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com>
2024-03-21 14:38:00 +01:00

93 lines
2.8 KiB
TypeScript

import { AppEvents } from '@grafana/data';
import { SceneGridLayout, SceneQueryRunner, VizPanel } from '@grafana/scenes';
import appEvents from 'app/core/app_events';
import { DashboardGridItem } from './DashboardGridItem';
import { DashboardScene } from './DashboardScene';
import { DashboardRepeatsProcessedEvent } from './types';
describe('DashboardSceneUrlSync', () => {
describe('Given a standard scene', () => {
it('Should set inspectPanelKey when url has inspect key', () => {
const scene = buildTestScene();
scene.urlSync?.updateFromUrl({ inspect: '2' });
expect(scene.state.inspectPanelKey).toBe('2');
});
it('Should handle inspect key that is not found', () => {
const scene = buildTestScene();
scene.urlSync?.updateFromUrl({ inspect: '12321' });
expect(scene.state.inspectPanelKey).toBe(undefined);
});
it('Should set viewPanelKey when url has viewPanel', () => {
const scene = buildTestScene();
scene.urlSync?.updateFromUrl({ viewPanel: '2' });
expect(scene.state.viewPanelScene!.getUrlKey()).toBe('panel-2');
});
});
describe('Given a viewPanelKey with clone that is not found', () => {
const scene = buildTestScene();
let errorNotice = 0;
appEvents.on(AppEvents.alertError, (evt) => errorNotice++);
scene.urlSync?.updateFromUrl({ viewPanel: 'panel-1-clone-1' });
expect(scene.state.viewPanelScene).toBeUndefined();
// Verify no error notice was shown
expect(errorNotice).toBe(0);
// fake adding clone panel
const layout = scene.state.body as SceneGridLayout;
layout.setState({
children: [
new DashboardGridItem({
key: 'griditem-1',
x: 0,
body: new VizPanel({
title: 'Clone Panel A',
key: 'panel-1-clone-1',
pluginId: 'table',
}),
}),
],
});
// Verify it subscribes to DashboardRepeatsProcessedEvent
scene.publishEvent(new DashboardRepeatsProcessedEvent({ source: scene }));
expect(scene.state.viewPanelScene?.getUrlKey()).toBe('panel-1-clone-1');
});
});
function buildTestScene() {
const scene = new DashboardScene({
title: 'hello',
uid: 'dash-1',
body: new SceneGridLayout({
children: [
new DashboardGridItem({
key: 'griditem-1',
x: 0,
body: new VizPanel({
title: 'Panel A',
key: 'panel-1',
pluginId: 'table',
$data: new SceneQueryRunner({ key: 'data-query-runner', queries: [{ refId: 'A' }] }),
}),
}),
new DashboardGridItem({
body: new VizPanel({
title: 'Panel B',
key: 'panel-2',
pluginId: 'table',
}),
}),
],
}),
});
return scene;
}