|
| 1 | +import { mount } from 'enzyme' |
| 2 | +import { degreesToRadians } from '@nivo/core' |
| 3 | +// @ts-ignore |
| 4 | +import { RadialBar, RadialBarSvgProps, ComputedBar, RadialBarTooltipProps } from '../src' |
| 5 | + |
| 6 | +const baseProps: RadialBarSvgProps = { |
| 7 | + width: 600, |
| 8 | + height: 600, |
| 9 | + startAngle: 0, |
| 10 | + endAngle: 270, |
| 11 | + data: [ |
| 12 | + { id: 'Combini', data: [{ x: 'Fruits', y: 10 }] }, |
| 13 | + { id: 'Online', data: [{ x: 'Fruits', y: 20 }] }, |
| 14 | + { id: 'Supermarket', data: [{ x: 'Fruits', y: 30 }] }, |
| 15 | + ], |
| 16 | + animate: false, |
| 17 | +} |
| 18 | + |
| 19 | +const stackedData: RadialBarSvgProps['data'] = [ |
| 20 | + { |
| 21 | + id: 'Combini', |
| 22 | + data: [ |
| 23 | + { x: 'Fruits', y: 10 }, |
| 24 | + { x: 'Vegetables', y: 10 }, |
| 25 | + { x: 'Meat', y: 10 }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + { |
| 29 | + id: 'Online', |
| 30 | + data: [ |
| 31 | + { x: 'Fruits', y: 20 }, |
| 32 | + { x: 'Vegetables', y: 20 }, |
| 33 | + { x: 'Meat', y: 20 }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + { |
| 37 | + id: 'Supermarket', |
| 38 | + data: [ |
| 39 | + { x: 'Fruits', y: 30 }, |
| 40 | + { x: 'Vegetables', y: 30 }, |
| 41 | + { x: 'Meat', y: 30 }, |
| 42 | + ], |
| 43 | + }, |
| 44 | +] |
| 45 | + |
| 46 | +it('should render a basic radial bar chart', () => { |
| 47 | + const wrapper = mount(<RadialBar {...baseProps} />) |
| 48 | + |
| 49 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 50 | + expect(bars).toHaveLength(3) |
| 51 | + |
| 52 | + const bar0 = bars.at(0) |
| 53 | + const datum0 = bar0.prop<ComputedBar>('datum') |
| 54 | + expect(datum0.id).toBe('Combini.Fruits') |
| 55 | + expect(datum0.groupId).toBe('Combini') |
| 56 | + expect(datum0.category).toBe('Fruits') |
| 57 | + expect(datum0.value).toBe(10) |
| 58 | + expect(datum0.arc.startAngle).toBe(0) |
| 59 | + expect(datum0.arc.endAngle).toBe(degreesToRadians(90)) |
| 60 | + expect(datum0.color).toBe('#e8c1a0') |
| 61 | + |
| 62 | + const bar1 = bars.at(1) |
| 63 | + const datum1 = bar1.prop<ComputedBar>('datum') |
| 64 | + expect(datum1.id).toBe('Online.Fruits') |
| 65 | + expect(datum1.groupId).toBe('Online') |
| 66 | + expect(datum1.category).toBe('Fruits') |
| 67 | + expect(datum1.value).toBe(20) |
| 68 | + expect(datum1.arc.startAngle).toBe(0) |
| 69 | + expect(datum1.arc.endAngle).toBe(degreesToRadians(180)) |
| 70 | + expect(datum1.color).toBe('#e8c1a0') |
| 71 | + |
| 72 | + const bar2 = bars.at(2) |
| 73 | + const datum2 = bar2.prop<ComputedBar>('datum') |
| 74 | + expect(datum2.id).toBe('Supermarket.Fruits') |
| 75 | + expect(datum2.groupId).toBe('Supermarket') |
| 76 | + expect(datum2.category).toBe('Fruits') |
| 77 | + expect(datum2.value).toBe(30) |
| 78 | + expect(datum2.arc.startAngle).toBe(0) |
| 79 | + expect(datum2.arc.endAngle).toBe(degreesToRadians(270)) |
| 80 | + expect(datum1.color).toBe('#e8c1a0') |
| 81 | +}) |
| 82 | + |
| 83 | +describe('data', () => { |
| 84 | + it('should support value formatting via d3 format', () => { |
| 85 | + const wrapper = mount(<RadialBar {...baseProps} valueFormat=">-.2f" />) |
| 86 | + |
| 87 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 88 | + expect(bars).toHaveLength(3) |
| 89 | + |
| 90 | + expect(bars.at(0).prop<ComputedBar>('datum').formattedValue).toBe('10.00') |
| 91 | + expect(bars.at(1).prop<ComputedBar>('datum').formattedValue).toBe('20.00') |
| 92 | + expect(bars.at(2).prop<ComputedBar>('datum').formattedValue).toBe('30.00') |
| 93 | + }) |
| 94 | + |
| 95 | + it('should support value formatting via custom function', () => { |
| 96 | + const wrapper = mount(<RadialBar {...baseProps} valueFormat={value => `${value}%`} />) |
| 97 | + |
| 98 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 99 | + expect(bars).toHaveLength(3) |
| 100 | + |
| 101 | + expect(bars.at(0).prop<ComputedBar>('datum').formattedValue).toBe('10%') |
| 102 | + expect(bars.at(1).prop<ComputedBar>('datum').formattedValue).toBe('20%') |
| 103 | + expect(bars.at(2).prop<ComputedBar>('datum').formattedValue).toBe('30%') |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('tooltip', () => { |
| 108 | + it('should show a tooltip with category, groupId and formatted value', () => { |
| 109 | + const wrapper = mount(<RadialBar {...baseProps} valueFormat=">-.2f" />) |
| 110 | + |
| 111 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 112 | + expect(bars).toHaveLength(3) |
| 113 | + |
| 114 | + bars.at(0).find('path').simulate('mouseenter') |
| 115 | + let tooltip = wrapper.find('RadialBarTooltip') |
| 116 | + expect(tooltip.text()).toBe('Fruits - Combini: 10.00') |
| 117 | + |
| 118 | + bars.at(1).find('path').simulate('mouseenter') |
| 119 | + tooltip = wrapper.find('RadialBarTooltip') |
| 120 | + expect(tooltip.text()).toBe('Fruits - Online: 20.00') |
| 121 | + |
| 122 | + bars.at(2).find('path').simulate('mouseenter') |
| 123 | + tooltip = wrapper.find('RadialBarTooltip') |
| 124 | + expect(tooltip.text()).toBe('Fruits - Supermarket: 30.00') |
| 125 | + }) |
| 126 | + |
| 127 | + it('should support a custom tooltip', () => { |
| 128 | + const CustomTooltip = ({ bar }: RadialBarTooltipProps) => ( |
| 129 | + <div> |
| 130 | + {bar.groupId} -> {bar.category} -> {bar.formattedValue} |
| 131 | + </div> |
| 132 | + ) |
| 133 | + const wrapper = mount( |
| 134 | + <RadialBar {...baseProps} valueFormat={value => `${value}%`} tooltip={CustomTooltip} /> |
| 135 | + ) |
| 136 | + |
| 137 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 138 | + expect(bars).toHaveLength(3) |
| 139 | + |
| 140 | + bars.at(0).find('path').simulate('mouseenter') |
| 141 | + let tooltip = wrapper.find(CustomTooltip) |
| 142 | + expect(tooltip.text()).toBe('Combini -> Fruits -> 10%') |
| 143 | + |
| 144 | + bars.at(1).find('path').simulate('mouseenter') |
| 145 | + tooltip = wrapper.find(CustomTooltip) |
| 146 | + expect(tooltip.text()).toBe('Online -> Fruits -> 20%') |
| 147 | + |
| 148 | + bars.at(2).find('path').simulate('mouseenter') |
| 149 | + tooltip = wrapper.find(CustomTooltip) |
| 150 | + expect(tooltip.text()).toBe('Supermarket -> Fruits -> 30%') |
| 151 | + }) |
| 152 | +}) |
| 153 | + |
| 154 | +describe('style', () => { |
| 155 | + it('custom colors array', () => { |
| 156 | + const colors = ['#ff0000', '#00ff00', '#0000ff'] |
| 157 | + const wrapper = mount(<RadialBar {...baseProps} data={stackedData} colors={colors} />) |
| 158 | + |
| 159 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 160 | + expect(bars).toHaveLength(9) |
| 161 | + |
| 162 | + expect(bars.at(0).prop<ComputedBar>('datum').color).toBe('#ff0000') |
| 163 | + expect(bars.at(1).prop<ComputedBar>('datum').color).toBe('#00ff00') |
| 164 | + expect(bars.at(2).prop<ComputedBar>('datum').color).toBe('#0000ff') |
| 165 | + |
| 166 | + expect(bars.at(3).prop<ComputedBar>('datum').color).toBe('#ff0000') |
| 167 | + expect(bars.at(4).prop<ComputedBar>('datum').color).toBe('#00ff00') |
| 168 | + expect(bars.at(5).prop<ComputedBar>('datum').color).toBe('#0000ff') |
| 169 | + |
| 170 | + expect(bars.at(6).prop<ComputedBar>('datum').color).toBe('#ff0000') |
| 171 | + expect(bars.at(7).prop<ComputedBar>('datum').color).toBe('#00ff00') |
| 172 | + expect(bars.at(8).prop<ComputedBar>('datum').color).toBe('#0000ff') |
| 173 | + }) |
| 174 | + |
| 175 | + it('custom colors function using groupId', () => { |
| 176 | + const colorMapping = { |
| 177 | + Combini: '#ff0000', |
| 178 | + Online: '#00ff00', |
| 179 | + Supermarket: '#0000ff', |
| 180 | + } |
| 181 | + const wrapper = mount( |
| 182 | + <RadialBar |
| 183 | + {...baseProps} |
| 184 | + colors={d => colorMapping[d.groupId as keyof typeof colorMapping]} |
| 185 | + /> |
| 186 | + ) |
| 187 | + |
| 188 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 189 | + expect(bars).toHaveLength(3) |
| 190 | + |
| 191 | + expect(bars.at(0).prop<ComputedBar>('datum').color).toBe('#ff0000') |
| 192 | + expect(bars.at(1).prop<ComputedBar>('datum').color).toBe('#00ff00') |
| 193 | + expect(bars.at(2).prop<ComputedBar>('datum').color).toBe('#0000ff') |
| 194 | + }) |
| 195 | + |
| 196 | + it('custom colors from data', () => { |
| 197 | + const wrapper = mount( |
| 198 | + <RadialBar |
| 199 | + {...baseProps} |
| 200 | + data={[ |
| 201 | + { id: 'Combini', data: [{ x: 'Fruits', y: 10, color: '#ff0000' }] }, |
| 202 | + { id: 'Online', data: [{ x: 'Fruits', y: 20, color: '#00ff00' }] }, |
| 203 | + { id: 'Supermarket', data: [{ x: 'Fruits', y: 30, color: '#0000ff' }] }, |
| 204 | + ]} |
| 205 | + colors={{ datum: 'data.color' }} |
| 206 | + /> |
| 207 | + ) |
| 208 | + |
| 209 | + const bars = wrapper.find('RadialBarArcs').find('ArcShape') |
| 210 | + expect(bars).toHaveLength(3) |
| 211 | + |
| 212 | + expect(bars.at(0).prop<ComputedBar>('datum').color).toBe('#ff0000') |
| 213 | + expect(bars.at(1).prop<ComputedBar>('datum').color).toBe('#00ff00') |
| 214 | + expect(bars.at(2).prop<ComputedBar>('datum').color).toBe('#0000ff') |
| 215 | + }) |
| 216 | +}) |
| 217 | + |
| 218 | +describe('accessibility', () => { |
| 219 | + it('should forward root aria properties to the SVG element', () => { |
| 220 | + const wrapper = mount( |
| 221 | + <RadialBar |
| 222 | + {...baseProps} |
| 223 | + ariaLabel="AriaLabel" |
| 224 | + ariaLabelledBy="AriaLabelledBy" |
| 225 | + ariaDescribedBy="AriaDescribedBy" |
| 226 | + /> |
| 227 | + ) |
| 228 | + |
| 229 | + const svg = wrapper.find('svg') |
| 230 | + |
| 231 | + expect(svg.prop('aria-label')).toBe('AriaLabel') |
| 232 | + expect(svg.prop('aria-labelledby')).toBe('AriaLabelledBy') |
| 233 | + expect(svg.prop('aria-describedby')).toBe('AriaDescribedBy') |
| 234 | + }) |
| 235 | +}) |
0 commit comments