Skip to content

Commit e259c04

Browse files
committedSep 7, 2021
feat(radar): add tests for value formatting
1 parent bb81efb commit e259c04

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed
 

‎packages/radar/src/Radar.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReactNode, Fragment } from 'react'
22
import { Container, useDimensions, SvgWrapper } from '@nivo/core'
33
import { BoxLegendSvg } from '@nivo/legends'
4-
import { RadarShapes } from './RadarShapes'
4+
import { RadarLayer } from './RadarLayer'
55
import { RadarGrid } from './RadarGrid'
66
import { RadarSlices } from './RadarSlices'
77
import { RadarDots } from './RadarDots'
@@ -83,7 +83,7 @@ const InnerRadar = <D extends Record<string, unknown>>({
8383

8484
const layerById: Record<RadarLayerId, ReactNode> = {
8585
grid: null,
86-
shapes: null,
86+
layers: null,
8787
slices: null,
8888
dots: null,
8989
legends: null,
@@ -105,11 +105,11 @@ const InnerRadar = <D extends Record<string, unknown>>({
105105
)
106106
}
107107

108-
if (layers.includes('shapes')) {
109-
layerById.shapes = (
110-
<g key="shapes" transform={`translate(${centerX}, ${centerY})`}>
108+
if (layers.includes('layers')) {
109+
layerById.layers = (
110+
<g key="layers" transform={`translate(${centerX}, ${centerY})`}>
111111
{keys.map(key => (
112-
<RadarShapes<D>
112+
<RadarLayer<D>
113113
key={key}
114114
data={data}
115115
item={key}
@@ -195,7 +195,7 @@ const InnerRadar = <D extends Record<string, unknown>>({
195195
ariaDescribedBy={ariaDescribedBy}
196196
>
197197
{layerById.grid}
198-
{layerById.shapes}
198+
{layerById.layers}
199199
{layerById.slices}
200200
{layerById.dots}
201201
{layerById.legends}

‎packages/radar/src/RadarShapes.tsx ‎packages/radar/src/RadarLayer.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useMotionConfig, useTheme, useAnimatedPath } from '@nivo/core'
66
import { useInheritedColor } from '@nivo/colors'
77
import { RadarCommonProps } from './types'
88

9-
interface RadarShapesProps<D extends Record<string, unknown>> {
9+
interface RadarLayerProps<D extends Record<string, unknown>> {
1010
data: D[]
1111
item: string
1212
colorByKey: Record<string | number, string>
@@ -19,7 +19,7 @@ interface RadarShapesProps<D extends Record<string, unknown>> {
1919
blendMode: RadarCommonProps['blendMode']
2020
}
2121

22-
export const RadarShapes = <D extends Record<string, unknown>>({
22+
export const RadarLayer = <D extends Record<string, unknown>>({
2323
data,
2424
item: key,
2525
colorByKey,
@@ -30,7 +30,7 @@ export const RadarShapes = <D extends Record<string, unknown>>({
3030
borderColor,
3131
fillOpacity,
3232
blendMode,
33-
}: RadarShapesProps<D>) => {
33+
}: RadarLayerProps<D>) => {
3434
const theme = useTheme()
3535
const getBorderColor = useInheritedColor(borderColor, theme)
3636

‎packages/radar/src/props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RadarSliceTooltip } from './RadarSliceTooltip'
33
import { RadarLayerId } from './types'
44

55
export const svgDefaultProps = {
6-
layers: ['grid', 'shapes', 'slices', 'dots', 'legends'] as RadarLayerId[],
6+
layers: ['grid', 'layers', 'slices', 'dots', 'legends'] as RadarLayerId[],
77

88
maxValue: 'auto' as const,
99

‎packages/radar/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface RadarSliceTooltipProps {
6565
}
6666
export type RadarSliceTooltipComponent = FunctionComponent<RadarSliceTooltipProps>
6767

68-
export type RadarLayerId = 'grid' | 'shapes' | 'slices' | 'dots' | 'legends'
68+
export type RadarLayerId = 'grid' | 'layers' | 'slices' | 'dots' | 'legends'
6969

7070
export type RadarColorMapping = Record<string, string>
7171

‎packages/radar/tests/Radar.test.tsx

+44-16
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,46 @@ const baseProps: RadarSvgProps<TestDatum> = {
2525
it('should render a basic radar chart', () => {
2626
const wrapper = mount(<Radar<TestDatum> {...baseProps} />)
2727

28-
const shapes = wrapper.find('RadarShapes')
29-
expect(shapes).toHaveLength(2)
30-
31-
const shape0 = shapes.at(0)
32-
expect(shape0.prop('item')).toBe('A')
33-
const shape0path = shape0.find('path')
34-
expect(shape0path.prop('fill')).toBe('rgba(232, 193, 160, 1)')
35-
36-
const shape1 = shapes.at(1)
37-
expect(shape1.prop('item')).toBe('B')
38-
const shape1path = shape1.find('path')
39-
expect(shape1path.prop('fill')).toBe('rgba(244, 117, 96, 1)')
28+
const layers = wrapper.find('RadarLayer')
29+
expect(layers).toHaveLength(2)
30+
31+
const layer0 = layers.at(0)
32+
expect(layer0.prop('item')).toBe('A')
33+
const layer0path = layer0.find('path')
34+
expect(layer0path.prop('fill')).toBe('rgba(232, 193, 160, 1)')
35+
36+
const layer1 = layers.at(1)
37+
expect(layer1.prop('item')).toBe('B')
38+
const layer1path = layer1.find('path')
39+
expect(layer1path.prop('fill')).toBe('rgba(244, 117, 96, 1)')
40+
})
41+
42+
describe('data', () => {
43+
it('should support value formatting', () => {
44+
const wrapper = mount(
45+
<Radar<TestDatum> {...baseProps} valueFormat={value => `${value}%`} />
46+
)
47+
48+
wrapper.find('RadarSlice').at(0).find('path').simulate('mouseenter')
49+
50+
const tooltip = wrapper.find('RadarSliceTooltip')
51+
const data = tooltip.prop<RadarSliceTooltipProps['data']>('data')
52+
expect(data[0].formattedValue).toEqual('20%')
53+
expect(data[1].formattedValue).toEqual('10%')
54+
})
55+
56+
it('should support value formatting depending on the key', () => {
57+
const wrapper = mount(
58+
<Radar<TestDatum> {...baseProps} valueFormat={(value, key) => `${value} ${key}s`} />
59+
)
60+
61+
wrapper.find('RadarSlice').at(2).find('path').simulate('mouseenter')
62+
63+
const tooltip = wrapper.find('RadarSliceTooltip')
64+
const data = tooltip.prop<RadarSliceTooltipProps['data']>('data')
65+
expect(data[0].formattedValue).toEqual('30 As')
66+
expect(data[1].formattedValue).toEqual('10 Bs')
67+
})
4068
})
4169

4270
describe('tooltip', () => {
@@ -90,8 +118,8 @@ describe('style', () => {
90118
const colors = ['rgba(255, 0, 0, 1)', 'rgba(0, 0, 255, 1)']
91119
const wrapper = mount(<Radar {...baseProps} colors={colors} />)
92120

93-
expect(wrapper.find('RadarShapes').at(0).find('path').prop('fill')).toBe(colors[0])
94-
expect(wrapper.find('RadarShapes').at(1).find('path').prop('fill')).toBe(colors[1])
121+
expect(wrapper.find('RadarLayer').at(0).find('path').prop('fill')).toBe(colors[0])
122+
expect(wrapper.find('RadarLayer').at(1).find('path').prop('fill')).toBe(colors[1])
95123
})
96124

97125
it('custom colors function', () => {
@@ -106,8 +134,8 @@ describe('style', () => {
106134
/>
107135
)
108136

109-
expect(wrapper.find('RadarShapes').at(0).find('path').prop('fill')).toBe(colorMapping.A)
110-
expect(wrapper.find('RadarShapes').at(1).find('path').prop('fill')).toBe(colorMapping.B)
137+
expect(wrapper.find('RadarLayer').at(0).find('path').prop('fill')).toBe(colorMapping.A)
138+
expect(wrapper.find('RadarLayer').at(1).find('path').prop('fill')).toBe(colorMapping.B)
111139
})
112140
})
113141

0 commit comments

Comments
 (0)
Please sign in to comment.