|
| 1 | +# Use [userEvent](https://github.com/testing-library/user-event) over using `fireEvent` for user interactions (prefer-user-event) |
| 2 | + |
| 3 | +From |
| 4 | +[testing-library/dom-testing-library#107](https://github.com/testing-library/dom-testing-library/issues/107): |
| 5 | + |
| 6 | +> [...] it is becoming apparent the need to express user actions on a web page |
| 7 | +> using a higher-level abstraction than `fireEvent` |
| 8 | +
|
| 9 | +`userEvent` adds related event calls from browsers to make tests more realistic than its counterpart `fireEvent`, which is a low-level api. |
| 10 | +See the appendix at the end to check how are the events from `fireEvent` mapped to `userEvent`. |
| 11 | + |
| 12 | +## Rule Details |
| 13 | + |
| 14 | +This rule enforces the usage of [userEvent](https://github.com/testing-library/user-event) methods over `fireEvent`. By default, the methods from `userEvent` take precedence, but you add exceptions by configuring the rule in `.eslintrc`. |
| 15 | + |
| 16 | +Examples of **incorrect** code for this rule: |
| 17 | + |
| 18 | +```ts |
| 19 | +// a method in fireEvent that has a userEvent equivalent |
| 20 | +import { fireEvent } from '@testing-library/dom'; |
| 21 | +fireEvent.click(node); |
| 22 | + |
| 23 | +// using fireEvent with an alias |
| 24 | +import { fireEvent as fireEventAliased } from '@testing-library/dom'; |
| 25 | +fireEventAliased.click(node); |
| 26 | + |
| 27 | +// using fireEvent after importing the entire library |
| 28 | +import * as dom from '@testing-library/dom'; |
| 29 | +dom.fireEvent.click(node); |
| 30 | +``` |
| 31 | + |
| 32 | +Examples of **correct** code for this rule: |
| 33 | + |
| 34 | +```ts |
| 35 | +import userEvent from '@testing-library/user-event'; |
| 36 | + |
| 37 | +// any userEvent method |
| 38 | +userEvent.click(); |
| 39 | + |
| 40 | +// fireEvent method that does not have an alternative in userEvent |
| 41 | +fireEvent.cut(node); |
| 42 | + |
| 43 | +import * as dom from '@testing-library/dom'; |
| 44 | +dom.fireEvent.cut(node); |
| 45 | +``` |
| 46 | + |
| 47 | +#### Options |
| 48 | + |
| 49 | +This rule allows to exclude specific functions with an equivalent in `userEvent` through configuration. This is useful if you need to allow an event from `fireEvent` to be used in the solution. For specific scenarios, you might want to consider disabling the rule inline. |
| 50 | + |
| 51 | +The configuration consists of an array of strings with the names of fireEvents methods to be excluded. |
| 52 | +An example looks like this |
| 53 | + |
| 54 | +```json |
| 55 | +{ |
| 56 | + "rules": { |
| 57 | + "prefer-user-event": [ |
| 58 | + "error", |
| 59 | + { |
| 60 | + "allowedMethods": ["click", "change"] |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +With this configuration example, the following use cases are considered valid |
| 68 | + |
| 69 | +```ts |
| 70 | +// using a named import |
| 71 | +import { fireEvent } from '@testing-library/dom'; |
| 72 | +fireEvent.click(node); |
| 73 | +fireEvent.change(node, { target: { value: 'foo' } }); |
| 74 | + |
| 75 | +// using fireEvent with an alias |
| 76 | +import { fireEvent as fireEventAliased } from '@testing-library/dom'; |
| 77 | +fireEventAliased.click(node); |
| 78 | +fireEventAliased.change(node, { target: { value: 'foo' } }); |
| 79 | + |
| 80 | +// using fireEvent after importing the entire library |
| 81 | +import * as dom from '@testing-library/dom'; |
| 82 | +dom.fireEvent.click(node); |
| 83 | +dom.fireEvent.change(node, { target: { value: 'foo' } }); |
| 84 | +``` |
| 85 | + |
| 86 | +## When Not To Use It |
| 87 | + |
| 88 | +When you don't want to use `userEvent`, such as if a legacy codebase is still using `fireEvent` or you need to have more low-level control over firing events (rather than the recommended approach of testing from a user's perspective) |
| 89 | + |
| 90 | +## Further Reading |
| 91 | + |
| 92 | +- [userEvent repository](https://github.com/testing-library/user-event) |
| 93 | +- [userEvent in the react-testing-library docs](https://testing-library.com/docs/ecosystem-user-event) |
| 94 | + |
| 95 | +## Appendix |
| 96 | + |
| 97 | +The following table lists all the possible equivalents from the low-level API `fireEvent` to the higher abstraction API `userEvent`. All the events not listed here do not have an equivalent (yet) |
| 98 | + |
| 99 | +| fireEvent method | Possible options in userEvent | |
| 100 | +| ---------------- | ----------------------------------------------------------------------------------------------------------- | |
| 101 | +| `click` | <ul><li>`click`</li><li>`type`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 102 | +| `change` | <ul><li>`upload`</li><li>`type`</li><li>`clear`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 103 | +| `dblClick` | <ul><li>`dblClick`</li></ul> | |
| 104 | +| `input` | <ul><li>`type`</li><li>`upload`</li><li>`selectOptions`</li><li>`deselectOptions`</li><li>`paste`</li></ul> | |
| 105 | +| `keyDown` | <ul><li>`type`</li><li>`tab`</li></ul> | |
| 106 | +| `keyPress` | <ul><li>`type`</li></ul> | |
| 107 | +| `keyUp` | <ul><li>`type`</li><li>`tab`</li></ul> | |
| 108 | +| `mouseDown` | <ul><li>`click`</li><li>`dblClick`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 109 | +| `mouseEnter` | <ul><li>`hover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 110 | +| `mouseLeave` | <ul><li>`unhover`</li></ul> | |
| 111 | +| `mouseMove` | <ul><li>`hover`</li><li>`unhover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 112 | +| `mouseOut` | <ul><li>`unhover`</li></ul> | |
| 113 | +| `mouseOver` | <ul><li>`hover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 114 | +| `mouseUp` | <ul><li>`click`</li><li>`dblClick`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 115 | +| `paste` | <ul><li>`paste`</li></ul> | |
| 116 | +| `pointerDown` | <ul><li>`click`</li><li>`dblClick`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 117 | +| `pointerEnter` | <ul><li>`hover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 118 | +| `pointerLeave` | <ul><li>`unhover`</li></ul> | |
| 119 | +| `pointerMove` | <ul><li>`hover`</li><li>`unhover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 120 | +| `pointerOut` | <ul><li>`unhover`</li></ul> | |
| 121 | +| `pointerOver` | <ul><li>`hover`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
| 122 | +| `pointerUp` | <ul><li>`click`</li><li>`dblClick`</li><li>`selectOptions`</li><li>`deselectOptions`</li></ul> | |
0 commit comments