@ -1,5 +1,6 @@
import { lastValueFrom , of } from 'rxjs' ;
import { AdHocVariableFilter } from '@grafana/data' ;
import { BackendSrvRequest , TemplateSrv } from '@grafana/runtime' ;
import config from 'app/core/config' ;
@ -10,6 +11,7 @@ import { mockInfluxQueryRequest } from './__mocks__/request';
import { mockInfluxFetchResponse , mockMetricFindQueryResponse } from './__mocks__/response' ;
import { BROWSER_MODE_DISABLED_MESSAGE } from './constants' ;
import InfluxDatasource from './datasource' ;
import { InfluxQuery , InfluxVersion } from './types' ;
const fetchMock = mockBackendService ( mockInfluxFetchResponse ( ) ) ;
@ -267,7 +269,10 @@ describe('InfluxDataSource Backend Mode [influxdbBackendMigration=true]', () =>
} ) ;
describe ( 'interpolateQueryExpr' , ( ) = > {
let ds = getMockInfluxDS ( getMockDSInstanceSettings ( ) , { } as TemplateSrv ) ;
const templateSrvStub = {
replace : jest.fn ( ) . mockImplementation ( ( . . . rest : unknown [ ] ) = > 'templateVarReplaced' ) ,
} as unknown as TemplateSrv ;
let ds = getMockInfluxDS ( getMockDSInstanceSettings ( ) , templateSrvStub ) ;
it ( 'should return the value as it is' , ( ) = > {
const value = 'normalValue' ;
const variableMock = queryBuilder ( ) . withId ( 'tempVar' ) . withName ( 'tempVar' ) . withMulti ( false ) . build ( ) ;
@ -373,4 +378,19 @@ describe('interpolateQueryExpr', () => {
const expectation = ` 1.0 ` ;
expect ( result ) . toBe ( expectation ) ;
} ) ;
it ( 'template var in adhoc' , ( ) = > {
const templateVarName = '$templateVarName' ;
const templateVarValue = 'templateVarValue' ;
const templateSrvStub = {
replace : jest
. fn ( )
. mockImplementation ( ( target? : string ) = > ( target === templateVarName ? templateVarValue : target ) ) ,
} as unknown as TemplateSrv ;
const ds = getMockInfluxDS ( getMockDSInstanceSettings ( ) , templateSrvStub ) ;
ds . version = InfluxVersion . SQL ;
const adhocFilter : AdHocVariableFilter [ ] = [ { key : 'bar' , value : templateVarName , operator : '=' } ] ;
const result = ds . applyTemplateVariables ( mockInfluxQueryRequest ( ) as unknown as InfluxQuery , { } , adhocFilter ) ;
expect ( result . tags ! [ 0 ] . value ) . toBe ( templateVarValue ) ;
} ) ;
} ) ;