From e341d4b26f50115ccacbcde6069c19e799cbc91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Mon, 11 May 2020 10:47:05 +0200 Subject: [PATCH] Reduce Transform: sort order is preserved as entered by user (#24494) --- .../grafana-data/src/utils/Registry.test.ts | 31 +++++++++++++++++++ packages/grafana-data/src/utils/Registry.ts | 14 ++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 packages/grafana-data/src/utils/Registry.test.ts diff --git a/packages/grafana-data/src/utils/Registry.test.ts b/packages/grafana-data/src/utils/Registry.test.ts new file mode 100644 index 00000000000..a979c2c6103 --- /dev/null +++ b/packages/grafana-data/src/utils/Registry.test.ts @@ -0,0 +1,31 @@ +import { Registry } from './Registry'; +import { FieldReducerInfo, fieldReducers, ReducerID } from '../transformations'; + +describe('Registry', () => { + describe('selectOptions', () => { + describe('when called with current', () => { + it('then order in select.current should be same as current', () => { + const list = fieldReducers.list(); + const registry = new Registry(() => list); + const current = [ReducerID.step, ReducerID.mean, ReducerID.allIsZero, ReducerID.first, ReducerID.delta]; + const select = registry.selectOptions(current); + expect(select.current).toEqual([ + { description: 'Minimum interval between values', label: 'Step', value: 'step' }, + { description: 'Average Value', label: 'Mean', value: 'mean' }, + { description: 'All values are zero', label: 'All Zeros', value: 'allIsZero' }, + { description: 'First Value', label: 'First', value: 'first' }, + { description: 'Cumulative change in value', label: 'Delta', value: 'delta' }, + ]); + }); + + describe('when called without current', () => { + it('then it should return an empty array', () => { + const list = fieldReducers.list(); + const registry = new Registry(() => list); + const select = registry.selectOptions(); + expect(select.current).toEqual([]); + }); + }); + }); + }); +}); diff --git a/packages/grafana-data/src/utils/Registry.ts b/packages/grafana-data/src/utils/Registry.ts index 9ac7f0c5e4c..6dadbef2c82 100644 --- a/packages/grafana-data/src/utils/Registry.ts +++ b/packages/grafana-data/src/utils/Registry.ts @@ -84,10 +84,10 @@ export class Registry { current: [], } as RegistrySelectInfo; - const currentIds: any = {}; + const currentOptions: Record> = {}; if (current) { for (const id of current) { - currentIds[id] = true; + currentOptions[id] = {}; } } @@ -106,10 +106,16 @@ export class Registry { }; select.options.push(option); - if (currentIds[ext.id]) { - select.current.push(option); + if (currentOptions[ext.id]) { + currentOptions[ext.id] = option; } } + + if (current) { + // this makes sure we preserve the order of ids + select.current = Object.values(currentOptions); + } + return select; }