From 4609dd4aab3e25e2c5c73560de52f47286834d9b Mon Sep 17 00:00:00 2001 From: Marcus Andersson Date: Tue, 4 Apr 2023 07:17:10 +0200 Subject: [PATCH] PluginExtensions: Adding full targets to the panel menu context (#65861) * Making sure we add the whole query to the dashboard panel context. * Adding some more tests. * Synced with main. (cherry picked from commit c8ecd0679b959665a7200d8a785d7e0b386cdbb8) --- .../src/types/pluginExtensions.ts | 9 ++--- .../dashboard/utils/getPanelMenu.test.ts | 4 +- .../features/dashboard/utils/getPanelMenu.ts | 7 +--- .../extensions/getPluginExtensions.test.ts | 39 +++++++++++++++++++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/packages/grafana-data/src/types/pluginExtensions.ts b/packages/grafana-data/src/types/pluginExtensions.ts index 463ff68d68e93..dcc312a9c9c87 100644 --- a/packages/grafana-data/src/types/pluginExtensions.ts +++ b/packages/grafana-data/src/types/pluginExtensions.ts @@ -1,3 +1,5 @@ +import { DataQuery } from '@grafana/schema'; + import { RawTimeRange, TimeZone } from './time'; // Plugin Extensions types @@ -73,7 +75,7 @@ export type PluginExtensionPanelContext = { timeRange: RawTimeRange; timeZone: TimeZone; dashboard: Dashboard; - targets: Target[]; + targets: DataQuery[]; }; type Dashboard = { @@ -81,8 +83,3 @@ type Dashboard = { title: string; tags: string[]; }; - -type Target = { - pluginId: string; - refId: string; -}; diff --git a/public/app/features/dashboard/utils/getPanelMenu.test.ts b/public/app/features/dashboard/utils/getPanelMenu.test.ts index 41c0011959c15..391f4bf82c1d3 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.test.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.test.ts @@ -228,7 +228,9 @@ describe('getPanelMenu()', () => { targets: [ { refId: 'A', - pluginId: 'testdata', + datasource: { + type: 'testdata', + }, }, ], dashboard: { diff --git a/public/app/features/dashboard/utils/getPanelMenu.ts b/public/app/features/dashboard/utils/getPanelMenu.ts index c8b7dea708620..405e971c6a085 100644 --- a/public/app/features/dashboard/utils/getPanelMenu.ts +++ b/public/app/features/dashboard/utils/getPanelMenu.ts @@ -332,16 +332,13 @@ function createExtensionContext(panel: PanelModel, dashboard: DashboardModel): P id: panel.id, pluginId: panel.type, title: panel.title, - timeRange: Object.assign({}, dashboard.time), + timeRange: dashboard.time, timeZone: dashboard.timezone, dashboard: { uid: dashboard.uid, title: dashboard.title, tags: Array.from(dashboard.tags), }, - targets: panel.targets.map((t) => ({ - refId: t.refId, - pluginId: t.datasource?.type ?? 'unknown', - })), + targets: panel.targets, }; } diff --git a/public/app/features/plugins/extensions/getPluginExtensions.test.ts b/public/app/features/plugins/extensions/getPluginExtensions.test.ts index d68b0e1f1305a..54d5b63b838d8 100644 --- a/public/app/features/plugins/extensions/getPluginExtensions.test.ts +++ b/public/app/features/plugins/extensions/getPluginExtensions.test.ts @@ -246,4 +246,43 @@ describe('getPluginExtensions()', () => { expect(global.console.warn).toHaveBeenCalledTimes(1); expect(global.console.warn).toHaveBeenCalledWith('[Plugin Extensions] Something went wrong!'); }); + + test('should pass a frozen copy of the context to the onClick() function', () => { + const context = { title: 'New title from the context!' }; + + link2.path = undefined; + link2.onClick = jest.fn(); + + const registry = createPluginExtensionRegistry([{ pluginId, extensionConfigs: [link2] }]); + const { extensions } = getPluginExtensions({ registry, context, extensionPointId: extensionPoint2 }); + const [extension] = extensions; + + assertPluginExtensionLink(extension); + extension.onClick?.({} as React.MouseEvent); + + const helpers = (link2.onClick as jest.Mock).mock.calls[0][1]; + + expect(link2.configure).toHaveBeenCalledTimes(1); + expect(Object.isFrozen(helpers.context)).toBe(true); + expect(() => { + helpers.context.title = 'New title'; + }).toThrow(); + }); + + test('should should not freeze the original context', () => { + const context = { + title: 'New title from the context!', + nested: { title: 'title' }, + array: ['a'], + }; + + const registry = createPluginExtensionRegistry([{ pluginId, extensionConfigs: [link2] }]); + getPluginExtensions({ registry, context, extensionPointId: extensionPoint2 }); + + expect(() => { + context.title = 'Updating the title'; + context.nested.title = 'new title'; + context.array.push('b'); + }).not.toThrow(); + }); });