From 393d10378276ca5ff660581d2a6376d521a22a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 22 Nov 2023 09:40:39 +0100 Subject: [PATCH] DashboardScene: Fixes issue with variables without current property (#78481) --- .../transformSaveModelToScene.test.ts | 38 +++++++++++++++++++ .../transformSaveModelToScene.ts | 12 +++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.test.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.test.ts index c6eac008668..ed2125af64c 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.test.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.test.ts @@ -679,6 +679,7 @@ describe('transformSaveModelToScene', () => { error: null, description: null, }; + const migrated = createSceneVariableFromVariableModel(variable); const { key, ...rest } = migrated.state; expect(rest).toEqual({ @@ -696,6 +697,7 @@ describe('transformSaveModelToScene', () => { value: '1m', }); }); + it.each(['textbox', 'system'])('should throw for unsupported (yet) variables', (type) => { const variable = { name: 'query0', @@ -704,6 +706,42 @@ describe('transformSaveModelToScene', () => { 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', () => { diff --git a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts index 263902d17cb..256507de13d 100644 --- a/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts +++ b/public/app/features/dashboard-scene/serialization/transformSaveModelToScene.ts @@ -274,8 +274,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode if (variable.type === 'custom') { return new CustomVariable({ ...commonProperties, - value: variable.current.value, - text: variable.current.text, + value: variable.current?.value ?? '', + text: variable.current?.text ?? '', description: variable.description, query: variable.query, isMulti: variable.multi, @@ -288,8 +288,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode } else if (variable.type === 'query') { return new QueryVariable({ ...commonProperties, - value: variable.current.value, - text: variable.current.text, + value: variable.current?.value ?? '', + text: variable.current?.text ?? '', description: variable.description, query: variable.query, datasource: variable.datasource, @@ -306,8 +306,8 @@ export function createSceneVariableFromVariableModel(variable: TypedVariableMode } else if (variable.type === 'datasource') { return new DataSourceVariable({ ...commonProperties, - value: variable.current.value, - text: variable.current.text, + value: variable.current?.value ?? '', + text: variable.current?.text ?? '', description: variable.description, regex: variable.regex, pluginId: variable.query,