-
Notifications
You must be signed in to change notification settings - Fork 727
Add guide for events #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eps1lon
merged 6 commits into
testing-library:master
from
eps1lon:feat/fireEvent-recommendations
Jul 7, 2019
Merged
Add guide for events #172
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
id: guide-events | ||
title: Considerations for fireEvent | ||
--- | ||
|
||
## Interactions vs. events | ||
|
||
Based on [the Guiding Principles](guiding-principles.md), your test should | ||
resemble how users interact with your code (component, page, etc.) as much as | ||
possible. With this in mind, you should know that `fireEvent` isn't _exactly_ | ||
how the user interacts with your application, but it's close enough for most | ||
scenarios. | ||
|
||
Consider `fireEvent.click` which creates a click event and dispatches that event | ||
on the given DOM node. This works properly for most situations when you simply | ||
want to test what happens when your element is clicked, but when the _user_ | ||
actually clicks your element, these are the events that are typically fired (in | ||
order): | ||
|
||
- fireEvent.mouseOver(element) | ||
- fireEvent.mouseMove(element) | ||
- fireEvent.mouseDown(element) | ||
- element.focus() (if that element is focusable) | ||
- fireEvent.mouseUp(element) | ||
- fireEvent.click(element) And then, if that element happens to be a child of a | ||
`label`, then it will also move focus to the form control that the label is | ||
labeling. So even though all you really are trying to test is the click | ||
handler, by simply using `fireEvent.click` you're missing out on several other | ||
potentially important events the user is firing along the way. Again, most of | ||
eps1lon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the time this isn't critical for your tests and the trade-off of simply using | ||
`fireEvent.click` is worth it. | ||
|
||
## Alternatives | ||
|
||
We will describe a couple of simple adjustments to your tests that will increase | ||
your confidence in the interactive behavior of your components. For other | ||
interactions you may want to either consider using | ||
[`user-event`](ecosystem-user-event) or testing your components in a real | ||
environment (e.g. manually, automatic with cypress etc.). | ||
|
||
### Keydown | ||
|
||
[A keydown is dispatched on the currently focused element, the body element or the document element](https://w3c.github.io/uievents/#events-keyboard-event-order). | ||
Following this you should prefer | ||
|
||
```diff | ||
- fireEvent.keyDown(getByText('click me')); | ||
+ getByText('click me').focus(); | ||
+ fireEvent.keyDown(document.activeElement || document.body); | ||
``` | ||
|
||
This will also test that the element in question can even receive keyboard | ||
events. | ||
|
||
### Focus/Blur | ||
|
||
If an element is focused not only a focus event is dispatched. The active | ||
element in the document also changes as well as the previously focused element | ||
getting blurred. To simulate this behavior you can simply replace `fireEvent` | ||
with imperative focus: | ||
|
||
```diff | ||
- fireEvent.focus(getByText('focus me'); | ||
+ getByText('focus me').focus(); | ||
``` | ||
|
||
A nice side-effect of this approach is that any assertion on fired focus events | ||
will fail if the element is not focusable. This is especially important if you | ||
follow-up with a keydown event. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.