Skip to content

Commit

Permalink
feat(hover): Add hover API (fixes #254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmccurdy committed Jun 9, 2020
1 parent dbdefef commit c286e4d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
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)`

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)
`)
})

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) {
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

0 comments on commit c286e4d

Please sign in to comment.