mirror of
https://github.com/grafana/grafana.git
synced 2025-01-13 09:32:12 -06:00
Templating: use dashboard timerange when variables are set to refresh 'On Dashboard Load' (#31721)
* Templating: use dashboard timerange when variables are set to load 'On Dashboard Load' * Add test
This commit is contained in:
parent
f951672e90
commit
7e0b1f2619
@ -418,7 +418,7 @@ function createMultiVariable(extend?: Partial<QueryVariableModel>): QueryVariabl
|
||||
tagsQuery: 'tags-query',
|
||||
tagValuesQuery: '',
|
||||
useTags: true,
|
||||
refresh: VariableRefresh.onDashboardLoad,
|
||||
refresh: VariableRefresh.never,
|
||||
regex: '',
|
||||
multi: true,
|
||||
includeAll: true,
|
||||
|
@ -144,7 +144,7 @@ const fetchTagValues = (tagText: string): ThunkResult<Promise<string[]>> => {
|
||||
};
|
||||
|
||||
const getTimeRange = (variable: QueryVariableModel) => {
|
||||
if (variable.refresh === VariableRefresh.onTimeRangeChanged) {
|
||||
if (variable.refresh === VariableRefresh.onTimeRangeChanged || variable.refresh === VariableRefresh.onDashboardLoad) {
|
||||
return getTimeSrv().timeRange();
|
||||
}
|
||||
return undefined;
|
||||
|
@ -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',
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user