diff --git a/packages/grafana-data/src/field/fieldOverrides.test.ts b/packages/grafana-data/src/field/fieldOverrides.test.ts index 1199f8118ff..c0295144ab1 100644 --- a/packages/grafana-data/src/field/fieldOverrides.test.ts +++ b/packages/grafana-data/src/field/fieldOverrides.test.ts @@ -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', () => { diff --git a/packages/grafana-data/src/field/fieldOverrides.ts b/packages/grafana-data/src/field/fieldOverrides.ts index 08bc0c63d43..f3b248a5000 100644 --- a/packages/grafana-data/src/field/fieldOverrides.ts +++ b/packages/grafana-data/src/field/fieldOverrides.ts @@ -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 = {};