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.
This commit is contained in:
Marcus Andersson 2023-04-04 07:17:10 +02:00 committed by GitHub
parent ea6aedd10a
commit c8ecd0679b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 12 deletions

View File

@ -1,3 +1,5 @@
import { DataQuery } from '@grafana/schema';
import { RawTimeRange, TimeZone } from './time'; import { RawTimeRange, TimeZone } from './time';
// Plugin Extensions types // Plugin Extensions types
@ -73,7 +75,7 @@ export type PluginExtensionPanelContext = {
timeRange: RawTimeRange; timeRange: RawTimeRange;
timeZone: TimeZone; timeZone: TimeZone;
dashboard: Dashboard; dashboard: Dashboard;
targets: Target[]; targets: DataQuery[];
}; };
type Dashboard = { type Dashboard = {
@ -81,8 +83,3 @@ type Dashboard = {
title: string; title: string;
tags: string[]; tags: string[];
}; };
type Target = {
pluginId: string;
refId: string;
};

View File

@ -228,7 +228,9 @@ describe('getPanelMenu()', () => {
targets: [ targets: [
{ {
refId: 'A', refId: 'A',
pluginId: 'testdata', datasource: {
type: 'testdata',
},
}, },
], ],
dashboard: { dashboard: {

View File

@ -332,16 +332,13 @@ function createExtensionContext(panel: PanelModel, dashboard: DashboardModel): P
id: panel.id, id: panel.id,
pluginId: panel.type, pluginId: panel.type,
title: panel.title, title: panel.title,
timeRange: Object.assign({}, dashboard.time), timeRange: dashboard.time,
timeZone: dashboard.timezone, timeZone: dashboard.timezone,
dashboard: { dashboard: {
uid: dashboard.uid, uid: dashboard.uid,
title: dashboard.title, title: dashboard.title,
tags: Array.from<string>(dashboard.tags), tags: Array.from<string>(dashboard.tags),
}, },
targets: panel.targets.map((t) => ({ targets: panel.targets,
refId: t.refId,
pluginId: t.datasource?.type ?? 'unknown',
})),
}; };
} }

View File

@ -246,4 +246,43 @@ describe('getPluginExtensions()', () => {
expect(global.console.warn).toHaveBeenCalledTimes(1); expect(global.console.warn).toHaveBeenCalledTimes(1);
expect(global.console.warn).toHaveBeenCalledWith('[Plugin Extensions] Something went wrong!'); 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();
});
}); });