|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React, { useState } from 'react'; |
| 4 | + |
| 5 | +import useChartModel, { UseChartModelProps } from '../model/use-chart-model'; |
| 6 | +import { ElementWrapper } from '@cloudscape-design/test-utils-core/dom'; |
| 7 | +import { ChartDataTypes } from '../../internal/components/cartesian-chart/interfaces'; |
| 8 | +import { act, render } from '@testing-library/react'; |
| 9 | +import { AreaChartProps } from '../interfaces'; |
| 10 | +import { KeyCode } from '../../internal/keycode'; |
| 11 | +import { useReaction } from '../model/async-store'; |
| 12 | +import { ChartModel } from '../model'; |
| 13 | +import PlotPoint = ChartModel.PlotPoint; |
| 14 | + |
| 15 | +class UseChartModelWrapper extends ElementWrapper { |
| 16 | + static selectors = { |
| 17 | + highlightedPoint: 'highlighted-point', |
| 18 | + highlightedSeries: 'highlighted-series', |
| 19 | + highlightedX: 'highlighted-x', |
| 20 | + }; |
| 21 | + |
| 22 | + findHighlightedPoint() { |
| 23 | + return this.findByClassName(UseChartModelWrapper.selectors.highlightedPoint); |
| 24 | + } |
| 25 | + |
| 26 | + findHighlightedSeries() { |
| 27 | + return this.findByClassName(UseChartModelWrapper.selectors.highlightedSeries); |
| 28 | + } |
| 29 | + |
| 30 | + findHighlightedX() { |
| 31 | + return this.findByClassName(UseChartModelWrapper.selectors.highlightedX); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function RenderChartModelHook(props: UseChartModelProps<ChartDataTypes>) { |
| 36 | + const [highlightedSeries, setHighlightedSeries] = useState<null | AreaChartProps.Series<ChartDataTypes>>(null); |
| 37 | + const [visibleSeries, setVisibleSeries] = useState(props.visibleSeries); |
| 38 | + const [highlightedPoint, setHighlightedPoint] = useState<PlotPoint<ChartDataTypes> | null>(null); |
| 39 | + const [highlightedX, setHighlightedX] = useState<readonly PlotPoint<ChartDataTypes>[] | null>(null); |
| 40 | + |
| 41 | + const { computed, handlers, interactions } = useChartModel({ |
| 42 | + ...props, |
| 43 | + highlightedSeries, |
| 44 | + setHighlightedSeries, |
| 45 | + visibleSeries, |
| 46 | + setVisibleSeries, |
| 47 | + }); |
| 48 | + |
| 49 | + useReaction(interactions, state => state.highlightedPoint, setHighlightedPoint); |
| 50 | + useReaction(interactions, state => state.highlightedX, setHighlightedX); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div |
| 54 | + onFocus={event => { |
| 55 | + handlers.onSVGFocus(event, 'keyboard'); |
| 56 | + }} |
| 57 | + onKeyDown={handlers.onSVGKeyDown} |
| 58 | + tabIndex={-1} |
| 59 | + > |
| 60 | + <span>{computed.xTicks.length}</span> |
| 61 | + <span className={UseChartModelWrapper.selectors.highlightedPoint}> |
| 62 | + {highlightedPoint ? `${highlightedPoint.x},${highlightedPoint.value}` : null} |
| 63 | + </span> |
| 64 | + <span className={UseChartModelWrapper.selectors.highlightedSeries}>{highlightedSeries?.title}</span> |
| 65 | + <span className={UseChartModelWrapper.selectors.highlightedX}> |
| 66 | + {highlightedX === null ? null : highlightedX[0].x} |
| 67 | + </span> |
| 68 | + <span>{visibleSeries[0].title}</span> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +function renderChartModelHook(props: UseChartModelProps<ChartDataTypes>) { |
| 74 | + const { rerender, container } = render(<RenderChartModelHook {...props} />); |
| 75 | + const wrapper = new UseChartModelWrapper(container.firstChild as HTMLElement); |
| 76 | + return { rerender, wrapper }; |
| 77 | +} |
| 78 | + |
| 79 | +describe('useChartModel', () => { |
| 80 | + const series: readonly AreaChartProps.Series<ChartDataTypes>[] = [ |
| 81 | + { |
| 82 | + type: 'area', |
| 83 | + title: 'series1', |
| 84 | + color: 'orange', |
| 85 | + data: [ |
| 86 | + { x: 0, y: 1 }, |
| 87 | + { x: 1, y: 3 }, |
| 88 | + { x: 2, y: 5 }, |
| 89 | + { x: 3, y: 7 }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + { |
| 93 | + type: 'area', |
| 94 | + title: 'series2', |
| 95 | + color: 'blue', |
| 96 | + data: [ |
| 97 | + { x: 0, y: 2 }, |
| 98 | + { x: 1, y: 4 }, |
| 99 | + { x: 2, y: 6 }, |
| 100 | + { x: 3, y: 8 }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + ]; |
| 104 | + |
| 105 | + describe('keyboard navigation', () => { |
| 106 | + it('cycles between the different series', () => { |
| 107 | + const { wrapper } = renderChartModelHook({ |
| 108 | + height: 0, |
| 109 | + highlightedSeries: null, |
| 110 | + setHighlightedSeries: (_series: AreaChartProps.Series<ChartDataTypes> | null) => _series, |
| 111 | + setVisibleSeries: (_series: readonly AreaChartProps.Series<ChartDataTypes>[]) => _series, |
| 112 | + width: 0, |
| 113 | + xDomain: undefined, |
| 114 | + xScaleType: 'linear', |
| 115 | + yScaleType: 'linear', |
| 116 | + externalSeries: series, |
| 117 | + visibleSeries: series, |
| 118 | + }); |
| 119 | + act(() => wrapper.focus()); |
| 120 | + |
| 121 | + // Show all series |
| 122 | + expect(wrapper.findHighlightedX()?.getElement()).toHaveTextContent('0'); |
| 123 | + expect(wrapper.findHighlightedPoint()?.getElement()).toBeEmptyDOMElement(); |
| 124 | + |
| 125 | + // Show first series (from the end) |
| 126 | + act(() => wrapper.keydown(KeyCode.down)); |
| 127 | + expect(wrapper.findHighlightedPoint()?.getElement()).toHaveTextContent('0,2'); |
| 128 | + |
| 129 | + // Show second series (from the end) |
| 130 | + act(() => wrapper.keydown(KeyCode.down)); |
| 131 | + expect(wrapper.findHighlightedPoint()?.getElement()).toHaveTextContent('0,1'); |
| 132 | + |
| 133 | + // Loop back to show all series |
| 134 | + act(() => wrapper.keydown(KeyCode.down)); |
| 135 | + expect(wrapper.findHighlightedX()?.getElement()).toHaveTextContent('0'); |
| 136 | + expect(wrapper.findHighlightedPoint()?.getElement()).toBeEmptyDOMElement(); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('navigation across X axis', () => { |
| 140 | + it('highlights next or previous point in the data series when a series is focused', () => { |
| 141 | + const { wrapper } = renderChartModelHook({ |
| 142 | + height: 0, |
| 143 | + highlightedSeries: null, |
| 144 | + setHighlightedSeries: (_series: AreaChartProps.Series<ChartDataTypes> | null) => _series, |
| 145 | + setVisibleSeries: (_series: readonly AreaChartProps.Series<ChartDataTypes>[]) => _series, |
| 146 | + width: 0, |
| 147 | + xDomain: undefined, |
| 148 | + xScaleType: 'linear', |
| 149 | + yScaleType: 'linear', |
| 150 | + externalSeries: series, |
| 151 | + visibleSeries: series, |
| 152 | + }); |
| 153 | + act(() => wrapper.focus()); |
| 154 | + |
| 155 | + act(() => wrapper.keydown(KeyCode.down)); |
| 156 | + expect(wrapper.findHighlightedPoint()?.getElement()).toHaveTextContent('0,2'); |
| 157 | + |
| 158 | + act(() => wrapper.keydown(KeyCode.right)); |
| 159 | + expect(wrapper.findHighlightedPoint()?.getElement()).toHaveTextContent('1,4'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('highlights next or previous X when no series is focused', () => { |
| 163 | + const { wrapper } = renderChartModelHook({ |
| 164 | + height: 0, |
| 165 | + highlightedSeries: null, |
| 166 | + setHighlightedSeries: (_series: AreaChartProps.Series<ChartDataTypes> | null) => _series, |
| 167 | + setVisibleSeries: (_series: readonly AreaChartProps.Series<ChartDataTypes>[]) => _series, |
| 168 | + width: 0, |
| 169 | + xDomain: undefined, |
| 170 | + xScaleType: 'linear', |
| 171 | + yScaleType: 'linear', |
| 172 | + externalSeries: series, |
| 173 | + visibleSeries: series, |
| 174 | + }); |
| 175 | + act(() => wrapper.focus()); |
| 176 | + expect(wrapper.findHighlightedX()?.getElement()).toHaveTextContent('0'); |
| 177 | + |
| 178 | + act(() => wrapper.keydown(KeyCode.right)); |
| 179 | + expect(wrapper.findHighlightedX()?.getElement()).toHaveTextContent('1'); |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments