|
| 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