Skip to content

Commit c8126d5

Browse files
committed
fix: bubble up mouseover and mouseout events
1 parent 02b1e4e commit c8126d5

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

lib/test-setup.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ var exposedProperties = ['window', 'navigator', 'document']
88

99
// http://airbnb.io/enzyme/docs/guides/jsdom.html
1010
global.document = jsdom('')
11+
global.document.createRange = () => ({
12+
commonAncestorContainer: global.document.body,
13+
setStart () {},
14+
setEnd () {},
15+
})
1116
global.window = document.defaultView
1217
Object.keys(document.defaultView).forEach((property) => {
1318
if (typeof global[property] === 'undefined') {
@@ -16,8 +21,11 @@ Object.keys(document.defaultView).forEach((property) => {
1621
}
1722
})
1823
global.navigator = {
19-
userAgent: 'node.js'
24+
userAgent: 'node.js',
25+
appVersion: { indexOf () {} },
2026
}
27+
global.Node = {}
28+
global.requestAnimationFrame = (fn) => fn()
2129

2230
// enzyme, and therefore chai-enzyme, needs to be required after
2331
// global.navigator is set up (https://github.com/airbnb/enzyme/issues/395)

src/tooltip.jsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ class Tooltip extends Component {
2828
}
2929

3030
render () {
31+
const child = Children.only(this.props.children)
32+
3133
const actionProps = this.props.visible == null ? {
32-
onMouseOver: () => this.setState({ shouldShow: true }),
33-
onMouseOut: () => this.setState({ shouldShow: false }),
34+
onMouseOver: (...args) => {
35+
child.props.onMouseOver && child.props.onMouseOver(...args)
36+
this.setState({ shouldShow: true })
37+
},
38+
onMouseOut: (...args) => {
39+
child.props.onMouseOut && child.props.onMouseOut(...args)
40+
this.setState({ shouldShow: false })
41+
},
3442
} : {}
3543

3644
return (

src/tooltip.spec.jsx

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash'
22
import React from 'react'
3-
import { shallow } from 'enzyme'
3+
import { mount, shallow } from 'enzyme'
4+
import sinon from 'sinon'
45

56
import PortalPopper from './portal-popper'
67

@@ -11,9 +12,9 @@ const defaultProps = {
1112
title: 'Do foo',
1213
}
1314

14-
const createComponent = (props) => (
15+
const createComponent = (props, child) => (
1516
<Tooltip {..._.extend({}, defaultProps, props)}>
16-
<div className='target' />
17+
{child || <div className='target' />}
1718
</Tooltip>
1819
)
1920

@@ -48,6 +49,22 @@ describe('<Tooltip />', () => {
4849
expect(component.find('span')).to.have.className('custom-wrap-class')
4950
})
5051

52+
it('calls onMouseOver if specified', () => {
53+
const onMouseOver = sinon.spy()
54+
const component = mount(createComponent({}, <div onMouseOver={onMouseOver} />))
55+
const args = [1, 2, 3]
56+
component.find('div').prop('onMouseOver')(...args)
57+
expect(onMouseOver).to.be.calledWith(...args)
58+
})
59+
60+
it('calls onMouseOut if specified', () => {
61+
const onMouseOut = sinon.spy()
62+
const component = mount(createComponent({}, <div onMouseOut={onMouseOut} />))
63+
const args = [1, 2, 3]
64+
component.find('div').prop('onMouseOut')(...args)
65+
expect(onMouseOut).to.be.calledWith(...args)
66+
})
67+
5168
describe('when visible is not explicitly specified', () => {
5269
let component
5370
beforeEach(() => {

0 commit comments

Comments
 (0)