Skip to content

Commit

Permalink
fix(swarmplot): fix swarmplot node onMouseMove event (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
Streeterxs authored May 22, 2021
1 parent 3cf486f commit 7f99881
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/swarmplot/src/SwarmPlotNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const SwarmPlotNode = memo(
node,
onMouseEnter,
])
const handleMouseMove = useCallback(event => onMouseMove && onMouseEnter(node, event), [
const handleMouseMove = useCallback(event => onMouseMove && onMouseMove(node, event), [
node,
onMouseMove,
])
Expand Down
154 changes: 154 additions & 0 deletions packages/swarmplot/tests/Swarmplot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import React from 'react'
import { mount } from 'enzyme'
// @ts-ignore
import { linearGradientDef, patternDotsDef } from '@nivo/core'
import { generateSwarmPlotData } from '@nivo/generators'
// @ts-ignore
import { SwarmPlot } from '../src'

const sampleData = [
{
id: 'A',
group: 'group A',
price: 293,
volume: 5,
},
{
id: 'B',
group: 'group B',
price: 300,
volume: 6,
},
{
id: 'C',
group: 'group C',
price: 500,
volume: 6,
},
]

const groups = ['group A', 'group B', 'group C']

describe('SwarmPlot', () => {
describe('interactivity', () => {
it('should support onClick handler', () => {
const onClick = jest.fn()
const wrapper = mount(
<SwarmPlot
width={400}
height={400}
groupBy="group"
groups={groups}
data={sampleData}
onClick={onClick}
/>
)

wrapper.find('circle').at(0).simulate('click')

expect(onClick).toHaveBeenCalledTimes(1)
const [datum] = onClick.mock.calls[0]
expect(datum.id).toEqual('A')
})

it('should support onMouseEnter handler', () => {
const onMouseEnter = jest.fn()
const wrapper = mount(
<SwarmPlot
width={400}
height={400}
groupBy="group"
groups={groups}
data={sampleData}
onMouseEnter={onMouseEnter}
/>
)

wrapper.find('circle').at(1).simulate('mouseenter')

expect(onMouseEnter).toHaveBeenCalledTimes(1)
const [datum] = onMouseEnter.mock.calls[0]
expect(datum.id).toEqual('B')
})

it('should support onMouseMove handler', () => {
const onMouseMove = jest.fn()
const wrapper = mount(
<SwarmPlot
width={400}
height={400}
groupBy="group"
groups={groups}
data={sampleData}
onMouseMove={onMouseMove}
/>
)

wrapper.find('circle').at(2).simulate('mousemove')

expect(onMouseMove).toHaveBeenCalledTimes(1)
const [datum] = onMouseMove.mock.calls[0]
expect(datum.id).toEqual('C')
})

it('should support onMouseLeave handler', () => {
const onMouseLeave = jest.fn()
const wrapper = mount(
<SwarmPlot
width={400}
height={400}
groupBy="group"
groups={groups}
data={sampleData}
onMouseLeave={onMouseLeave}
/>
)

wrapper.find('circle').at(0).simulate('mouseleave')

expect(onMouseLeave).toHaveBeenCalledTimes(1)
const [datum] = onMouseLeave.mock.calls[0]
expect(datum.id).toEqual('A')
})

it.skip('should allow to completely disable interactivity', () => {
const onClick = jest.fn()
const onMouseEnter = jest.fn()
const onMouseMove = jest.fn()
const onMouseLeave = jest.fn()

const wrapper = mount(
<SwarmPlot
width={400}
height={400}
groupBy="group"
groups={groups}
data={sampleData}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
isInteractive={false}
/>
)

wrapper.find('circle').at(0).simulate('click')
wrapper.find('circle').at(0).simulate('mouseenter')
wrapper.find('circle').at(0).simulate('mousemove')
wrapper.find('circle').at(0).simulate('mouseleave')

expect(onClick).not.toHaveBeenCalled()
expect(onMouseEnter).not.toHaveBeenCalled()
expect(onMouseMove).not.toHaveBeenCalled()
expect(onMouseLeave).not.toHaveBeenCalled()

wrapper.find('circle').forEach(node => {
const circle = node.find('circle')
expect(circle.prop('onClick')).toBeUndefined()
expect(circle.prop('onMouseEnter')).toBeUndefined()
expect(circle.prop('onMouseMove')).toBeUndefined()
expect(circle.prop('onMouseLeave')).toBeUndefined()
})
})
})
})

0 comments on commit 7f99881

Please sign in to comment.