-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(xychart): add BarStack tests (#866)
* tes(xychart/BarStack): add tests * test(xychart/combineBarStackData): add tests * test(xychart/findNearestStackDatum): add tests * internal(xychart): remove console logs in tests
- Loading branch information
1 parent
2a5a8dd
commit 9612b6f
Showing
6 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React, { useContext } from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { BarStack, BarSeries, DataProvider, DataContext } from '../../src'; | ||
|
||
const providerProps = { | ||
initialDimensions: { width: 100, height: 100 }, | ||
xScale: { type: 'linear' }, | ||
yScale: { type: 'linear' }, | ||
} as const; | ||
|
||
const accessors = { | ||
xAccessor: (d: { x: number }) => d.x, | ||
yAccessor: (d: { y: number }) => d.y, | ||
}; | ||
|
||
const series1 = { | ||
key: 'bar1', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: 5 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
const series2 = { | ||
key: 'bar2', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: 20 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
describe('<BarStack />', () => { | ||
it('should be defined', () => { | ||
expect(BarSeries).toBeDefined(); | ||
}); | ||
|
||
it('should render rects', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<BarStack horizontal> | ||
<BarSeries dataKey={series1.key} {...series1} /> | ||
<BarSeries dataKey={series2.key} {...series2} /> | ||
</BarStack> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
expect(wrapper.find('rect')).toHaveLength(4); | ||
}); | ||
|
||
it('should update scale domain to include stack sums including negative values', () => { | ||
expect.hasAssertions(); | ||
|
||
function Assertion() { | ||
const { yScale, dataRegistry } = useContext(DataContext); | ||
if (yScale && dataRegistry?.keys().length === 2) { | ||
expect(yScale.domain()).toEqual([-20, 10]); | ||
} | ||
return null; | ||
} | ||
|
||
mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<BarStack> | ||
<BarSeries dataKey={series1.key} {...series1} /> | ||
<BarSeries | ||
dataKey={series2.key} | ||
{...series2} | ||
data={[ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: -20 }, | ||
]} | ||
/> | ||
</BarStack> | ||
</svg> | ||
<Assertion /> | ||
</DataProvider>, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/visx-xychart/test/utils/combineBarStackData.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { BarSeries } from '../../src'; | ||
import combineBarStackData from '../../src/utils/combineBarStackData'; | ||
|
||
const accessors = { | ||
xAccessor: (d: { x: number }) => d.x, | ||
yAccessor: (d: { y: number }) => d.y, | ||
}; | ||
|
||
const series1 = { | ||
dataKey: 'bar1', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: -5 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
const series2 = { | ||
dataKey: 'bar2', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: 20 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
const seriesChildren = [ | ||
<BarSeries key={series1.dataKey} {...series1} />, | ||
<BarSeries key={series2.dataKey} {...series2} />, | ||
]; | ||
|
||
describe('combineBarStackData', () => { | ||
it('should be defined', () => { | ||
expect(combineBarStackData).toBeDefined(); | ||
}); | ||
|
||
it('should combine data by x stack value when horizontal=false', () => { | ||
expect(combineBarStackData(seriesChildren)).toEqual([ | ||
{ stack: 7, bar1: -5, bar2: 20, positiveSum: 20, negativeSum: -5 }, | ||
{ stack: 10, bar1: 5, bar2: 5, positiveSum: 10, negativeSum: 0 }, | ||
]); | ||
}); | ||
it('should combine data by y stack value when horizontal=true', () => { | ||
expect(combineBarStackData(seriesChildren, true)).toEqual([ | ||
{ stack: 5, bar1: 10, bar2: 10, positiveSum: 20, negativeSum: 0 }, | ||
{ stack: 20, bar1: undefined, bar2: 7, positiveSum: 7, negativeSum: 0 }, | ||
{ stack: -5, bar1: 7, bar2: undefined, positiveSum: 7, negativeSum: 0 }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters