@ -22,7 +22,7 @@ import {
initQueryVariableEditor ,
initQueryVariableEditor ,
updateQueryVariableOptions ,
updateQueryVariableOptions ,
} from './actions' ;
} from './actions' ;
import { updateVariableOptions , updateVariableTags } from './reducer' ;
import { updateVariableOptions } from './reducer' ;
import {
import {
addVariableEditorError ,
addVariableEditorError ,
changeVariableEditorExtended ,
changeVariableEditorExtended ,
@ -78,64 +78,12 @@ describe('query actions', () => {
variableAdapters . setInit ( ( ) = > [ createQueryVariableAdapter ( ) ] ) ;
variableAdapters . setInit ( ( ) = > [ createQueryVariableAdapter ( ) ] ) ;
describe ( 'when updateQueryVariableOptions is dispatched for variable with tags and includeAll' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const tagsMetrics = [ createMetric ( 'tagA' ) , createMetric ( 'tagB' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , tagsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. whenActionIsDispatched ( addVariable ( toVariablePayload ( variable , { global : false , index : 0 , model : variable } ) ) )
. whenAsyncActionIsDispatched ( updateQueryVariableOptions ( toVariablePayload ( variable ) ) , true ) ;
const option = createOption ( ALL_VARIABLE_TEXT , ALL_VARIABLE_VALUE ) ;
const update = { results : optionsMetrics , templatedRegex : '' } ;
tester . thenDispatchedActionsShouldEqual (
updateVariableOptions ( toVariablePayload ( variable , update ) ) ,
updateVariableTags ( toVariablePayload ( variable , tagsMetrics ) ) ,
setCurrentVariableValue ( toVariablePayload ( variable , { option } ) )
) ;
} ) ;
} ) ;
describe ( 'when updateQueryVariableOptions is dispatched for variable with tags' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : false } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const tagsMetrics = [ createMetric ( 'tagA' ) , createMetric ( 'tagB' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , tagsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. whenActionIsDispatched ( addVariable ( toVariablePayload ( variable , { global : false , index : 0 , model : variable } ) ) )
. whenAsyncActionIsDispatched ( updateQueryVariableOptions ( toVariablePayload ( variable ) ) , true ) ;
const option = createOption ( 'A' ) ;
const update = { results : optionsMetrics , templatedRegex : '' } ;
tester . thenDispatchedActionsPredicateShouldEqual ( ( actions ) = > {
const [ updateOptions , updateTags , setCurrentAction ] = actions ;
const expectedNumberOfActions = 3 ;
expect ( updateOptions ) . toEqual ( updateVariableOptions ( toVariablePayload ( variable , update ) ) ) ;
expect ( updateTags ) . toEqual ( updateVariableTags ( toVariablePayload ( variable , tagsMetrics ) ) ) ;
expect ( setCurrentAction ) . toEqual ( setCurrentVariableValue ( toVariablePayload ( variable , { option } ) ) ) ;
return actions . length === expectedNumberOfActions ;
} ) ;
} ) ;
} ) ;
describe ( 'when updateQueryVariableOptions is dispatched for variable without both tags and includeAll' , ( ) = > {
describe ( 'when updateQueryVariableOptions is dispatched for variable without both tags and includeAll' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : false , useTags : false } ) ;
const variable = createVariable ( { includeAll : false } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( variable , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -154,10 +102,10 @@ describe('query actions', () => {
describe ( 'when updateQueryVariableOptions is dispatched for variable with includeAll but without tags' , ( ) = > {
describe ( 'when updateQueryVariableOptions is dispatched for variable with includeAll but without tags' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( variable , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -180,10 +128,10 @@ describe('query actions', () => {
describe ( 'when updateQueryVariableOptions is dispatched for variable open in editor' , ( ) = > {
describe ( 'when updateQueryVariableOptions is dispatched for variable open in editor' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( variable , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -208,10 +156,10 @@ describe('query actions', () => {
describe ( 'when updateQueryVariableOptions is dispatched for variable with searchFilter' , ( ) = > {
describe ( 'when updateQueryVariableOptions is dispatched for variable with searchFilter' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
mockDatasourceMetrics ( variable , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( variable , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -235,7 +183,7 @@ describe('query actions', () => {
describe ( 'when updateQueryVariableOptions is dispatched and fails for variable open in editor' , ( ) = > {
describe ( 'when updateQueryVariableOptions is dispatched and fails for variable open in editor' , ( ) = > {
silenceConsoleOutput ( ) ;
silenceConsoleOutput ( ) ;
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const error = { message : 'failed to fetch metrics' } ;
const error = { message : 'failed to fetch metrics' } ;
mocks [ variable . datasource ! ] . metricFindQuery = jest . fn ( ( ) = > Promise . reject ( error ) ) ;
mocks [ variable . datasource ! ] . metricFindQuery = jest . fn ( ( ) = > Promise . reject ( error ) ) ;
@ -267,7 +215,7 @@ describe('query actions', () => {
describe ( 'when initQueryVariableEditor is dispatched' , ( ) = > {
describe ( 'when initQueryVariableEditor is dispatched' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const testMetricSource = { name : 'test' , value : 'test' , meta : { } } ;
const testMetricSource = { name : 'test' , value : 'test' , meta : { } } ;
const editor = { } ;
const editor = { } ;
@ -296,7 +244,7 @@ describe('query actions', () => {
describe ( 'when initQueryVariableEditor is dispatched and metricsource without value is available' , ( ) = > {
describe ( 'when initQueryVariableEditor is dispatched and metricsource without value is available' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const testMetricSource = { name : 'test' , value : ( null as unknown ) as string , meta : { } } ;
const testMetricSource = { name : 'test' , value : ( null as unknown ) as string , meta : { } } ;
const editor = { } ;
const editor = { } ;
@ -325,7 +273,7 @@ describe('query actions', () => {
describe ( 'when initQueryVariableEditor is dispatched and no metric sources was found' , ( ) = > {
describe ( 'when initQueryVariableEditor is dispatched and no metric sources was found' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { includeAll : true , useTags : false } ) ;
const variable = createVariable ( { includeAll : true } ) ;
const editor = { } ;
const editor = { } ;
mocks . dataSourceSrv . getList = jest . fn ( ) . mockReturnValue ( [ ] ) ;
mocks . dataSourceSrv . getList = jest . fn ( ) . mockReturnValue ( [ ] ) ;
@ -433,13 +381,12 @@ describe('query actions', () => {
describe ( 'when changeQueryVariableQuery is dispatched' , ( ) = > {
describe ( 'when changeQueryVariableQuery is dispatched' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const tagsMetrics = [ createMetric ( 'tagA' ) , createMetric ( 'tagB' ) ] ;
const variable = createVariable ( { datasource : 'datasource' , includeAll : true } ) ;
const variable = createVariable ( { datasource : 'datasource' , useTags : true , includeAll : true } ) ;
const query = '$datasource' ;
const query = '$datasource' ;
const definition = 'depends on datasource variable' ;
const definition = 'depends on datasource variable' ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics , tagsMetrics ) ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -455,7 +402,6 @@ describe('query actions', () => {
changeVariableProp ( toVariablePayload ( variable , { propName : 'definition' , propValue : definition } ) ) ,
changeVariableProp ( toVariablePayload ( variable , { propName : 'definition' , propValue : definition } ) ) ,
variableStateFetching ( toVariablePayload ( variable ) ) ,
variableStateFetching ( toVariablePayload ( variable ) ) ,
updateVariableOptions ( toVariablePayload ( variable , update ) ) ,
updateVariableOptions ( toVariablePayload ( variable , update ) ) ,
updateVariableTags ( toVariablePayload ( variable , tagsMetrics ) ) ,
setCurrentVariableValue ( toVariablePayload ( variable , { option } ) ) ,
setCurrentVariableValue ( toVariablePayload ( variable , { option } ) ) ,
variableStateCompleted ( toVariablePayload ( variable ) )
variableStateCompleted ( toVariablePayload ( variable ) )
) ;
) ;
@ -465,12 +411,12 @@ describe('query actions', () => {
describe ( 'when changeQueryVariableQuery is dispatched for variable without tags' , ( ) = > {
describe ( 'when changeQueryVariableQuery is dispatched for variable without tags' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const variable = createVariable ( { datasource : 'datasource' , useTags : false , includeAll : true } ) ;
const variable = createVariable ( { datasource : 'datasource' , includeAll : true } ) ;
const query = '$datasource' ;
const query = '$datasource' ;
const definition = 'depends on datasource variable' ;
const definition = 'depends on datasource variable' ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -495,11 +441,11 @@ describe('query actions', () => {
describe ( 'when changeQueryVariableQuery is dispatched for variable without tags and all' , ( ) = > {
describe ( 'when changeQueryVariableQuery is dispatched for variable without tags and all' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const optionsMetrics = [ createMetric ( 'A' ) , createMetric ( 'B' ) ] ;
const variable = createVariable ( { datasource : 'datasource' , useTags : false , includeAll : false } ) ;
const variable = createVariable ( { datasource : 'datasource' , includeAll : false } ) ;
const query = '$datasource' ;
const query = '$datasource' ;
const definition = 'depends on datasource variable' ;
const definition = 'depends on datasource variable' ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics , [ ] ) ;
mockDatasourceMetrics ( { . . . variable , query } , optionsMetrics ) ;
const tester = await reduxTester < RootReducerType > ( )
const tester = await reduxTester < RootReducerType > ( )
. givenRootReducer ( getRootReducer ( ) )
. givenRootReducer ( getRootReducer ( ) )
@ -523,7 +469,7 @@ describe('query actions', () => {
describe ( 'when changeQueryVariableQuery is dispatched with invalid query' , ( ) = > {
describe ( 'when changeQueryVariableQuery is dispatched with invalid query' , ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
it ( 'then correct actions are dispatched' , async ( ) = > {
const variable = createVariable ( { datasource : 'datasource' , useTags : false , includeAll : false } ) ;
const variable = createVariable ( { datasource : 'datasource' , includeAll : false } ) ;
const query = ` $ ${ variable . name } ` ;
const query = ` $ ${ variable . name } ` ;
const definition = 'depends on datasource variable' ;
const definition = 'depends on datasource variable' ;
@ -701,10 +647,9 @@ describe('query actions', () => {
} ) ;
} ) ;
} ) ;
} ) ;
function mockDatasourceMetrics ( variable : QueryVariableModel , optionsMetrics : any [ ] , tagsMetrics : any [ ] ) {
function mockDatasourceMetrics ( variable : QueryVariableModel , optionsMetrics : any [ ] ) {
const metrics : Record < string , any [ ] > = {
const metrics : Record < string , any [ ] > = {
[ variable . query ] : optionsMetrics ,
[ variable . query ] : optionsMetrics ,
[ variable . tagsQuery ] : tagsMetrics ,
} ;
} ;
const { metricFindQuery } = mocks [ variable . datasource ! ] ;
const { metricFindQuery } = mocks [ variable . datasource ! ] ;
@ -729,10 +674,6 @@ function createVariable(extend?: Partial<QueryVariableModel>): QueryVariableMode
datasource : 'datasource' ,
datasource : 'datasource' ,
definition : '' ,
definition : '' ,
sort : VariableSort.alphabeticalAsc ,
sort : VariableSort.alphabeticalAsc ,
tags : [ ] ,
tagsQuery : 'tags-query' ,
tagValuesQuery : '' ,
useTags : true ,
refresh : VariableRefresh.onDashboardLoad ,
refresh : VariableRefresh.onDashboardLoad ,
regex : '' ,
regex : '' ,
multi : true ,
multi : true ,