From 86c7f96fcb98fb0c2b4339657cab65ae684a7c31 Mon Sep 17 00:00:00 2001 From: Victor Marin <36818606+mdvictor@users.noreply.github.com> Date: Wed, 27 Aug 2025 13:59:07 +0300 Subject: [PATCH] TextBoxVariable: Fix change detection for `query` prop (#109895) * fix textbox variable model save on change * better test name * betterer * fix test * fix test --- .betterer.results | 4 +- .../saving/getDashboardChanges.test.ts | 58 +++++++++++++++++++ .../saving/getDashboardChanges.ts | 16 ++++- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/.betterer.results b/.betterer.results index ca0b5eeff6f..398edbbc345 100644 --- a/.betterer.results +++ b/.betterer.results @@ -1765,7 +1765,9 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "3"], [0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Do not use any type assertions.", "5"], - [0, 0, 0, "Unexpected any. Specify a different type.", "6"] + [0, 0, 0, "Do not use any type assertions.", "6"], + [0, 0, 0, "Do not use any type assertions.", "7"], + [0, 0, 0, "Unexpected any. Specify a different type.", "8"] ], "public/app/features/dashboard-scene/scene/PanelMenuBehavior.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"] diff --git a/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts b/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts index ae184d6aecf..a683f551952 100644 --- a/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts +++ b/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts @@ -326,6 +326,64 @@ describe('getDashboardChanges', () => { expect(result).toEqual(expectedChanges); }); + it('should not see any changes on modified textbox var when we do not update variable values', () => { + const newDashboard: Dashboard = { + ...initial, + templating: { + list: [ + { + name: 'var1', + type: 'textbox', + query: '', + current: { + value: 'value1', + text: 'text1', + }, + options: [], + }, + ], + }, + }; + + const changedDashboard: Dashboard = { + ...newDashboard, + templating: { + list: [ + { + name: 'var1', + type: 'textbox', + query: 'query', + current: { + value: 'value1', + text: 'text1', + }, + options: [], + }, + ], + }, + }; + + const expectedChanges = { + initialSaveModel: { + ...newDashboard, + }, + changedSaveModel: { + ...changedDashboard, + }, + diffs: {}, + diffCount: 0, + hasChanges: false, + hasTimeChanges: false, + isNew: false, + hasVariableValueChanges: false, + hasRefreshChange: false, + }; + + const result = getRawDashboardChanges(newDashboard, changedDashboard, false, false, false); + + expect(result).toEqual(expectedChanges); + }); + it('should return the correct result when the variable value changes', () => { const changed = { ...initial, diff --git a/public/app/features/dashboard-scene/saving/getDashboardChanges.ts b/public/app/features/dashboard-scene/saving/getDashboardChanges.ts index 33c046e84b9..1a1617029fd 100644 --- a/public/app/features/dashboard-scene/saving/getDashboardChanges.ts +++ b/public/app/features/dashboard-scene/saving/getDashboardChanges.ts @@ -1,11 +1,12 @@ // @ts-ignore -import type { AdHocVariableModel, TypedVariableModel } from '@grafana/data'; +import type { AdHocVariableModel, TextBoxVariableModel, TypedVariableModel } from '@grafana/data'; import { Dashboard, Panel, VariableOption } from '@grafana/schema'; import { AdHocFilterWithLabels, AdhocVariableSpec, Spec as DashboardV2Spec, + TextVariableSpec, VariableKind, } from '@grafana/schema/dist/esm/schema/dashboard/v2'; import { ResponseTransformers } from 'app/features/dashboard/api/ResponseTransformers'; @@ -217,7 +218,11 @@ export function applyVariableChangesV2( if (!saveVariables) { if (variable.kind === 'AdhocVariable') { variable.spec.filters = (original.spec as AdhocVariableSpec).filters; - } else { + } else if (variable.kind === 'TextVariable') { + variable.spec.query = (original.spec as TextVariableSpec).query; + } + + if (variable.kind !== 'AdhocVariable') { if (hasCurrentValueToSave(variable) && hasCurrentValueToSave(original)) { variable.spec.current = original.spec.current; } @@ -262,9 +267,14 @@ export function applyVariableChanges(saveModel: Dashboard, originalSaveModel: Da if (!saveVariables) { const typed = variable as TypedVariableModel; + if (typed.type === 'adhoc') { typed.filters = (original as AdHocVariableModel).filters; - } else { + } else if (typed.type === 'textbox') { + typed.query = (original as TextBoxVariableModel).query; + } + + if (typed.type !== 'adhoc') { variable.current = original.current; variable.options = original.options; }