Skip to content

Commit 0414363

Browse files
meatnordrinktimdeschryveralexkrolickeps1lonph-fritsche
authored
Improving intro clarity (#1086)
* Improving intro clarity Several engineers at our company have had trouble understanding what the v14 upgrade entailed for us. Having gone through it, it's simple enough, but these changes attempt to clarify things by a) improving grammar b) clarifying a few key points and examples * Update docs/user-event/intro.mdx Adopting CR suggestion Co-authored-by: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> * Further grammar tweaks Attempting to address CR comment * Update docs/user-event/intro.mdx Accepting CR suggestion Co-authored-by: Alex Krolick <alexkrolick@users.noreply.github.com> * Accepting CR suggested edit * accept change simulates => dispatches Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> * be more explicit and re-add link Co-authored-by: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Co-authored-by: Alex Krolick <alexkrolick@users.noreply.github.com> Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com> Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>
1 parent 4f8445a commit 0414363

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

docs/user-event/intro.mdx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,49 @@ events that would happen if the interaction took place in a browser.
1111

1212
These docs describe `user-event@14`. We recommend updating your projects to this
1313
version, as it includes important bug fixes and new features. You can find the
14-
docs for `user-event@13.5.0` [here](../ecosystem-user-event.mdx).
14+
docs for `user-event@13.5.0` [here](../ecosystem-user-event.mdx), and the
15+
changelog for the release
16+
[here](https://github.com/testing-library/user-event/releases/tag/v14.0.0).
1517

1618
:::
1719

1820
While most examples with `user-event` are for `React`, the library can be used
1921
with any framework as long as there is a DOM.
2022

21-
## Difference to `fireEvent`
23+
## Differences from `fireEvent`
2224

23-
The built-in [`fireEvent`](dom-testing-library/api-events.mdx#fireevent) is a
24-
utility to easily dispatch events. It dispatches exactly the events you tell it
25-
to and just those - even if those exact events never had been dispatched in a
26-
real interaction in a browser.
25+
`fireEvent` dispatches _DOM events_, whereas `user-event` simulates full
26+
_interactions_, which may fire multiple events and do additional checks along
27+
the way.
2728

28-
`user-event` on the other hand dispatches the events like they would happen if a
29-
user interacted with the document. That might lead to the same events you
30-
previously dispatched per `fireEvent` directly, but it also might catch bugs
31-
that make it impossible for a user to trigger said events. This is
29+
Testing Library's built-in
30+
[`fireEvent`](dom-testing-library/api-events.mdx#fireevent) is a lightweight
31+
wrapper around the browser's low-level `dispatchEvent` API, which allows
32+
developers to trigger any event on any element. The problem is that the browser
33+
usually does more than just trigger one event for one interaction. For example,
34+
when a user types into a text box, the element has to be focused, and then
35+
keyboard and input events are fired and the selection and value on the element
36+
are manipulated as they type.
37+
38+
`user-event` allows you to describe a user interaction instead of a concrete
39+
event. It adds visibility and interactability checks along the way and
40+
manipulates the DOM just like a user interaction in the browser would. It
41+
factors in that the browser e.g. wouldn't let a user click a hidden element or
42+
type in a disabled text box.
43+
This is
3244
[why you should use `user-event`](https://ph-fritsche.github.io/blog/post/why-userevent)
3345
to test interaction with your components.
3446

3547
## Writing tests with `userEvent`
3648

37-
We recommend to use [`userEvent.setup()`](setup.mdx) when rendering your
38-
component and inline that rendering and setup in your test or use a setup
39-
function. We discourage rendering or using any `userEvent` functions outside of
40-
the test itself - e.g. in a `before`/`after` hook - for reasons described in
49+
We recommend invoking [`userEvent.setup()`](setup.mdx) before the component is
50+
rendered. This can be done in the test itself, or by using a setup function. We
51+
discourage rendering or using any `userEvent` functions outside of the test
52+
itself - e.g. in a `before`/`after` hook - for reasons described in
4153
["Avoid Nesting When You're Testing"](https://kentcdodds.com/blog/avoid-nesting-when-youre-testing).
4254

4355
```js
56+
// inlining
4457
test('trigger some awesome feature when clicking the button', async () => {
4558
const user = userEvent.setup()
4659
render(<MyComponent />)
@@ -52,6 +65,7 @@ test('trigger some awesome feature when clicking the button', async () => {
5265
```
5366

5467
```js
68+
// setup function
5569
function setup(jsx) {
5670
return {
5771
user: userEvent.setup(),
@@ -64,3 +78,9 @@ test('render with a setup function', async () => {
6478
// ...
6579
})
6680
```
81+
82+
Note that, while directly invoking APIs such as `userEvent.click()` (which will
83+
trigger `setup` internally) is (still supported in
84+
v14)[https://testing-library.com/docs/user-event/setup#direct-apis], this option
85+
exists to ease the migration from v13 to v14, and for simple tests. We recommend
86+
using the methods on the instances returned by `userEvent.setup()`.

0 commit comments

Comments
 (0)