Fix: DataLinks from data sources override user defined data link (#65996)

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
This commit is contained in:
Alexa V 2023-04-05 15:44:43 +02:00 committed by GitHub
parent 232834f455
commit 9041e7dfa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 0 deletions

View File

@ -415,6 +415,124 @@ describe('setFieldConfigDefaults', () => {
}
`);
});
it('applies field config defaults correctly when links property exist in field config and no links are defined in panel', () => {
const dsFieldConfig: FieldConfig = {
links: [
{
title: 'Google link',
url: 'https://google.com',
},
],
};
const panelFieldConfig: FieldConfig = {};
const context: FieldOverrideEnv = {
data: [],
field: { type: FieldType.number } as Field,
dataFrameIndex: 0,
fieldConfigRegistry: customFieldRegistry,
};
// we mutate dsFieldConfig
// @ts-ignore
setFieldConfigDefaults(dsFieldConfig, panelFieldConfig, context);
expect(dsFieldConfig).toMatchInlineSnapshot(`
{
"custom": {},
"links": [
{
"title": "Google link",
"url": "https://google.com",
},
],
}
`);
});
it('applies field config defaults correctly when links property exist in panel config and no links are defined in ds field config', () => {
const dsFieldConfig: FieldConfig = {};
const panelFieldConfig: FieldConfig = {
links: [
{
title: 'Google link',
url: 'https://google.com',
},
],
};
const context: FieldOverrideEnv = {
data: [],
field: { type: FieldType.number } as Field,
dataFrameIndex: 0,
fieldConfigRegistry: customFieldRegistry,
};
// we mutate dsFieldConfig
// @ts-ignore
setFieldConfigDefaults(dsFieldConfig, panelFieldConfig, context);
expect(dsFieldConfig).toMatchInlineSnapshot(`
{
"custom": {},
"links": [
{
"title": "Google link",
"url": "https://google.com",
},
],
}
`);
});
it('applies a merge strategy for links when they exist in ds config and panel', () => {
const dsFieldConfig: FieldConfig = {
links: [
{
title: 'Google link',
url: 'https://google.com',
},
],
};
const panelFieldConfig: FieldConfig = {
links: [
{
title: 'Grafana',
url: 'https://grafana.com',
},
],
};
const context: FieldOverrideEnv = {
data: [],
field: { type: FieldType.number } as Field,
dataFrameIndex: 0,
fieldConfigRegistry: customFieldRegistry,
};
// we mutate dsFieldConfig
setFieldConfigDefaults(dsFieldConfig, panelFieldConfig, context);
expect(dsFieldConfig).toMatchInlineSnapshot(`
{
"custom": {},
"links": [
{
"title": "Google link",
"url": "https://google.com",
},
{
"title": "Grafana",
"url": "https://grafana.com",
},
],
}
`);
});
});
describe('setDynamicConfigValue', () => {

View File

@ -289,6 +289,11 @@ export function setDynamicConfigValue(config: FieldConfig, value: DynamicConfigV
// config -> from DS
// defaults -> from Panel config
export function setFieldConfigDefaults(config: FieldConfig, defaults: FieldConfig, context: FieldOverrideEnv) {
// For cases where we have links on the datasource config and the panel config, we need to merge them
if (config.links && defaults.links) {
// Combine the data source links and the panel default config links
config.links = [...config.links, ...defaults.links];
}
for (const fieldConfigProperty of context.fieldConfigRegistry.list()) {
if (fieldConfigProperty.isCustom && !config.custom) {
config.custom = {};