diff --git a/packages/grafana-data/src/field/fieldOverrides.test.ts b/packages/grafana-data/src/field/fieldOverrides.test.ts index 1a298e878be..1ccb40c2395 100644 --- a/packages/grafana-data/src/field/fieldOverrides.test.ts +++ b/packages/grafana-data/src/field/fieldOverrides.test.ts @@ -4,6 +4,7 @@ import { setFieldConfigDefaults, setDynamicConfigValue, applyFieldOverrides, + getLinksSupplier, } from './fieldOverrides'; import { MutableDataFrame, toDataFrame } from '../dataframe'; import { @@ -20,7 +21,7 @@ import { mockStandardProperties } from '../utils/tests/mockStandardProperties'; import { FieldMatcherID } from '../transformations'; import { FieldConfigOptionsRegistry } from './FieldConfigOptionsRegistry'; import { getFieldDisplayName } from './fieldState'; - +import { locationUtil } from '../utils'; const property1 = { id: 'custom.property1', // Match field properties path: 'property1', // Match field properties @@ -418,3 +419,40 @@ describe('setDynamicConfigValue', () => { expect(config.displayName).toBeUndefined(); }); }); + +describe('getLinksSupplier', () => { + it('will replace variables in url and title of the data link', () => { + locationUtil.initialize({ + getConfig: () => ({} as any), + buildParamsFromVariables: (() => {}) as any, + getTimeRangeForUrl: (() => {}) as any, + }); + + const f0 = new MutableDataFrame({ + name: 'A', + fields: [ + { + name: 'message', + type: FieldType.string, + values: [10, 20], + config: { + links: [ + { + url: 'url to be interpolated', + title: 'title to be interpolated', + }, + ], + }, + }, + ], + }); + + const replaceSpy = jest.fn(); + const supplier = getLinksSupplier(f0, f0.fields[0], {}, replaceSpy, { theme: {} as GrafanaTheme }); + supplier({}); + + expect(replaceSpy).toBeCalledTimes(2); + expect(replaceSpy.mock.calls[0][0]).toEqual('url to be interpolated'); + expect(replaceSpy.mock.calls[1][0]).toEqual('title to be interpolated'); + }); +}); diff --git a/packages/grafana-data/src/field/fieldOverrides.ts b/packages/grafana-data/src/field/fieldOverrides.ts index f39a4faf9d5..350104c3293 100644 --- a/packages/grafana-data/src/field/fieldOverrides.ts +++ b/packages/grafana-data/src/field/fieldOverrides.ts @@ -343,7 +343,7 @@ export function validateFieldConfig(config: FieldConfig) { } } -const getLinksSupplier = ( +export const getLinksSupplier = ( frame: DataFrame, field: Field, fieldScopedVars: ScopedVars, @@ -366,7 +366,7 @@ const getLinksSupplier = ( const info: LinkModel = { href: locationUtil.assureBaseUrl(href.replace(/\n/g, '')), - title: replaceVariables(link.title || ''), + title: link.title || '', target: link.targetBlank ? '_blank' : '_self', origin: field, }; @@ -402,7 +402,7 @@ const getLinksSupplier = ( } } - info.href = replaceVariables(info.href, { + const variables = { ...fieldScopedVars, __value: { text: 'Value', @@ -417,8 +417,10 @@ const getLinksSupplier = ( text: variablesQuery, value: variablesQuery, }, - }); + }; + info.href = replaceVariables(info.href, variables); + info.title = replaceVariables(info.title, variables); info.href = locationUtil.processUrl(info.href); return info;