diff --git a/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.test.tsx b/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.test.tsx new file mode 100644 index 00000000000..3fe8ab135ff --- /dev/null +++ b/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.test.tsx @@ -0,0 +1,110 @@ +import { screen } from '@testing-library/react'; +import { render } from 'test/test-utils'; + +import { getPanelPlugin } from '@grafana/data/test'; +import { setPluginImportUtils } from '@grafana/runtime'; +import { SceneGridLayout, SceneTimeRange, VizPanel } from '@grafana/scenes'; + +import { DashboardScene } from '../scene/DashboardScene'; +import { AutoGridItem } from '../scene/layout-auto-grid/AutoGridItem'; +import { AutoGridLayout } from '../scene/layout-auto-grid/AutoGridLayout'; +import { AutoGridLayoutManager } from '../scene/layout-auto-grid/AutoGridLayoutManager'; +import { DashboardGridItem } from '../scene/layout-default/DashboardGridItem'; +import { DefaultGridLayoutManager } from '../scene/layout-default/DefaultGridLayoutManager'; +import { activateFullSceneTree } from '../utils/test-utils'; + +import { ShareLibraryPanelTab } from './ShareLibraryPanelTab'; + +jest.mock('app/features/dashboard/components/ShareModal/ShareLibraryPanel', () => ({ + // eslint-disable-next-line react/display-name + ShareLibraryPanel: ({ panel }: { panel: { title: string } }) => ( +
panel-title:{panel.title}
+ ), +})); + +setPluginImportUtils({ + importPanelPlugin: () => Promise.resolve(getPanelPlugin({})), + getPanelPluginFromCache: () => undefined, +}); + +describe('ShareLibraryPanelTab', () => { + it('renders library panel content for auto grid panels', () => { + const { tab } = setupAutoGridScenario(); + + render(); + + expect(screen.getByTestId('share-library-panel')).toHaveTextContent('panel-title:Auto panel'); + }); + + it('renders library panel content for default grid panels', () => { + const { tab } = setupDefaultGridScenario(); + + render(); + + expect(screen.getByTestId('share-library-panel')).toHaveTextContent('panel-title:Default panel'); + }); +}); + +function setupAutoGridScenario() { + const vizPanel = new VizPanel({ + key: 'panel-1', + pluginId: 'table', + title: 'Auto panel', + }); + + const autoGridItem = new AutoGridItem({ + key: 'auto-grid-item-1', + body: vizPanel, + }); + + const layoutManager = new AutoGridLayoutManager({ + layout: new AutoGridLayout({ children: [autoGridItem] }), + }); + + const tab = new ShareLibraryPanelTab({ + panelRef: vizPanel.getRef(), + }); + + const dashboard = new DashboardScene({ + title: 'Dash', + uid: 'dash-1', + meta: { canEdit: true }, + $timeRange: new SceneTimeRange({}), + body: layoutManager, + overlay: tab, + }); + + activateFullSceneTree(dashboard); + + return { tab, dashboard }; +} + +function setupDefaultGridScenario() { + const vizPanel = new VizPanel({ + key: 'panel-1', + pluginId: 'table', + title: 'Default panel', + }); + + const gridItem = new DashboardGridItem({ + key: 'grid-item-1', + body: vizPanel, + }); + + const tab = new ShareLibraryPanelTab({ + panelRef: vizPanel.getRef(), + }); + + const dashboard = new DashboardScene({ + title: 'Dash', + uid: 'dash-1', + meta: { canEdit: true }, + $timeRange: new SceneTimeRange({}), + body: new DefaultGridLayoutManager({ grid: new SceneGridLayout({ children: [gridItem] }) }), + overlay: tab, + }); + + activateFullSceneTree(dashboard); + + return { tab, dashboard }; +} diff --git a/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.tsx b/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.tsx index 4c882b60618..3a30931063c 100644 --- a/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.tsx +++ b/public/app/features/dashboard-scene/sharing/ShareLibraryPanelTab.tsx @@ -6,8 +6,13 @@ import { shareDashboardType } from 'app/features/dashboard/components/ShareModal import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; import { PanelModel } from 'app/features/dashboard/state/PanelModel'; +import { AutoGridItem } from '../scene/layout-auto-grid/AutoGridItem'; import { DashboardGridItem } from '../scene/layout-default/DashboardGridItem'; -import { gridItemToPanel, transformSceneToSaveModel } from '../serialization/transformSceneToSaveModel'; +import { + gridItemToPanel, + transformSceneToSaveModel, + vizPanelToPanel, +} from '../serialization/transformSceneToSaveModel'; import { getDashboardSceneFor } from '../utils/utils'; import { SceneShareTabState } from './types'; @@ -35,9 +40,10 @@ function ShareLibraryPanelTabRenderer({ model }: SceneComponentProps