|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Store } from 'redux'; |
| 21 | + |
| 22 | +import { MockGlobalSpec, MockSeriesSpec } from '../../../../mocks/specs/specs'; |
| 23 | +import { MockStore } from '../../../../mocks/store/store'; |
| 24 | +import { |
| 25 | + HeatmapElementEvent, |
| 26 | + LayerValue, |
| 27 | + PartitionElementEvent, |
| 28 | + XYChartElementEvent, |
| 29 | +} from '../../../../specs/settings'; |
| 30 | +import { onMouseDown, onMouseUp, onPointerMove } from '../../../../state/actions/mouse'; |
| 31 | +import { GlobalChartState } from '../../../../state/chart_state'; |
| 32 | +import { DebugState, PartitionDebugState, SinglePartitionDebugState } from '../../../../state/types'; |
| 33 | +import { PartitionLayout } from '../../layout/types/config_types'; |
| 34 | +import { isSunburst } from '../../layout/viewmodel/viewmodel'; |
| 35 | +import { getDebugStateSelector } from './get_debug_state'; |
| 36 | +import { createOnElementClickCaller } from './on_element_click_caller'; |
| 37 | + |
| 38 | +describe.each([ |
| 39 | + [PartitionLayout.sunburst, 9, 9], |
| 40 | + [PartitionLayout.treemap, 9, 6], |
| 41 | + [PartitionLayout.flame, 9, 6], |
| 42 | + [PartitionLayout.icicle, 9, 6], |
| 43 | + [PartitionLayout.mosaic, 9, 6], |
| 44 | +])('Partition - debug state %s', (partitionLayout, numberOfElements, numberOfCalls) => { |
| 45 | + type TestDatum = { cat1: string; cat2: string; val: number }; |
| 46 | + const specJSON = { |
| 47 | + config: { |
| 48 | + partitionLayout, |
| 49 | + }, |
| 50 | + data: [ |
| 51 | + { cat1: 'Asia', cat2: 'Japan', val: 1 }, |
| 52 | + { cat1: 'Asia', cat2: 'China', val: 1 }, |
| 53 | + { cat1: 'Europe', cat2: 'Germany', val: 1 }, |
| 54 | + { cat1: 'Europe', cat2: 'Italy', val: 1 }, |
| 55 | + { cat1: 'North America', cat2: 'United States', val: 1 }, |
| 56 | + { cat1: 'North America', cat2: 'Canada', val: 1 }, |
| 57 | + ], |
| 58 | + valueAccessor: (d: TestDatum) => d.val, |
| 59 | + layers: [ |
| 60 | + { |
| 61 | + groupByRollup: (d: TestDatum) => d.cat1, |
| 62 | + }, |
| 63 | + { |
| 64 | + groupByRollup: (d: TestDatum) => d.cat2, |
| 65 | + }, |
| 66 | + ], |
| 67 | + }; |
| 68 | + let store: Store<GlobalChartState>; |
| 69 | + let onClickListener: jest.Mock< |
| 70 | + undefined, |
| 71 | + Array<(XYChartElementEvent | PartitionElementEvent | HeatmapElementEvent)[]> |
| 72 | + >; |
| 73 | + let debugState: DebugState; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + onClickListener = jest.fn((): undefined => undefined); |
| 77 | + store = MockStore.default({ width: 500, height: 500, top: 0, left: 0 }); |
| 78 | + const onElementClickCaller = createOnElementClickCaller(); |
| 79 | + store.subscribe(() => { |
| 80 | + onElementClickCaller(store.getState()); |
| 81 | + }); |
| 82 | + MockStore.addSpecs( |
| 83 | + [ |
| 84 | + MockSeriesSpec.sunburst(specJSON), |
| 85 | + MockGlobalSpec.settings({ debugState: true, onElementClick: onClickListener }), |
| 86 | + ], |
| 87 | + store, |
| 88 | + ); |
| 89 | + debugState = getDebugStateSelector(store.getState()); |
| 90 | + }); |
| 91 | + |
| 92 | + it('can compute debug state', () => { |
| 93 | + // small multiple panels |
| 94 | + expect(debugState.partition).toHaveLength(1); |
| 95 | + // partition sectors |
| 96 | + expect(debugState.partition![0].partitions).toHaveLength(numberOfElements); |
| 97 | + }); |
| 98 | + |
| 99 | + it('can click on every sector', () => { |
| 100 | + const [{ partitions }] = debugState.partition as PartitionDebugState[]; |
| 101 | + let counter = 0; |
| 102 | + for (let index = 0; index < partitions.length; index++) { |
| 103 | + const partition = partitions[index]; |
| 104 | + if (!isSunburst(partitionLayout) && partition.depth < 2) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + expectCorrectClickInfo(store, onClickListener, partition, counter); |
| 108 | + counter++; |
| 109 | + } |
| 110 | + expect(onClickListener).toBeCalledTimes(numberOfCalls); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +function expectCorrectClickInfo( |
| 115 | + store: Store<GlobalChartState>, |
| 116 | + onClickListener: jest.Mock<undefined, Array<(XYChartElementEvent | PartitionElementEvent | HeatmapElementEvent)[]>>, |
| 117 | + partition: SinglePartitionDebugState, |
| 118 | + index: number, |
| 119 | +) { |
| 120 | + const { |
| 121 | + depth, |
| 122 | + value, |
| 123 | + name, |
| 124 | + coords: [x, y], |
| 125 | + } = partition; |
| 126 | + |
| 127 | + store.dispatch(onPointerMove({ x, y }, index * 3)); |
| 128 | + store.dispatch(onMouseDown({ x, y }, index * 3 + 1)); |
| 129 | + store.dispatch(onMouseUp({ x, y }, index * 3 + 2)); |
| 130 | + |
| 131 | + expect(onClickListener).toBeCalledTimes(index + 1); |
| 132 | + const obj = onClickListener.mock.calls[index][0][0][0] as LayerValue[]; |
| 133 | + // pick the last element of the path |
| 134 | + expect(obj[obj.length - 1]).toMatchObject({ |
| 135 | + depth, |
| 136 | + groupByRollup: name, |
| 137 | + value, |
| 138 | + }); |
| 139 | +} |
0 commit comments