diff --git a/public/app/features/variables/utils.test.ts b/public/app/features/variables/utils.test.ts index 90836b2d822..188def5efed 100644 --- a/public/app/features/variables/utils.test.ts +++ b/public/app/features/variables/utils.test.ts @@ -1,4 +1,11 @@ -import { ensureStringValues, findTemplateVarChanges, getCurrentText, getVariableRefresh, isAllVariable } from './utils'; +import { + containsVariable, + ensureStringValues, + findTemplateVarChanges, + getCurrentText, + getVariableRefresh, + isAllVariable, +} from './utils'; import { VariableRefresh } from './types'; import { UrlQueryMap } from '@grafana/data'; @@ -174,3 +181,16 @@ describe('ensureStringValues', () => { expect(ensureStringValues(value)).toEqual(expected); }); }); + +describe('containsVariable', () => { + it.each` + value | expected + ${''} | ${false} + ${'$var'} | ${true} + ${{ thing1: '${var}' }} | ${true} + ${{ thing1: ['1', '${var}'] }} | ${true} + ${{ thing1: { thing2: '${var}' } }} | ${true} + `('when called with value:$value then result should be:$expected', ({ value, expected }) => { + expect(containsVariable(value, 'var')).toEqual(expected); + }); +}); diff --git a/public/app/features/variables/utils.ts b/public/app/features/variables/utils.ts index e62a7850827..cbcd37ef1c9 100644 --- a/public/app/features/variables/utils.ts +++ b/public/app/features/variables/utils.ts @@ -1,4 +1,4 @@ -import { isString, isArray, isEqual } from 'lodash'; +import { isArray, isEqual } from 'lodash'; import { ScopedVars, UrlQueryMap, VariableType } from '@grafana/data'; import { getTemplateSrv } from '@grafana/runtime'; @@ -6,6 +6,7 @@ import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from './state/types'; import { QueryVariableModel, VariableModel, VariableRefresh } from './types'; import { getTimeSrv } from '../dashboard/services/TimeSrv'; import { variableAdapters } from './adapters'; +import { safeStringifyValue } from 'app/core/utils/explore'; /* * This regex matches 3 types of variable reference with an optional format specifier @@ -51,7 +52,7 @@ export const getSearchFilterScopedVar = (args: { export function containsVariable(...args: any[]) { const variableName = args[args.length - 1]; - args[0] = isString(args[0]) ? args[0] : Object['values'](args[0]).join(' '); + args[0] = typeof args[0] === 'string' ? args[0] : safeStringifyValue(args[0]); const variableString = args.slice(0, -1).join(' '); const matches = variableString.match(variableRegex); const isMatchingVariable =