mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Reduce Transform: sort order is preserved as entered by user (#24494)
This commit is contained in:
parent
c3b6ee1456
commit
e341d4b26f
31
packages/grafana-data/src/utils/Registry.test.ts
Normal file
31
packages/grafana-data/src/utils/Registry.test.ts
Normal file
@ -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<FieldReducerInfo>(() => 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<FieldReducerInfo>(() => list);
|
||||||
|
const select = registry.selectOptions();
|
||||||
|
expect(select.current).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -84,10 +84,10 @@ export class Registry<T extends RegistryItem> {
|
|||||||
current: [],
|
current: [],
|
||||||
} as RegistrySelectInfo;
|
} as RegistrySelectInfo;
|
||||||
|
|
||||||
const currentIds: any = {};
|
const currentOptions: Record<string, SelectableValue<string>> = {};
|
||||||
if (current) {
|
if (current) {
|
||||||
for (const id of current) {
|
for (const id of current) {
|
||||||
currentIds[id] = true;
|
currentOptions[id] = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,10 +106,16 @@ export class Registry<T extends RegistryItem> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
select.options.push(option);
|
select.options.push(option);
|
||||||
if (currentIds[ext.id]) {
|
if (currentOptions[ext.id]) {
|
||||||
select.current.push(option);
|
currentOptions[ext.id] = option;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (current) {
|
||||||
|
// this makes sure we preserve the order of ids
|
||||||
|
select.current = Object.values(currentOptions);
|
||||||
|
}
|
||||||
|
|
||||||
return select;
|
return select;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user