@@ -5,22 +5,26 @@ import { mockDatasource, mockQuery } from './__mocks__/datasource';
55
66describe ( 'DataSource' , ( ) => {
77 describe ( 'applyTemplateVariables' , ( ) => {
8- const scopedVars : Record < string , any > = {
9- $simple : 'foo' ,
10- $multiple : [ 'foo' , 'bar' ] ,
8+ const scopedVars : ScopedVars = {
9+ $simple : { value : 'foo' } ,
10+ $multiple : { value : [ 'foo' , 'bar' ] } ,
11+ __interval_ms : { value : 5000000 } ,
12+ __interval : { value : 50000 } ,
1113 } ;
1214 // simplified version of getTemplateSrv().replace
13- const replace = jest . fn ( ( target ?: string , scopedVars ?: ScopedVars , format ?: string | Function ) => {
14- let res = target ?? '' ;
15- if ( scopedVars && typeof format === 'function' ) {
16- Object . keys ( scopedVars ) . forEach ( ( v ) => ( res = res . replace ( v , format ( scopedVars [ v ] ) ) ) ) ;
15+ const replaceMock = jest . fn ( ) . mockImplementation (
16+ ( target ?: string , scopedVars ?: ScopedVars , format ?: string | Function ) => {
17+ let res = target ?? '' ;
18+ if ( scopedVars && typeof format === 'function' ) {
19+ Object . keys ( scopedVars ) . forEach ( ( v ) => ( res = res . replace ( v , format ( scopedVars [ v ] ?. value ) ) ) ) ;
20+ }
21+ return res ;
1722 }
18- return res ;
19- } ) ;
23+ ) ;
2024 beforeEach ( ( ) => {
2125 jest . spyOn ( runtime , 'getTemplateSrv' ) . mockImplementation ( ( ) => ( {
2226 getVariables : jest . fn ( ) ,
23- replace : replace ,
27+ replace : replaceMock ,
2428 containsTemplate : jest . fn ( ) ,
2529 updateTimeRange : jest . fn ( ) ,
2630 } ) ) ;
@@ -41,5 +45,18 @@ describe('DataSource', () => {
4145 ) ;
4246 expect ( res . rawQuery ) . toEqual ( `select * from foo where var in ('foo','bar')` ) ;
4347 } ) ;
48+
49+ it ( 'should replace __interval interpolated variables with their original string' , ( ) => {
50+ mockDatasource . applyTemplateVariables (
51+ { ...mockQuery , rawQuery : 'select $__interval_ms, $__interval' } ,
52+ {
53+ __interval_ms : { value : 5000000 } ,
54+ __interval : { value : 50000 } ,
55+ }
56+ ) ;
57+ // check rawQuery.replace is called with correct interval value
58+ expect ( replaceMock . mock . calls [ 3 ] [ 1 ] . __interval ) . toEqual ( { value : '$__interval' } ) ;
59+ expect ( replaceMock . mock . calls [ 3 ] [ 1 ] . __interval_ms ) . toEqual ( { value : '$__interval_ms' } ) ;
60+ } ) ;
4461 } ) ;
4562} ) ;
0 commit comments