-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(xychart): add AreaStack tests #1036
Merged
williaster
merged 6 commits into
chris--xychart-stacked-areas
from
chris--xychart-areastack-tests
Jan 26, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d13229
fix(xychart/BaseAxis): handle undefined scale
williaster 2a0fc7a
fix(xychart/useDimensions): fix partial initialDimensions
williaster d5722d7
new(xychart/useScales): avoid NaN domain when no values are present
williaster cfa668c
fix(xychart/DataProvider): no negative ranges or dimensions
williaster 5bed320
fix(xychart/getScaleBandwidth): handle undefined scale case
williaster d7cfcaa
test(xychart): add AreaStack, useStackedData tests
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { AxisScale } from '@visx/axis'; | ||
|
||
export default function getScaleBandwidth<Scale extends AxisScale>(scale: Scale) { | ||
export default function getScaleBandwidth<Scale extends AxisScale>(scale?: Scale) { | ||
// Broaden type before using 'xxx' in s as typeguard. | ||
const s = scale as AxisScale; | ||
return 'bandwidth' in s ? s?.bandwidth() ?? 0 : 0; | ||
return s && 'bandwidth' in s ? s?.bandwidth() ?? 0 : 0; | ||
} |
180 changes: 180 additions & 0 deletions
180
packages/visx-xychart/test/components/AreaStack.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,180 @@ | ||
import React, { useContext, useEffect } from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { animated } from 'react-spring'; | ||
import { Area, LinePath } from '@visx/shape'; | ||
import { | ||
AreaStack, | ||
AreaSeries, | ||
DataProvider, | ||
DataContext, | ||
useEventEmitter, | ||
AnimatedAreaStack, | ||
} from '../../src'; | ||
import setupTooltipTest from '../mocks/setupTooltipTest'; | ||
import { XYCHART_EVENT_SOURCE } from '../../src/constants'; | ||
|
||
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: 'area1', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: 5 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
const series2 = { | ||
key: 'area2', | ||
data: [ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: 20 }, | ||
], | ||
...accessors, | ||
}; | ||
|
||
describe('<AreaStack />', () => { | ||
it('should be defined', () => { | ||
expect(AreaSeries).toBeDefined(); | ||
}); | ||
|
||
it('should render Areas', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<AreaStack> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
<AreaSeries dataKey={series2.key} {...series2} /> | ||
</AreaStack> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
// @ts-ignore produces a union type that is too complex to represent.ts(2590) | ||
expect(wrapper.find(Area)).toHaveLength(2); | ||
}); | ||
|
||
it('should render LinePaths if renderLine=true', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<AreaStack renderLine> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
<AreaSeries dataKey={series2.key} {...series2} /> | ||
</AreaStack> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
// @ts-ignore produces a union type that is too complex to represent.ts(2590) | ||
expect(wrapper.find(LinePath)).toHaveLength(2); | ||
}); | ||
|
||
it('should render Glyphs if focus/blur handlers are set', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<AreaStack onFocus={() => {}}> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
</AreaStack> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
expect(wrapper.find('circle')).toHaveLength(series1.data.length); | ||
}); | ||
|
||
it('should update scale domain to include stack sums including negative values', () => { | ||
expect.hasAssertions(); | ||
|
||
function Assertion() { | ||
const { yScale } = useContext(DataContext); | ||
if (yScale) { | ||
expect(yScale.domain()).toEqual([-20, 10]); | ||
} | ||
return null; | ||
} | ||
|
||
mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<AreaStack> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
<AreaSeries | ||
dataKey={series2.key} | ||
{...series2} | ||
data={[ | ||
{ x: 10, y: 5 }, | ||
{ x: 7, y: -20 }, | ||
]} | ||
/> | ||
</AreaStack> | ||
</svg> | ||
<Assertion /> | ||
</DataProvider>, | ||
); | ||
}); | ||
|
||
it('should invoke showTooltip/hideTooltip on pointermove/pointerout', () => { | ||
expect.assertions(2); | ||
|
||
const showTooltip = jest.fn(); | ||
const hideTooltip = jest.fn(); | ||
|
||
const EventEmitter = () => { | ||
const emit = useEventEmitter(); | ||
const { yScale } = useContext(DataContext); | ||
|
||
useEffect(() => { | ||
// checking for yScale ensures stack data is registered and stacks are rendered | ||
if (emit && yScale) { | ||
// @ts-ignore not a React.MouseEvent | ||
emit('pointermove', new MouseEvent('pointermove'), XYCHART_EVENT_SOURCE); | ||
expect(showTooltip).toHaveBeenCalledTimes(2); // one per key | ||
|
||
// @ts-ignore not a React.MouseEvent | ||
emit('pointerout', new MouseEvent('pointerout'), XYCHART_EVENT_SOURCE); | ||
expect(showTooltip).toHaveBeenCalled(); | ||
} | ||
}); | ||
|
||
return null; | ||
}; | ||
|
||
setupTooltipTest( | ||
<> | ||
<AreaStack> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
<AreaSeries dataKey={series2.key} {...series2} /> | ||
</AreaStack> | ||
<EventEmitter /> | ||
</>, | ||
{ showTooltip, hideTooltip }, | ||
); | ||
}); | ||
}); | ||
|
||
describe('<AnimatedAreaStack />', () => { | ||
it('should be defined', () => { | ||
expect(AnimatedAreaStack).toBeDefined(); | ||
}); | ||
it('should render an animated.path', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<AnimatedAreaStack renderLine={false}> | ||
<AreaSeries dataKey={series1.key} {...series1} /> | ||
<AreaSeries dataKey={series2.key} {...series2} /> | ||
</AnimatedAreaStack> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
expect(wrapper.find(animated.path)).toHaveLength(2); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this look cleaner?
const scale: Scale | undefined = ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely ideal, but the type won't work without a coercion either here, or when I get the scales from
DataContext
(I haven't successfully figured out how to use generics withContext
types; most Series coerce theContext
values right now)