Skip to content

Commit

Permalink
fix: naming, limit recursive depth and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Jan 20, 2021
1 parent a2972a0 commit dac6d17
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Partition - Legend item extra values should return all extra values in nested legend 1`] = `Object {}`;

exports[`Partition - Legend item extra values should return extra values in nested legend within max depth of 1 1`] = `Object {}`;

exports[`Partition - Legend item extra values should return extra values in nested legend within max depth of 2 1`] = `Object {}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Store } from 'redux';

import { MockSeriesSpec, MockGlobalSpec } from '../../../../mocks/specs';
import { MockStore } from '../../../../mocks/store';
import { GlobalChartState } from '../../../../state/chart_state';
import { PrimitiveValue } from '../../layout/utils/group_by_rollup';
import { getLegendItemsExtra } from './get_legend_items_extra';

describe('Partition - Legend item extra values', () => {
type TestDatum = [string, string, string, number];
const spec = MockSeriesSpec.sunburst({
data: [
['aaa', 'aa', '1', 1],
['aaa', 'aa', '1', 2],
['aaa', 'aa', '3', 1],
['aaa', 'bb', '4', 1],
['aaa', 'bb', '5', 1],
['aaa', 'bb', '6', 1],
['bbb', 'aa', '7', 1],
['bbb', 'aa', '8', 1],
['bbb', 'bb', '9', 1],
['bbb', 'bb', '10', 1],
['bbb', 'cc', '11', 1],
['bbb', 'cc', '12', 1],
],
valueAccessor: (d: TestDatum) => d[3],
layers: [
{
groupByRollup: (datum: TestDatum) => datum[0],
nodeLabel: (d: PrimitiveValue) => String(d),
},
{
groupByRollup: (datum: TestDatum) => datum[1],
nodeLabel: (d: PrimitiveValue) => String(d),
},
{
groupByRollup: (datum: TestDatum) => datum[2],
nodeLabel: (d: PrimitiveValue) => String(d),
},
],
});
let store: Store<GlobalChartState>;

beforeEach(() => {
store = MockStore.default();
});

it('should return all extra values in nested legend', () => {
MockStore.addSpecs([spec], store);

const extraValues = getLegendItemsExtra(store.getState());
expect([...extraValues.keys()]).toEqual([
'0',
'0__0',
'0__0__0',
'0__0__0__0',
'0__0__0__1',
'0__0__1',
'0__0__1__0',
'0__0__1__1',
'0__0__1__2',
'0__1',
'0__1__0',
'0__1__0__0',
'0__1__0__1',
'0__1__1',
'0__1__1__0',
'0__1__1__1',
'0__1__2',
'0__1__2__0',
'0__1__2__1',
]);
expect(extraValues.values()).toMatchSnapshot();
});

it('should return extra values in nested legend within max depth of 1', () => {
const settings = MockGlobalSpec.settings({ legendMaxDepth: 1 });
MockStore.addSpecs([settings, spec], store);

const extraValues = getLegendItemsExtra(store.getState());
expect([...extraValues.keys()]).toEqual(['0', '0__0', '0__1']);
expect(extraValues.values()).toMatchSnapshot();
});

it('should return extra values in nested legend within max depth of 2', () => {
const settings = MockGlobalSpec.settings({ legendMaxDepth: 2 });
MockStore.addSpecs([settings, spec], store);

const extraValues = getLegendItemsExtra(store.getState());
expect([...extraValues.keys()]).toEqual([
'0',
'0__0',
'0__0__0',
'0__0__1',
'0__1',
'0__1__0',
'0__1__1',
'0__1__2',
]);
expect(extraValues.values()).toMatchSnapshot();
});

it('filters all extraValues is depth is 0', () => {
const settings = MockGlobalSpec.settings({ legendMaxDepth: 0 });
MockStore.addSpecs([settings, spec], store);

const extraValues = getLegendItemsExtra(store.getState());
expect([...extraValues.keys()]).toEqual([]);
});

it('filters all extraValues is depth is NaN', () => {
const settings = MockGlobalSpec.settings({ legendMaxDepth: NaN });
MockStore.addSpecs([settings, spec], store);

const extraValues = getLegendItemsExtra(store.getState());
expect([...extraValues.keys()]).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ export const getLegendItemsExtra = createCachedSelector(
(pieSpec, { legendMaxDepth }, tree): Map<SeriesKey, LegendItemExtraValues> => {
const legendExtraValues = new Map<SeriesKey, LegendItemExtraValues>();

return pieSpec && !isInvalidLegendMaxDepth(legendMaxDepth) ? getExtraValueMap(pieSpec, tree) : legendExtraValues;
return pieSpec && isValidLegendMaxDepth(legendMaxDepth)
? getExtraValueMap(pieSpec, tree, legendMaxDepth)
: legendExtraValues;
},
)(getChartIdSelector);

/**
* Check if the legendMaxDepth from settings is not a valid number (NaN or <=0)
* Check if the legendMaxDepth from settings is a valid number (NaN or <=0)
*
* @param legendMaxDepth - SettingsSpec['legendMaxDepth']
*/
function isInvalidLegendMaxDepth(legendMaxDepth: SettingsSpec['legendMaxDepth']): boolean {
return typeof legendMaxDepth === 'number' && (Number.isNaN(legendMaxDepth) || legendMaxDepth <= 0);
function isValidLegendMaxDepth(legendMaxDepth: SettingsSpec['legendMaxDepth']): boolean {
return typeof legendMaxDepth === 'number' && !Number.isNaN(legendMaxDepth) && legendMaxDepth > 0;
}

/**
Expand All @@ -54,6 +56,8 @@ function isInvalidLegendMaxDepth(legendMaxDepth: SettingsSpec['legendMaxDepth'])
function getExtraValueMap(
{ layers, valueFormatter }: Pick<PartitionSpec, 'layers' | 'valueFormatter'>,
tree: HierarchyOfArrays,
maxDepth: number,
depth: number = 0,
keys: Map<SeriesKey, LegendItemExtraValues> = new Map(),
): Map<SeriesKey, LegendItemExtraValues> {
for (let i = 0; i < tree.length; i++) {
Expand All @@ -69,7 +73,9 @@ function getExtraValueMap(
keys.set(path.map(({ index }) => index).join('__'), values);
}

getExtraValueMap({ layers, valueFormatter }, children, keys);
if (depth < maxDepth) {
getExtraValueMap({ layers, valueFormatter }, children, maxDepth, depth + 1, keys);
}
}
return keys;
}

0 comments on commit dac6d17

Please sign in to comment.