DataLinks: Ensure replaceVariables provided to link.onBuildUrl is bound to data frame variables (#68371)

* bind variable replacer provided to link.onBuildUrl to data frame vars

* revert internal link change
This commit is contained in:
Domas 2023-05-15 12:06:48 +03:00 committed by GitHub
parent 07c725800b
commit bb2cbd2e01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View File

@ -838,7 +838,10 @@ describe('getLinksSupplier', () => {
links: [
{
url: 'should not be ignored',
onClick: onClickSpy,
onClick: (evt) => {
onClickSpy();
evt.replaceVariables?.('${foo}');
},
title: 'title to be interpolated',
},
{
@ -850,8 +853,8 @@ describe('getLinksSupplier', () => {
},
],
});
const supplier = getLinksSupplier(f0, f0.fields[0], {}, replaceSpy);
const scopedVars = { foo: { text: 'bar', value: 'bar' } };
const supplier = getLinksSupplier(f0, f0.fields[0], scopedVars, replaceSpy);
const links = supplier({});
expect(links.length).toBe(2);
@ -861,12 +864,15 @@ describe('getLinksSupplier', () => {
links[0].onClick!({});
expect(onClickSpy).toBeCalledTimes(1);
expect(replaceSpy).toBeCalledTimes(4);
// check that onClick variable replacer has scoped vars bound to it
expect(replaceSpy.mock.calls[1][1]).toHaveProperty('foo', { text: 'bar', value: 'bar' });
});
it('handles links built dynamically', () => {
const replaceSpy = jest.fn().mockReturnValue('url interpolated 10');
const onBuildUrlSpy = jest.fn();
const scopedVars = { foo: { text: 'bar', value: 'bar' } };
const f0 = createDataFrame({
name: 'A',
fields: [
@ -878,8 +884,9 @@ describe('getLinksSupplier', () => {
links: [
{
url: 'should be ignored',
onBuildUrl: () => {
onBuildUrl: (evt) => {
onBuildUrlSpy();
evt?.replaceVariables?.('${foo}');
return 'url to be interpolated';
},
title: 'title to be interpolated',
@ -894,12 +901,15 @@ describe('getLinksSupplier', () => {
],
});
const supplier = getLinksSupplier(f0, f0.fields[0], {}, replaceSpy);
const supplier = getLinksSupplier(f0, f0.fields[0], scopedVars, replaceSpy);
const links = supplier({});
expect(onBuildUrlSpy).toBeCalledTimes(1);
expect(links.length).toBe(2);
expect(links[0].href).toEqual('url interpolated 10');
expect(replaceSpy).toBeCalledTimes(5);
// check that onBuildUrl variable replacer has scoped vars bound to it
expect(replaceSpy.mock.calls[1][1]).toHaveProperty('foo', { text: 'bar', value: 'bar' });
});
});
});

View File

@ -378,6 +378,9 @@ export const getLinksSupplier =
__dataContext: dataContext,
};
const boundReplaceVariables: InterpolateFunction = (value, scopedVars, format) =>
replaceVariables(value, { ...dataLinkScopedVars, ...scopedVars }, format);
// We are not displaying reduction result
if (config.valueRowIndex !== undefined && !isNaN(config.valueRowIndex)) {
dataContext.value.rowIndex = config.valueRowIndex;
@ -394,7 +397,7 @@ export const getLinksSupplier =
link.onClick!({
origin: origin ?? field,
e: evt,
replaceVariables: (v) => replaceVariables(v, dataLinkScopedVars),
replaceVariables: boundReplaceVariables,
});
},
origin: field,
@ -415,7 +418,7 @@ export const getLinksSupplier =
let href = link.onBuildUrl
? link.onBuildUrl({
origin: field,
replaceVariables,
replaceVariables: boundReplaceVariables,
})
: link.url;