|
| 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 | +import { chartStoreReducer, GlobalChartState } from '../../../../state/chart_state'; |
| 20 | +import { createStore, Store } from 'redux'; |
| 21 | +import { PartitionSpec } from '../../specs'; |
| 22 | +import { upsertSpec, specParsed, specParsing } from '../../../../state/actions/specs'; |
| 23 | +import { MockGlobalSpec, MockSeriesSpec } from '../../../../mocks/specs'; |
| 24 | +import { updateParentDimensions } from '../../../../state/actions/chart_settings'; |
| 25 | +import { partitionGeometries } from './geometries'; |
| 26 | +import { onMouseDown, onMouseUp, onPointerMove } from '../../../../state/actions/mouse'; |
| 27 | +import { createOnElementClickCaller } from './on_element_click_caller'; |
| 28 | +import { SettingsSpec, XYChartElementEvent, PartitionElementEvent } from '../../../../specs'; |
| 29 | + |
| 30 | +describe('Picked shapes selector', () => { |
| 31 | + function initStore() { |
| 32 | + const storeReducer = chartStoreReducer('chartId'); |
| 33 | + return createStore(storeReducer); |
| 34 | + } |
| 35 | + function addSeries(store: Store<GlobalChartState>, spec: PartitionSpec, settings?: Partial<SettingsSpec>) { |
| 36 | + store.dispatch(specParsing()); |
| 37 | + store.dispatch(upsertSpec(MockGlobalSpec.settings(settings))); |
| 38 | + store.dispatch(upsertSpec(spec)); |
| 39 | + store.dispatch(specParsed()); |
| 40 | + store.dispatch(updateParentDimensions({ width: 300, height: 300, top: 0, left: 0 })); |
| 41 | + } |
| 42 | + let store: Store<GlobalChartState>; |
| 43 | + let treemapSpec: PartitionSpec; |
| 44 | + let sunburstSpec: PartitionSpec; |
| 45 | + beforeEach(() => { |
| 46 | + store = initStore(); |
| 47 | + const common = { |
| 48 | + valueAccessor: (d: { v: number }) => { |
| 49 | + return d.v; |
| 50 | + }, |
| 51 | + data: [ |
| 52 | + { g1: 'a', g2: 'a', v: 1 }, |
| 53 | + { g1: 'a', g2: 'b', v: 1 }, |
| 54 | + { g1: 'b', g2: 'a', v: 1 }, |
| 55 | + { g1: 'b', g2: 'b', v: 1 }, |
| 56 | + ], |
| 57 | + layers: [ |
| 58 | + { |
| 59 | + groupByRollup: (datum: { g1: string }) => datum.g1, |
| 60 | + }, |
| 61 | + { |
| 62 | + groupByRollup: (datum: { g2: string }) => datum.g2, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }; |
| 66 | + treemapSpec = MockSeriesSpec.treemap(common); |
| 67 | + sunburstSpec = MockSeriesSpec.sunburst(common); |
| 68 | + }); |
| 69 | + test('check initial geoms', () => { |
| 70 | + addSeries(store, treemapSpec); |
| 71 | + const treemapGeometries = partitionGeometries(store.getState()); |
| 72 | + expect(treemapGeometries.quadViewModel).toHaveLength(6); |
| 73 | + |
| 74 | + addSeries(store, sunburstSpec); |
| 75 | + const sunburstGeometries = partitionGeometries(store.getState()); |
| 76 | + expect(sunburstGeometries.quadViewModel).toHaveLength(6); |
| 77 | + }); |
| 78 | + test('treemap check picked geometries', () => { |
| 79 | + const onClickListener = jest.fn<undefined, Array<(XYChartElementEvent | PartitionElementEvent)[]>>( |
| 80 | + (): undefined => undefined, |
| 81 | + ); |
| 82 | + addSeries(store, treemapSpec, { |
| 83 | + onElementClick: onClickListener, |
| 84 | + }); |
| 85 | + const geometries = partitionGeometries(store.getState()); |
| 86 | + expect(geometries.quadViewModel).toHaveLength(6); |
| 87 | + |
| 88 | + const onElementClickCaller = createOnElementClickCaller(); |
| 89 | + store.subscribe(() => { |
| 90 | + onElementClickCaller(store.getState()); |
| 91 | + }); |
| 92 | + store.dispatch(onPointerMove({ x: 200, y: 200 }, 0)); |
| 93 | + store.dispatch(onMouseDown({ x: 200, y: 200 }, 1)); |
| 94 | + store.dispatch(onMouseUp({ x: 200, y: 200 }, 2)); |
| 95 | + expect(onClickListener).toBeCalled(); |
| 96 | + expect(onClickListener.mock.calls[0][0]).toEqual([ |
| 97 | + [ |
| 98 | + [ |
| 99 | + { groupByRollup: 'b', value: 2 }, |
| 100 | + { groupByRollup: 'b', value: 1 }, |
| 101 | + ], |
| 102 | + { |
| 103 | + specId: treemapSpec.id, |
| 104 | + key: `spec{${treemapSpec.id}}`, |
| 105 | + }, |
| 106 | + ], |
| 107 | + ]); |
| 108 | + }); |
| 109 | + test('sunburst check picked geometries', () => { |
| 110 | + const onClickListener = jest.fn<undefined, Array<(XYChartElementEvent | PartitionElementEvent)[]>>( |
| 111 | + (): undefined => undefined, |
| 112 | + ); |
| 113 | + addSeries(store, sunburstSpec, { |
| 114 | + onElementClick: onClickListener, |
| 115 | + }); |
| 116 | + const geometries = partitionGeometries(store.getState()); |
| 117 | + expect(geometries.quadViewModel).toHaveLength(6); |
| 118 | + |
| 119 | + const onElementClickCaller = createOnElementClickCaller(); |
| 120 | + store.subscribe(() => { |
| 121 | + onElementClickCaller(store.getState()); |
| 122 | + }); |
| 123 | + store.dispatch(onPointerMove({ x: 200, y: 200 }, 0)); |
| 124 | + store.dispatch(onMouseDown({ x: 200, y: 200 }, 1)); |
| 125 | + store.dispatch(onMouseUp({ x: 200, y: 200 }, 2)); |
| 126 | + expect(onClickListener).toBeCalled(); |
| 127 | + expect(onClickListener.mock.calls[0][0]).toEqual([ |
| 128 | + [ |
| 129 | + [ |
| 130 | + { groupByRollup: 'b', value: 2 }, |
| 131 | + { groupByRollup: 'b', value: 1 }, |
| 132 | + ], |
| 133 | + { |
| 134 | + specId: sunburstSpec.id, |
| 135 | + key: `spec{${sunburstSpec.id}}`, |
| 136 | + }, |
| 137 | + ], |
| 138 | + ]); |
| 139 | + }); |
| 140 | +}); |
0 commit comments