@testing-library/user-event

dog

Fire events the same way the user does

--- [![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] [![All Contributors][all-contributors-badge]](#contributors) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] ## The problem From [testing-library/dom-testing-library#107](https://github.com/testing-library/dom-testing-library/issues/107): > [...] it is becoming apparent the need to express user actions on a web page > using a higher-level abstraction than `fireEvent` ## The solution `user-event` tries to simulate the real events that would happen in the browser as the user interacts with it. For example `userEvent.click(checkbox)` would change the state of the checkbox. **The library is still a work in progress and any help is appreciated.** ## Table of Contents - [Installation](#installation) - [API](#api) - [`click(element, eventInit, options)`](#clickelement-eventinit-options) - [`dblClick(element, eventInit, options)`](#dblclickelement-eventinit-options) - [`type(element, text, [options])`](#typeelement-text-options) - [`upload(element, file, [{ clickInit, changeInit }])`](#uploadelement-file--clickinit-changeinit-) - [`clear(element)`](#clearelement) - [`selectOptions(element, values)`](#selectoptionselement-values) - [`deselectOptions(element, values)`](#deselectoptionselement-values) - [`tab({shift, focusTrap})`](#tabshift-focustrap) - [`hover(element)`](#hoverelement) - [`unhover(element)`](#unhoverelement) - [`paste(element, text, eventInit, options)`](#pasteelement-text-eventinit-options) - [`specialChars`](#specialchars) - [Issues](#issues) - [πŸ› Bugs](#-bugs) - [πŸ’‘ Feature Requests](#-feature-requests) - [Contributors ✨](#contributors-) - [LICENSE](#license) ## Installation With NPM: ```sh npm install @testing-library/user-event @testing-library/dom --save-dev ``` With Yarn: ```sh yarn add @testing-library/user-event @testing-library/dom --dev ``` Now simply import it in your tests: ```js import userEvent from '@testing-library/user-event' // or const {default: userEvent} = require('@testing-library/user-event') ``` ## API Note: All userEvent methods are synchronous with one exception: when `delay` with `userEvent.type` as described below). We also discourage using `userEvent` inside `before/after` blocks at all, for important reasons described in ["Avoid Nesting When You're Testing"](https://kentcdodds.com/blog/avoid-nesting-when-youre-testing). ### `click(element, eventInit, options)` Clicks `element`, depending on what `element` is it can have different side effects. ```jsx import React from 'react' import {render, screen} from '@testing-library/react' import userEvent from '@testing-library/user-event' test('click', () => { render(
, ) userEvent.click(screen.getByText('Check')) expect(screen.getByLabelText('Check')).toBeChecked() }) ``` You can also ctrlClick / shiftClick etc with ```js userEvent.click(elem, {ctrlKey: true, shiftKey: true}) ``` See the [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) constructor documentation for more options. Note that `click` will trigger hover events before clicking. To disable this, set the `skipHover` option to `true`. ### `dblClick(element, eventInit, options)` Clicks `element` twice, depending on what `element` is it can have different side effects. ```jsx import React from 'react' import {render, screen} from '@testing-library/react' import userEvent from '@testing-library/user-event' test('double click', () => { const onChange = jest.fn() render() const checkbox = screen.getByRole('checkbox') userEvent.dblClick(checkbox) expect(onChange).toHaveBeenCalledTimes(2) expect(checkbox).not.toBeChecked() }) ``` ### `type(element, text, [options])` Writes `text` inside an `` or a `