Color: Fixes issue where colors where reset to gray when switch panels (#31611)

pull/31681/head
Torkel Ödegaard 4 years ago committed by GitHub
parent 8016b3c5ca
commit 4c5321bd99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      public/app/features/dashboard/state/getPanelOptionsWithDefaults.test.ts
  2. 11
      public/app/features/dashboard/state/getPanelOptionsWithDefaults.ts

@ -8,6 +8,7 @@ import {
standardEditorsRegistry,
standardFieldConfigEditorRegistry,
StandardOptionConfig,
ThresholdsMode,
} from '@grafana/data';
import { getPanelPlugin } from 'app/features/plugins/__mocks__/pluginMocks';
import { mockStandardFieldConfigOptions } from 'test/helpers/fieldConfig';
@ -59,9 +60,7 @@ describe('getPanelOptionsWithDefaults', () => {
expect(result).toMatchInlineSnapshot(`
Object {
"fieldConfig": Object {
"defaults": Object {
"custom": Object {},
},
"defaults": Object {},
"overrides": Array [],
},
"options": Object {},
@ -232,6 +231,30 @@ describe('getPanelOptionsWithDefaults', () => {
});
});
describe('when changing panel type to one that does not use standard field config', () => {
it('should clean defaults', () => {
const plugin = getPanelPlugin({ id: 'graph' });
const result = getPanelOptionsWithDefaults({
plugin,
currentOptions: {},
currentFieldConfig: {
defaults: {
color: { mode: FieldColorModeId.Thresholds },
thresholds: {
mode: ThresholdsMode.Absolute,
steps: [],
},
},
overrides: [],
},
isAfterPluginChange: true,
});
expect(result.fieldConfig.defaults.thresholds).toBeUndefined();
});
});
describe('when applying defaults clean properties that are no longer part of the registry', () => {
it('should remove custom defaults that no longer exist', () => {
const result = runScenario({

@ -105,6 +105,8 @@ export function filterFieldConfigOverrides(
}
function cleanProperties(obj: any, parentPath: string, fieldConfigRegistry: FieldConfigOptionsRegistry) {
let found = false;
for (const propName of Object.keys(obj)) {
const value = obj[propName];
const fullPath = `${parentPath}${propName}`;
@ -112,6 +114,7 @@ function cleanProperties(obj: any, parentPath: string, fieldConfigRegistry: Fiel
// need to check early here as some standard properties have nested properies
if (existsInRegistry) {
found = true;
continue;
}
@ -120,9 +123,15 @@ function cleanProperties(obj: any, parentPath: string, fieldConfigRegistry: Fiel
unset(obj, propName);
}
} else {
cleanProperties(value, `${fullPath}.`, fieldConfigRegistry);
const childPropFound = cleanProperties(value, `${fullPath}.`, fieldConfigRegistry);
// If no child props found unset the main object
if (!childPropFound) {
unset(obj, propName);
}
}
}
return found;
}
function adaptFieldColorMode(

Loading…
Cancel
Save