-
Notifications
You must be signed in to change notification settings - Fork 719
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 BarGroup + mouseover tests (#871)
* test(xychart/mocks): add @visx/event mock * test(xychart): add BarGroup tests * test(xychart): add mousemove/out tests to all series * test(xychart): remove .test. from setupTooltipTest filename * lint(xychart/setupTooltipTest)
- Loading branch information
1 parent
d6d09c0
commit fd522db
Showing
8 changed files
with
265 additions
and
10 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,8 @@ | ||
export function localPoint() { | ||
return { | ||
x: 5, | ||
y: 3, | ||
value: () => ({ x: 5, y: 3 }), | ||
toArray: () => [5, 3], | ||
}; | ||
} |
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,89 @@ | ||
import React, { useEffect } from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { BarGroup, BarSeries, DataProvider, useEventEmitter } from '../../src'; | ||
import setupTooltipTest from '../mocks/setupTooltipTest'; | ||
|
||
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('<BarGroup />', () => { | ||
it('should be defined', () => { | ||
expect(BarSeries).toBeDefined(); | ||
}); | ||
|
||
it('should render rects', () => { | ||
const wrapper = mount( | ||
<DataProvider {...providerProps}> | ||
<svg> | ||
<BarGroup horizontal> | ||
<BarSeries dataKey={series1.key} {...series1} /> | ||
<BarSeries dataKey={series2.key} {...series2} /> | ||
</BarGroup> | ||
</svg> | ||
</DataProvider>, | ||
); | ||
expect(wrapper.find('rect')).toHaveLength(4); | ||
}); | ||
|
||
it('should invoke showTooltip/hideTooltip on mousemove/mouseout', () => { | ||
expect.assertions(2); | ||
|
||
const showTooltip = jest.fn(); | ||
const hideTooltip = jest.fn(); | ||
|
||
const EventEmitter = () => { | ||
const emit = useEventEmitter(); | ||
|
||
useEffect(() => { | ||
if (emit) { | ||
// @ts-ignore not a React.MouseEvent | ||
emit('mousemove', new MouseEvent('mousemove')); | ||
expect(showTooltip).toHaveBeenCalledTimes(2); // one per key | ||
|
||
// @ts-ignore not a React.MouseEvent | ||
emit('mouseout', new MouseEvent('mouseout')); | ||
expect(showTooltip).toHaveBeenCalled(); | ||
} | ||
}); | ||
|
||
return null; | ||
}; | ||
|
||
setupTooltipTest( | ||
<> | ||
<BarGroup horizontal> | ||
<BarSeries dataKey={series1.key} {...series1} /> | ||
<BarSeries dataKey={series2.key} {...series2} /> | ||
</BarGroup> | ||
<EventEmitter /> | ||
</>, | ||
{ showTooltip, hideTooltip }, | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint import/no-extraneous-dependencies: 'off' */ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { DataProvider, EventEmitterProvider, TooltipContext, TooltipContextType } from '../../src'; | ||
|
||
const providerProps = { | ||
initialDimensions: { width: 100, height: 100 }, | ||
xScale: { type: 'linear' }, | ||
yScale: { type: 'linear' }, | ||
} as const; | ||
|
||
const defaultTooltipContext = { | ||
tooltipOpen: false, | ||
/* eslint-disable no-undef */ | ||
showTooltip: jest.fn(), // eslint doesn't know jest is in context in non-.test file | ||
updateTooltip: jest.fn(), | ||
hideTooltip: jest.fn(), | ||
/* eslint-enable no-undef */ | ||
}; | ||
|
||
// sets up boilerplate context for testing tooltips | ||
export default function setupTooltipTest( | ||
children: React.ReactNode, | ||
tooltipContext?: Partial<TooltipContextType<object>>, | ||
) { | ||
return mount( | ||
<DataProvider {...providerProps}> | ||
<EventEmitterProvider> | ||
<TooltipContext.Provider value={{ ...defaultTooltipContext, ...tooltipContext }}> | ||
<svg>{children}</svg> | ||
</TooltipContext.Provider> | ||
</EventEmitterProvider> | ||
</DataProvider>, | ||
); | ||
} |