From ee247e33b4e3887d3c0623a3f33b6416fc7b4c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 19 Apr 2023 13:47:55 +0200 Subject: [PATCH] AngularMigration: Clear old angular panel props when auto migrating (#66719) * AngularMigration: Clear old angular panel props when auto migrating * Update test name --- .betterer.results | 4 +- .../dashboard/state/PanelModel.test.ts | 38 +++++++++++++++++++ .../features/dashboard/state/PanelModel.ts | 18 +++++---- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/.betterer.results b/.betterer.results index d49d92f14c5..38d4720c438 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2592,7 +2592,9 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"], - [0, 0, 0, "Unexpected any. Specify a different type.", "5"] + [0, 0, 0, "Unexpected any. Specify a different type.", "5"], + [0, 0, 0, "Unexpected any. Specify a different type.", "6"], + [0, 0, 0, "Unexpected any. Specify a different type.", "7"] ], "public/app/features/dashboard/state/PanelModel.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/public/app/features/dashboard/state/PanelModel.test.ts b/public/app/features/dashboard/state/PanelModel.test.ts index 70aa401a1bd..ba83da44802 100644 --- a/public/app/features/dashboard/state/PanelModel.test.ts +++ b/public/app/features/dashboard/state/PanelModel.test.ts @@ -352,6 +352,44 @@ describe('PanelModel', () => { }); }); + describe('when autoMigrateFrom angular to react', () => { + const onPanelTypeChanged = (panel: PanelModel, prevPluginId: string, prevOptions: Record) => { + panel.fieldConfig = { defaults: { unit: 'bytes' }, overrides: [] }; + return { name: prevOptions.angular.oldName }; + }; + + const reactPlugin = getPanelPlugin({ id: 'timeseries' }) + .setPanelChangeHandler(onPanelTypeChanged as any) + .useFieldConfig({ + disableStandardOptions: [FieldConfigProperty.Thresholds], + }) + .setPanelOptions((builder) => { + builder.addTextInput({ + name: 'Name', + path: 'name', + }); + }); + + beforeEach(() => { + model = new PanelModel({ + autoMigrateFrom: 'graph', + oldName: 'old name', + type: 'timeseries', + }); + + model.pluginLoaded(reactPlugin); + }); + + it('should run panel changed handler and remove old model props', () => { + expect(model.options).toEqual({ name: 'old name' }); + expect(model.fieldConfig).toEqual({ defaults: { unit: 'bytes' }, overrides: [] }); + expect(model.autoMigrateFrom).toBe(undefined); + expect(model.oldName).toBe(undefined); + expect(model.plugin).toBe(reactPlugin); + expect(model.type).toBe('timeseries'); + }); + }); + describe('variables interpolation', () => { let panelQueryRunner: any; diff --git a/public/app/features/dashboard/state/PanelModel.ts b/public/app/features/dashboard/state/PanelModel.ts index bbb3f10e42f..a38cafe238d 100644 --- a/public/app/features/dashboard/state/PanelModel.ts +++ b/public/app/features/dashboard/state/PanelModel.ts @@ -416,18 +416,22 @@ export class PanelModel implements DataConfigSource, IPanelModel { pluginLoaded(plugin: PanelPlugin) { this.plugin = plugin; + const version = getPluginVersion(plugin); if (this.autoMigrateFrom) { const wasAngular = autoMigrateAngular[this.autoMigrateFrom] != null; - this.callPanelTypeChangeHandler( - plugin, - this.autoMigrateFrom, - this.getOptionsToRemember(), // old options - wasAngular - ); + const oldOptions = this.getOptionsToRemember(); + const prevPluginId = this.autoMigrateFrom; + const newPluginId = this.type; - delete this.autoMigrateFrom; + this.clearPropertiesBeforePluginChange(); + + // Need to set these again as they get cleared by the above function + this.type = newPluginId; + this.plugin = plugin; + + this.callPanelTypeChangeHandler(plugin, prevPluginId, oldOptions, wasAngular); } if (plugin.onPanelMigration) {