diff --git a/public/app/features/variables/pickers/OptionsPicker/actions.test.ts b/public/app/features/variables/pickers/OptionsPicker/actions.test.ts index d1f51c6ee23..b4417488024 100644 --- a/public/app/features/variables/pickers/OptionsPicker/actions.test.ts +++ b/public/app/features/variables/pickers/OptionsPicker/actions.test.ts @@ -418,7 +418,7 @@ function createMultiVariable(extend?: Partial): QueryVariabl tagsQuery: 'tags-query', tagValuesQuery: '', useTags: true, - refresh: VariableRefresh.onDashboardLoad, + refresh: VariableRefresh.never, regex: '', multi: true, includeAll: true, diff --git a/public/app/features/variables/pickers/OptionsPicker/actions.ts b/public/app/features/variables/pickers/OptionsPicker/actions.ts index 42c89affba7..585c570ab66 100644 --- a/public/app/features/variables/pickers/OptionsPicker/actions.ts +++ b/public/app/features/variables/pickers/OptionsPicker/actions.ts @@ -144,7 +144,7 @@ const fetchTagValues = (tagText: string): ThunkResult> => { }; const getTimeRange = (variable: QueryVariableModel) => { - if (variable.refresh === VariableRefresh.onTimeRangeChanged) { + if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) { return getTimeSrv().timeRange(); } return undefined; diff --git a/public/app/features/variables/query/queryRunners.test.ts b/public/app/features/variables/query/queryRunners.test.ts index a31621b151a..bef2104d212 100644 --- a/public/app/features/variables/query/queryRunners.test.ts +++ b/public/app/features/variables/query/queryRunners.test.ts @@ -70,6 +70,41 @@ describe('QueryRunners', () => { }); }); + describe('and calling runRequest with a variable that refreshes on dashboard load', () => { + const { datasource, runner, runnerArgs, request, timeSrv, defaultTimeRange } = getLegacyTestContext({ + query: 'A query', + refresh: VariableRefresh.onDashboardLoad, + }); + const observable = runner.runRequest(runnerArgs, request); + + it('then it should return correct observable', async () => { + await expect(observable).toEmitValuesWith((received) => { + const value = received[0]; + expect(value).toEqual({ + series: [{ text: 'A', value: 'A' }], + state: 'Done', + timeRange: defaultTimeRange, + }); + }); + }); + + it('and it should call timeSrv.timeRange()', () => { + expect(timeSrv.timeRange).toHaveBeenCalledTimes(1); + }); + + it('and it should call metricFindQuery with correct options', () => { + expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1); + expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', { + range: defaultTimeRange, + searchFilter: 'A searchFilter', + variable: { + query: 'A query', + refresh: VariableRefresh.onDashboardLoad, + }, + }); + }); + }); + describe('and calling runRequest with a variable that does not refresh when time range changes', () => { const { datasource, runner, runnerArgs, request, timeSrv } = getLegacyTestContext({ query: 'A query', diff --git a/public/app/features/variables/utils.ts b/public/app/features/variables/utils.ts index 76dd337073a..25eef231040 100644 --- a/public/app/features/variables/utils.ts +++ b/public/app/features/variables/utils.ts @@ -137,7 +137,7 @@ export function getTemplatedRegex(variable: QueryVariableModel, templateSrv = ge export function getLegacyQueryOptions(variable: QueryVariableModel, searchFilter?: string, timeSrv = getTimeSrv()) { const queryOptions: any = { range: undefined, variable, searchFilter }; - if (variable.refresh === VariableRefresh.onTimeRangeChanged) { + if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) { queryOptions.range = timeSrv.timeRange(); }