Skip to content

fix: make fireEvent mouseEnter/mouseLeave work with addEventListener #588

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/__tests__/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ const eventTypes = [
},
]

// native select event isn't passing
const nonNativeEvents = {doubleClick: 'dblclick', select: true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should doubleclick even be in the events list?

Also, select probably doesn't work because JSDOM, which is unfortunate.

I probably wouldn't implement things this way and instead I would just have if statements in the test itself for each of these individually with comments explaining why these are special cases. No reason to have this indirection/abstraction.

Copy link
Contributor Author

@zbrogz zbrogz Feb 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kentcdodds I made some changes based on your feedback.

instead I would just have if statements in the test itself for each of these individually with comments explaining why these are special cases

Done

Should doubleclick even be in the events list?

I think it makes sense to test both the doubleClick (synthetic) and dblclick (native) events.

Also, select probably doesn't work because JSDOM, which is unfortunate.

I included a fix for this in the latest commit.


eventTypes.forEach(({type, events, elementType, init}) => {
describe(`${type} Events`, () => {
events.forEach(eventName => {
Expand All @@ -153,6 +156,42 @@ eventTypes.forEach(({type, events, elementType, init}) => {
})
})

eventTypes.forEach(({type, events, elementType, init}) => {
describe(`Native ${type} Events`, () => {
events.forEach(eventName => {
let nativeEventName = eventName.toLowerCase()
if (nonNativeEvents[eventName]) {
if (nonNativeEvents[eventName] === true) {
return
}
nativeEventName = nonNativeEvents[eventName]
}

it(`triggers native ${nativeEventName}`, () => {
const ref = React.createRef()
const spy = jest.fn()
const Element = elementType

const NativeEventElement = () => {
React.useEffect(() => {
const element = ref.current
element.addEventListener(nativeEventName, spy)
return () => {
element.removeEventListener(nativeEventName, spy)
}
})
return <Element ref={ref} />
}

render(<NativeEventElement />)

fireEvent[eventName](ref.current, init)
expect(spy).toHaveBeenCalledTimes(1)
})
})
})
})

test('onChange works', () => {
const handleChange = jest.fn()
const {
Expand Down
12 changes: 10 additions & 2 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ Object.keys(dtlFireEvent).forEach(key => {
// React event system tracks native mouseOver/mouseOut events for
// running onMouseEnter/onMouseLeave handlers
// @link https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/EnterLeaveEventPlugin.js#L24-L31
fireEvent.mouseEnter = fireEvent.mouseOver
fireEvent.mouseLeave = fireEvent.mouseOut
const mouseEnter = fireEvent.mouseEnter
const mouseLeave = fireEvent.mouseLeave
fireEvent.mouseEnter = (...args) => {
mouseEnter(...args)
return fireEvent.mouseOver(...args)
}
fireEvent.mouseLeave = (...args) => {
mouseLeave(...args)
return fireEvent.mouseOut(...args)
}

fireEvent.select = (node, init) => {
// React tracks this event only on focused inputs
Expand Down