Skip to content
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

feat(hover): Add hover API (fixes #254) #331

Merged
merged 6 commits into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Expand Up @@ -50,7 +50,6 @@ change the state of the checkbox.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Installation](#installation)
- [API](#api)
- [`click(element)`](#clickelement)
Expand All @@ -61,6 +60,7 @@ change the state of the checkbox.
- [`selectOptions(element, values)`](#selectoptionselement-values)
- [`toggleSelectOptions(element, values)`](#toggleselectoptionselement-values)
- [`tab({shift, focusTrap})`](#tabshift-focustrap)
- [`hover(element)`](#hoverelement)
- [Issues](#issues)
- [🐛 Bugs](#-bugs)
- [💡 Feature Requests](#-feature-requests)
Expand Down Expand Up @@ -398,6 +398,33 @@ it('should cycle elements in document tab order', () => {
})
```

### `hover(element)`

kentcdodds marked this conversation as resolved.
Show resolved Hide resolved
Hovers over `element`.

```jsx
import React from 'react'
import {render, screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'

test('hover', () => {
const handler = jest.fn()
render(
<button
data-testid="button"
onMouseEnter={handler}
onMouseOver={handler}
onMouseMove={handler}
onMouseOut={handler}
onMouseLeave={handler}
/>,
)

userEvent.hover(screen.getByTestId('button'))
expect(handler).toHaveBeenCalledTimes(5)
})
```

## Issues

_Looking to contribute? Look for the [Good First Issue][good-first-issue]
Expand Down Expand Up @@ -482,6 +509,7 @@ Thanks goes to these people ([emoji key][emojis]):

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
35 changes: 35 additions & 0 deletions src/__tests__/hover.js
@@ -0,0 +1,35 @@
import React from 'react'
import {render, screen} from '@testing-library/react'
import userEvent from '..'
import {setup} from './helpers/utils'

test('hover', () => {
const {element, getEventCalls} = setup(<button />)

userEvent.hover(element)
expect(getEventCalls()).toMatchInlineSnapshot(`
mouseenter: Left (0)
mouseover: Left (0)
mousemove: Left (0)
mouseout: Left (0)
mouseleave: Left (0)
nickmccurdy marked this conversation as resolved.
Show resolved Hide resolved
`)
})

test('hover should fire events', () => {
const handler = jest.fn()
render(
// eslint-disable-next-line jsx-a11y/mouse-events-have-key-events
<button
data-testid="button"
onMouseEnter={handler}
onMouseOver={handler}
onMouseMove={handler}
onMouseOut={handler}
onMouseLeave={handler}
/>,
)

userEvent.hover(screen.getByTestId('button'))
expect(handler).toHaveBeenCalledTimes(5)
})
9 changes: 9 additions & 0 deletions src/index.js
Expand Up @@ -451,6 +451,14 @@ function tab({shift = false, focusTrap = document} = {}) {
}
}

function hover(element, init) {
nickmccurdy marked this conversation as resolved.
Show resolved Hide resolved
fireEvent.mouseEnter(element, getMouseEventOptions('mouseenter', init))
fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init))
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
fireEvent.mouseOut(element, getMouseEventOptions('mouseout', init))
fireEvent.mouseLeave(element, getMouseEventOptions('mouseleave', init))
}

const userEvent = {
click,
dblClick,
Expand All @@ -460,6 +468,7 @@ const userEvent = {
type,
upload,
tab,
hover,
}

export default userEvent
Expand Down