Dashboards: Query variable editor sets default ds on variable on init (#113843)

* Query variable editor sets default ds on var instead of undefined

* fix old arch tests
This commit is contained in:
Victor Marin
2025-11-13 18:19:10 +02:00
committed by GitHub
parent 1bb9de6f75
commit ed17fb22dc
3 changed files with 28 additions and 1 deletions
@@ -89,6 +89,14 @@ export function QueryVariableEditorForm({
onQueryChange(query);
}
if (!datasourceRef) {
const instanceSettings = getDataSourceSrv().getInstanceSettings({ type: datasource.type, uid: datasource.uid });
if (instanceSettings) {
onDataSourceChange(instanceSettings);
}
}
return { datasource, VariableQueryEditor };
}, [datasourceRef]);
@@ -148,6 +148,20 @@ describe('QueryVariableEditor', () => {
expect(staticOptionsToggle).toBeInTheDocument();
});
it('should update the variable with default datasource when opening editor', async () => {
const onRunQueryMock = jest.fn();
const variable = new QueryVariable({ datasource: undefined, query: '' });
await setup({
variable,
onRunQuery: onRunQueryMock,
});
await waitFor(async () => {
expect(variable.state.datasource).not.toBe(undefined);
});
});
it('should update the variable with default query for the selected DS', async () => {
const onRunQueryMock = jest.fn();
const variable = new QueryVariable({ datasource: { uid: 'mock-ds-2', type: 'test' }, query: '' });
@@ -29,7 +29,12 @@ ds.variables = {
};
const setupTestContext = async (options: Partial<Props>) => {
const variableDefaults: Partial<QueryVariableModel> = { rootStateKey: 'key' };
const variableDefaults: Partial<QueryVariableModel> = {
rootStateKey: 'key',
// adds a default datasource in old arch tests so they continue passing
// in new scenes arch the datasource will be calculated if not provided
datasource: { uid: 'uid', type: 'type' },
};
const extended = {
VariableQueryEditor: LegacyVariableQueryEditor,
dataSource: ds,