DashboardScene: Fixes issue with variables without current property (#78481)

This commit is contained in:
Torkel Ödegaard 2023-11-22 09:40:39 +01:00 committed by GitHub
parent 5a19813771
commit 393d103782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View File

@ -679,6 +679,7 @@ describe('transformSaveModelToScene', () => {
error: null, error: null,
description: null, description: null,
}; };
const migrated = createSceneVariableFromVariableModel(variable); const migrated = createSceneVariableFromVariableModel(variable);
const { key, ...rest } = migrated.state; const { key, ...rest } = migrated.state;
expect(rest).toEqual({ expect(rest).toEqual({
@ -696,6 +697,7 @@ describe('transformSaveModelToScene', () => {
value: '1m', value: '1m',
}); });
}); });
it.each(['textbox', 'system'])('should throw for unsupported (yet) variables', (type) => { it.each(['textbox', 'system'])('should throw for unsupported (yet) variables', (type) => {
const variable = { const variable = {
name: 'query0', name: 'query0',
@ -704,6 +706,42 @@ describe('transformSaveModelToScene', () => {
expect(() => createSceneVariableFromVariableModel(variable as TypedVariableModel)).toThrow(); expect(() => createSceneVariableFromVariableModel(variable as TypedVariableModel)).toThrow();
}); });
it('should handle variable without current', () => {
// @ts-expect-error
const variable: TypedVariableModel = {
id: 'query1',
name: 'query1',
type: 'datasource',
global: false,
regex: '/^gdev/',
options: [],
query: 'prometheus',
multi: true,
includeAll: true,
refresh: 1,
allValue: 'Custom all',
};
const migrated = createSceneVariableFromVariableModel(variable);
const { key, ...rest } = migrated.state;
expect(migrated).toBeInstanceOf(DataSourceVariable);
expect(rest).toEqual({
allValue: 'Custom all',
defaultToAll: true,
includeAll: true,
label: undefined,
name: 'query1',
options: [],
pluginId: 'prometheus',
regex: '/^gdev/',
text: '',
type: 'datasource',
value: '',
isMulti: true,
});
});
}); });
describe('Repeating rows', () => { describe('Repeating rows', () => {

View File

@ -274,8 +274,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode
if (variable.type === 'custom') { if (variable.type === 'custom') {
return new CustomVariable({ return new CustomVariable({
...commonProperties, ...commonProperties,
value: variable.current.value, value: variable.current?.value ?? '',
text: variable.current.text, text: variable.current?.text ?? '',
description: variable.description, description: variable.description,
query: variable.query, query: variable.query,
isMulti: variable.multi, isMulti: variable.multi,
@ -288,8 +288,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode
} else if (variable.type === 'query') { } else if (variable.type === 'query') {
return new QueryVariable({ return new QueryVariable({
...commonProperties, ...commonProperties,
value: variable.current.value, value: variable.current?.value ?? '',
text: variable.current.text, text: variable.current?.text ?? '',
description: variable.description, description: variable.description,
query: variable.query, query: variable.query,
datasource: variable.datasource, datasource: variable.datasource,
@ -306,8 +306,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode
} else if (variable.type === 'datasource') { } else if (variable.type === 'datasource') {
return new DataSourceVariable({ return new DataSourceVariable({
...commonProperties, ...commonProperties,
value: variable.current.value, value: variable.current?.value ?? '',
text: variable.current.text, text: variable.current?.text ?? '',
description: variable.description, description: variable.description,
regex: variable.regex, regex: variable.regex,
pluginId: variable.query, pluginId: variable.query,