Skip to content

Commit 3f5ed41

Browse files
committedDec 29, 2021
feat(bump): add tests for the AreaBump chart
1 parent 6bb16dc commit 3f5ed41

File tree

9 files changed

+210
-16
lines changed

9 files changed

+210
-16
lines changed
 

‎packages/bump/src/area-bump/Area.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const Area = <Datum extends AreaBumpDatum, ExtraProps extends AreaBumpSer
6464

6565
return (
6666
<animated.path
67+
data-testid={`area.${serie.id}`}
6768
d={animatedPath}
6869
fill={serie.fill ? serie.fill : animatedProps.color}
6970
fillOpacity={animatedProps.fillOpacity}

‎packages/bump/src/area-bump/AreaBump.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const InnerAreaBump = <Datum extends AreaBumpDatum, ExtraProps extends AreaBumpS
182182
<Fragment key="labels">
183183
{startLabel !== false && (
184184
<AreasLabels<Datum, ExtraProps>
185-
label={startLabel}
185+
getLabel={startLabel}
186186
series={series}
187187
position="start"
188188
padding={startLabelPadding}
@@ -191,7 +191,7 @@ const InnerAreaBump = <Datum extends AreaBumpDatum, ExtraProps extends AreaBumpS
191191
)}
192192
{endLabel !== false && (
193193
<AreasLabels<Datum, ExtraProps>
194-
label={endLabel}
194+
getLabel={endLabel}
195195
series={series}
196196
position="end"
197197
padding={endLabelPadding}

‎packages/bump/src/area-bump/AreasLabels.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { useAreaBumpSeriesLabels } from './hooks'
1111

1212
interface AreaLabelsProps<Datum extends AreaBumpDatum, ExtraProps extends AreaBumpSerieExtraProps> {
13-
label: Exclude<AreaBumpLabel<Datum, ExtraProps>, false>
13+
getLabel: Exclude<AreaBumpLabel<Datum, ExtraProps>, false>
1414
series: AreaBumpComputedSerie<Datum, ExtraProps>[]
1515
position: 'start' | 'end'
1616
padding: number
@@ -21,7 +21,7 @@ export const AreasLabels = <
2121
Datum extends AreaBumpDatum,
2222
ExtraProps extends AreaBumpSerieExtraProps
2323
>({
24-
label,
24+
getLabel,
2525
series,
2626
position,
2727
padding,
@@ -31,7 +31,7 @@ export const AreasLabels = <
3131
const { animate, config: springConfig } = useMotionConfig()
3232

3333
const labels = useAreaBumpSeriesLabels<Datum, ExtraProps>({
34-
label,
34+
getLabel,
3535
series,
3636
position,
3737
padding,
@@ -60,6 +60,7 @@ export const AreasLabels = <
6060

6161
return (
6262
<animated.text
63+
data-testid={`label.${position}.${label.serie.id}`}
6364
key={label.id}
6465
x={animatedProps.x}
6566
y={animatedProps.y}

‎packages/bump/src/area-bump/defaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const commonDefaultProps: Omit<
3636
startLabel: false,
3737
startLabelPadding: 12,
3838
startLabelTextColor: { from: 'color', modifiers: [['darker', 1]] },
39-
endLabel: 'id',
39+
endLabel: true,
4040
endLabelPadding: 12,
4141
endLabelTextColor: { from: 'color', modifiers: [['darker', 1]] },
4242

‎packages/bump/src/area-bump/hooks.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement, useMemo, useCallback, useState } from 'react'
22
import { area as d3Area, curveBasis, curveLinear } from 'd3-shape'
3-
import { useTheme, usePropertyAccessor } from '@nivo/core'
3+
import { useTheme } from '@nivo/core'
44
import { useOrdinalColorScale, useInheritedColor, InheritedColorConfig } from '@nivo/colors'
55
import { useTooltip } from '@nivo/tooltip'
66
import { computeSeries } from './compute'
@@ -321,23 +321,21 @@ export const useAreaBumpSeriesLabels = <
321321
Datum extends AreaBumpDatum,
322322
ExtraProps extends AreaBumpSerieExtraProps
323323
>({
324-
label,
325324
series,
326325
position,
327326
padding,
328327
color,
328+
getLabel,
329329
}: {
330-
label: Exclude<AreaBumpLabel<Datum, ExtraProps>, false>
331330
series: AreaBumpComputedSerie<Datum, ExtraProps>[]
332331
position: 'start' | 'end'
333332
padding: number
334333
color: InheritedColorConfig<AreaBumpComputedSerie<Datum, ExtraProps>>
334+
getLabel: Exclude<AreaBumpLabel<Datum, ExtraProps>, false>
335335
}): AreaBumpLabelData<Datum, ExtraProps>[] => {
336336
const theme = useTheme()
337337
const getColor = useInheritedColor(color, theme)
338338

339-
const getLabel = usePropertyAccessor(label)
340-
341339
return useMemo(() => {
342340
let textAnchor: 'start' | 'end'
343341
let signedPadding: number
@@ -350,12 +348,17 @@ export const useAreaBumpSeriesLabels = <
350348
}
351349

352350
return series.map(serie => {
351+
let label = serie.id
352+
if (typeof getLabel === 'function') {
353+
label = getLabel(serie.data)
354+
}
355+
353356
const point =
354357
position === 'start' ? serie.points[0] : serie.points[serie.points.length - 1]
355358

356359
return {
357360
id: serie.id,
358-
label: getLabel(serie.data),
361+
label,
359362
x: point.x + signedPadding,
360363
y: point.y,
361364
color: getColor(serie),

‎packages/bump/src/area-bump/types.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FunctionComponent, MouseEvent } from 'react'
22
import { Area } from 'd3-shape'
33
import {
4-
PropertyAccessor,
54
Box,
65
Theme,
76
Dimensions,
@@ -78,7 +77,7 @@ export type AreaBumpInterpolation = 'smooth' | 'linear'
7877
export type AreaBumpLabel<
7978
Datum extends AreaBumpDatum,
8079
ExtraProps extends AreaBumpSerieExtraProps
81-
> = PropertyAccessor<AreaBumpSerie<Datum, ExtraProps>, string> | false
80+
> = ((serie: AreaBumpSerie<Datum, ExtraProps>) => string) | boolean
8281
export interface AreaBumpLabelData<
8382
Datum extends AreaBumpDatum,
8483
ExtraProps extends AreaBumpSerieExtraProps

‎packages/bump/src/bump/LinesLabels.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useBumpSeriesLabels } from './hooks'
66

77
interface LineLabelsProps<Datum extends BumpDatum, ExtraProps extends BumpSerieExtraProps> {
88
series: BumpComputedSerie<Datum, ExtraProps>[]
9-
getLabel: BumpLabel<Datum, ExtraProps>
9+
getLabel: Exclude<BumpLabel<Datum, ExtraProps>, false>
1010
position: 'start' | 'end'
1111
padding: number
1212
color: InheritedColorConfig<BumpComputedSerie<Datum, ExtraProps>>

‎packages/bump/src/bump/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export const useBumpSeriesLabels = <
384384
position: 'start' | 'end'
385385
padding: number
386386
color: InheritedColorConfig<BumpComputedSerie<Datum, ExtraProps>>
387-
getLabel: BumpLabel<Datum, ExtraProps>
387+
getLabel: Exclude<BumpLabel<Datum, ExtraProps>, false>
388388
}) => {
389389
const theme = useTheme()
390390
const getColor = useInheritedColor(color, theme)

‎packages/bump/tests/AreaBump.test.tsx

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { mount } from 'enzyme'
2+
// @ts-ignore
3+
import { AreaBump, AreaBumpSvgProps } from '../src'
4+
5+
interface Datum {
6+
x: number
7+
y: number
8+
}
9+
10+
const sampleData: AreaBumpSvgProps<Datum, Record<string, unknown>>['data'] = [
11+
{
12+
id: 'A',
13+
data: [
14+
{
15+
x: 2000,
16+
y: 9,
17+
},
18+
{
19+
x: 2001,
20+
y: 9,
21+
},
22+
{
23+
x: 2002,
24+
y: 2,
25+
},
26+
{
27+
x: 2003,
28+
y: 4,
29+
},
30+
],
31+
},
32+
{
33+
id: 'B',
34+
data: [
35+
{
36+
x: 2000,
37+
y: 8,
38+
},
39+
{
40+
x: 2001,
41+
y: 3,
42+
},
43+
{
44+
x: 2002,
45+
y: 1,
46+
},
47+
{
48+
x: 2003,
49+
y: 7,
50+
},
51+
],
52+
},
53+
{
54+
id: 'C',
55+
data: [
56+
{
57+
x: 2000,
58+
y: 12,
59+
},
60+
{
61+
x: 2001,
62+
y: 4,
63+
},
64+
{
65+
x: 2002,
66+
y: 5,
67+
},
68+
{
69+
x: 2003,
70+
y: 6,
71+
},
72+
],
73+
},
74+
]
75+
76+
const baseProps: AreaBumpSvgProps<Datum, Record<string, unknown>> = {
77+
width: 800,
78+
height: 600,
79+
data: sampleData,
80+
animate: false,
81+
}
82+
83+
it('should render a basic area bump chart', () => {
84+
const wrapper = mount(<AreaBump<Datum> {...baseProps} />)
85+
86+
const areaA = wrapper.find(`path[data-testid='area.A']`)
87+
expect(areaA.exists()).toBeTruthy()
88+
89+
const areaB = wrapper.find(`path[data-testid='area.B']`)
90+
expect(areaB.exists()).toBeTruthy()
91+
92+
const areaC = wrapper.find(`path[data-testid='area.C']`)
93+
expect(areaC.exists()).toBeTruthy()
94+
})
95+
96+
describe('style', () => {
97+
it('custom colors array', () => {
98+
const colors = ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)', 'rgba(0, 0, 255, 1)']
99+
const wrapper = mount(<AreaBump<Datum> {...baseProps} colors={colors} />)
100+
101+
expect(wrapper.find(`path[data-testid='area.A']`).prop('fill')).toEqual(colors[0])
102+
expect(wrapper.find(`path[data-testid='area.B']`).prop('fill')).toEqual(colors[1])
103+
expect(wrapper.find(`path[data-testid='area.C']`).prop('fill')).toEqual(colors[2])
104+
})
105+
106+
it('colors from data', () => {
107+
const colors = ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)', 'rgba(0, 0, 255, 1)']
108+
const wrapper = mount(
109+
<AreaBump<Datum, { color: string }>
110+
{...baseProps}
111+
data={sampleData.map((serie, i) => ({
112+
...serie,
113+
color: colors[i],
114+
}))}
115+
colors={serie => serie.color}
116+
/>
117+
)
118+
119+
expect(wrapper.find(`path[data-testid='area.A']`).prop('fill')).toEqual(colors[0])
120+
expect(wrapper.find(`path[data-testid='area.B']`).prop('fill')).toEqual(colors[1])
121+
expect(wrapper.find(`path[data-testid='area.C']`).prop('fill')).toEqual(colors[2])
122+
})
123+
})
124+
125+
describe('labels', () => {
126+
it('default end labels', () => {
127+
const wrapper = mount(<AreaBump<Datum> {...baseProps} />)
128+
129+
const endLabelA = wrapper.find(`text[data-testid='label.end.A']`)
130+
expect(endLabelA.exists()).toBeTruthy()
131+
expect(endLabelA.text()).toEqual('A')
132+
expect(endLabelA.prop('textAnchor')).toEqual('start')
133+
134+
const endLabelB = wrapper.find(`text[data-testid='label.end.B']`)
135+
expect(endLabelB.exists()).toBeTruthy()
136+
expect(endLabelB.text()).toEqual('B')
137+
expect(endLabelB.prop('textAnchor')).toEqual('start')
138+
139+
const endLabelC = wrapper.find(`text[data-testid='label.end.C']`)
140+
expect(endLabelC.exists()).toBeTruthy()
141+
expect(endLabelC.text()).toEqual('C')
142+
expect(endLabelC.prop('textAnchor')).toEqual('start')
143+
})
144+
145+
it('customize end labels', () => {
146+
const wrapper = mount(
147+
<AreaBump<Datum> {...baseProps} endLabel={serie => `Serie ${serie.id}`} />
148+
)
149+
150+
expect(wrapper.find(`text[data-testid='label.end.A']`).text()).toEqual('Serie A')
151+
expect(wrapper.find(`text[data-testid='label.end.B']`).text()).toEqual('Serie B')
152+
expect(wrapper.find(`text[data-testid='label.end.C']`).text()).toEqual('Serie C')
153+
})
154+
155+
it('label from data', () => {
156+
const wrapper = mount(
157+
<AreaBump<Datum, { label: string }>
158+
{...baseProps}
159+
data={sampleData.map(serie => ({
160+
...serie,
161+
label: `Serie ${serie.id} label`,
162+
}))}
163+
endLabel={serie => serie.label}
164+
/>
165+
)
166+
167+
expect(wrapper.find(`text[data-testid='label.end.A']`).text()).toEqual('Serie A label')
168+
expect(wrapper.find(`text[data-testid='label.end.B']`).text()).toEqual('Serie B label')
169+
expect(wrapper.find(`text[data-testid='label.end.C']`).text()).toEqual('Serie C label')
170+
})
171+
172+
it('start labels', () => {
173+
const wrapper = mount(<AreaBump<Datum> {...baseProps} startLabel />)
174+
175+
const startLabelA = wrapper.find(`text[data-testid='label.start.A']`)
176+
expect(startLabelA.exists()).toBeTruthy()
177+
expect(startLabelA.text()).toEqual('A')
178+
expect(startLabelA.prop('textAnchor')).toEqual('end')
179+
180+
const startLabelB = wrapper.find(`text[data-testid='label.start.B']`)
181+
expect(startLabelB.exists()).toBeTruthy()
182+
expect(startLabelB.text()).toEqual('B')
183+
expect(startLabelB.prop('textAnchor')).toEqual('end')
184+
185+
const startLabelC = wrapper.find(`text[data-testid='label.start.C']`)
186+
expect(startLabelC.exists()).toBeTruthy()
187+
expect(startLabelC.text()).toEqual('C')
188+
expect(startLabelC.prop('textAnchor')).toEqual('end')
189+
})
190+
})

0 commit comments

Comments
 (0)
Please sign in to comment.