You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1
Original file line number
Diff line number
Diff line change
@@ -143,6 +143,7 @@ To enable this configuration use the `extends` property in your
143
143
|[prefer-find-by](docs/rules/prefer-find-by.md)| Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries |![dom-badge][]![angular-badge][]![react-badge][]![vue-badge][]|![fixable-badge][]|
144
144
|[prefer-presence-queries](docs/rules/prefer-presence-queries.md)| Enforce specific queries when checking element is present or not |||
145
145
|[prefer-screen-queries](docs/rules/prefer-screen-queries.md)| Suggest using screen while using queries |![dom-badge][]![angular-badge][]![react-badge][]![vue-badge][]||
146
+
|[prefer-user-event](docs/rules/prefer-user-event.md)| Suggest using `userEvent` library instead of `fireEvent` for simulating user interaction |![react-badge][]||
146
147
|[prefer-wait-for](docs/rules/prefer-wait-for.md)| Use `waitFor` instead of deprecated wait methods ||![fixable-badge][]|
> [...] 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 rules enforces the usage of [userEvent](https://github.com/testing-library/user-event) methods over `fireEvent`. By default, the methods from `userEvent` takes precedence, but you add exceptions by configuring the rule in `.eslintrc`
15
+
16
+
See below for examples of valid usages of `fireEvent` methods with the configuration.
17
+
18
+
Examples of **incorrect** code for this rule:
19
+
20
+
```ts
21
+
// a method in fireEvent that has a userEvent equivalent
// using fireEvent after importing the entire library
30
+
import*asdomfrom'@testing-library/dom';
31
+
dom.fireEvent.click(node);
32
+
```
33
+
34
+
Examples of **correct** code for this rule:
35
+
36
+
```ts
37
+
// any userEvent method
38
+
userEvent.click();
39
+
// fireEvent method that does not have an alternative in userEvent
40
+
fireEvent.cut(node);
41
+
import*asdomfrom'@testing-library/dom';
42
+
dom.fireEvent.cut(node);
43
+
44
+
// a function called fireEvent that's not imported from testing-library
45
+
function fireEvent() {
46
+
// do stuff
47
+
}
48
+
fireEvent();
49
+
```
50
+
51
+
#### Options
52
+
53
+
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.
54
+
55
+
The configuration consists of an array of strings with the names of fireEvents methods to be excluded.
56
+
An example looks like this
57
+
58
+
```json
59
+
{
60
+
"rules": {
61
+
"prefer-user-event": [
62
+
"error",
63
+
{
64
+
"allowedMethods": ["click", "change"]
65
+
}
66
+
]
67
+
}
68
+
}
69
+
```
70
+
71
+
With this configuration example, the following use cases are considered valid
-[userEvent in the react-testing-library docs](https://testing-library.com/docs/ecosystem-user-event)
98
+
99
+
## Appendix
100
+
101
+
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)
102
+
103
+
| fireEvent method | Possible options in userEvent |
0 commit comments