Skip to content

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

Conversation

eps1lon
Copy link
Member

@eps1lon eps1lon commented Jul 1, 2019

Closes testing-library/dom-testing-library#293, testing-library/dom-testing-library#292

View rendered text

@eps1lon eps1lon requested a review from kentcdodds July 1, 2019 12:17
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. I have a few edit suggestions.

possible. With this in mind, you should be aware that `fireEvent` is not actually
how the user interacts with your application.

Depending on the interaction multiple events can be fired in the browser and sometimes
Copy link
Member

@kentcdodds kentcdodds Jul 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we replace this paragraph and the next with this instead?

Suggested change
Depending on the interaction multiple events can be fired in the browser and sometimes
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 button, these are the events that are typically fired (in order):
- fireEvent.mouseOver(element)
- fireEvent.mouseMove(element)
- fireEvent.mouseDown(element)
- element.focus()
- 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 the time this isn't critical for your tests and the trade-off of simply using `fireEvent.click` is worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff. Though we start by mentioning a button and then transition to the child of a label. I replaced button with a generic element since I never encountered buttons as children of labels.

@eps1lon eps1lon requested a review from kentcdodds July 4, 2019 07:22
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two more things, then this is mergable!

@eps1lon eps1lon merged commit a7ada27 into testing-library:master Jul 7, 2019
@eps1lon eps1lon deleted the feat/fireEvent-recommendations branch July 7, 2019 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fireEvent.focus should be discouraged
3 participants