diff --git a/README.md b/README.md
index c951f48b..73062c11 100644
--- a/README.md
+++ b/README.md
@@ -50,7 +50,6 @@ change the state of the checkbox.
-
- [Installation](#installation)
- [API](#api)
- [`click(element)`](#clickelement)
@@ -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)
@@ -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(
+ ,
+ )
+
+ userEvent.hover(screen.getByTestId('button'))
+ expect(handler).toHaveBeenCalledTimes(5)
+})
+```
+
## Issues
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
@@ -482,6 +509,7 @@ Thanks goes to these people ([emoji key][emojis]):
+
This project follows the [all-contributors][all-contributors] specification.
diff --git a/src/__tests__/hover.js b/src/__tests__/hover.js
new file mode 100644
index 00000000..5edebc99
--- /dev/null
+++ b/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()
+
+ 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
+ ,
+ )
+
+ userEvent.hover(screen.getByTestId('button'))
+ expect(handler).toHaveBeenCalledTimes(5)
+})
diff --git a/src/index.js b/src/index.js
index d09cb723..0f0dbbe9 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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,
@@ -460,6 +468,7 @@ const userEvent = {
type,
upload,
tab,
+ hover,
}
export default userEvent