-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add fireEvent from dom-testing-library #48
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
Changes from 5 commits
8a10c39
2fda80f
108e295
7f7daa6
2381249
6cbbce9
cc2310b
eb482e7
32f7560
8304913
9006f0e
04fec2e
a85fad9
e210c0c
404e14b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,8 @@ facilitate testing implementation details). Read more about this in | |
* [Installation](#installation) | ||
* [Usage](#usage) | ||
* [`render`](#render) | ||
* [`renderIntoDocument`](#renderintodocument) | ||
* [`clearDocument`](#cleardocument) | ||
* [`Simulate`](#simulate) | ||
* [`wait`](#wait) | ||
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event) | ||
|
@@ -265,6 +267,27 @@ const usernameInputElement = getByTestId('username-input') | |
> Learn more about `data-testid`s from the blog post | ||
> ["Making your UI tests resilient to change"][data-testid-blog-post] | ||
|
||
### `renderIntoDocument` | ||
|
||
Render into `document.body`. Should be used with [clearDocument](#cleardocument) | ||
|
||
```javascript | ||
renderIntoDocument(<div>) | ||
``` | ||
|
||
### `clearDocument` | ||
|
||
Clears the `document.body`. Good for preventing memory leaks. Should be used with [renderIntoDocument](#renderintodocument) | ||
|
||
```javascript | ||
afterEach(clearDocument) | ||
|
||
test('renders into document', () => { | ||
renderIntoDocument(<div>) | ||
// ... | ||
}) | ||
``` | ||
|
||
### `Simulate` | ||
|
||
This is simply a re-export from the `Simulate` utility from | ||
|
@@ -318,24 +341,60 @@ intervals. | |
|
||
Fire DOM events. | ||
|
||
React attaches an event handler on the `document` and handles some DOM events via event delegation (events bubbling up from a `target` to an ancestor). Because of this, your `node` must be in the `document.body` for `fireEvent` to work with React. You can render into the document using the [renderIntoDocument](#renderintodocument) utility. This is an alternative to simulating Synthetic React Events via [Simulate](#simulate). The benefit of using `fireEvent` over `Simulate` is that you are testing real DOM events instead of Synthetic Events. This aligns better with [the Guiding Principles](#guiding-principles). | ||
|
||
```javascript | ||
// <button>Submit</button> | ||
fireEvent( | ||
getElementByText('Submit'), | ||
new MouseEvent('click', { | ||
bubbles: true, | ||
cancelable: true, | ||
}), | ||
) | ||
import { renderIntoDocument, clearDocument, render, fireEvent } | ||
|
||
// don't forget to clean up the document.body | ||
afterEach(clearDocument) | ||
|
||
test('clicks submit button', () => { | ||
const spy = jest.fn(); | ||
const { unmount, getByText } = render(<button onClick={spy}>Submit</button>) | ||
|
||
fireEvent( | ||
getByText('Submit'), | ||
new MouseEvent('click', { | ||
bubbles: true, // click events must bubble for React to see it | ||
cancelable: true, | ||
}) | ||
) | ||
|
||
// don't forget to unmount component so componentWillUnmount can clean up subscriptions | ||
unmount(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
``` | ||
|
||
#### `fireEvent[eventName](node: HTMLElement, eventInit)` | ||
#### `fireEvent[eventName](node: HTMLElement, eventProperties: Object)` | ||
|
||
Convenience methods for firing DOM events. Look [here](./src/events.js) for full list. | ||
Convenience methods for firing DOM events. Check out | ||
[dom-testing-library/src/events.js](https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js) | ||
for a full list as well as default `eventProperties`. | ||
|
||
```javascript | ||
// <button>Submit</button> | ||
fireEvent.click(getElementByText('Submit')) | ||
import { renderIntoDocument, clearDocument, render, fireEvent } | ||
|
||
// don't forget to clean up the document.body | ||
afterEach(clearDocument) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
test('right clicks submit button', () => { | ||
const spy = jest.fn(); | ||
const onClick = e => e.button === 2 && spy(); | ||
const { unmount, getByText } = render(<button onClick={onClick}>Submit</button>) | ||
|
||
// click will bubble for React to see it | ||
const rightClick = {button: 2} | ||
fireEvent.click(getElementByText('Submit'), rightClick) | ||
// default `button` property for click events is set to `0` which is a left click. | ||
|
||
// don't forget to unmount component so componentWillUnmount can clean up subscriptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this totally required for react to do its job? I'm not certain it's necessary 🤔 If it is then perhaps our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok we can do it in the library, but i think this is even more reason to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done but kept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This makes a lot of sense. Sorry, but could you change it back? 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. haha np, will change it back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
unmount(); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
``` | ||
|
||
## `TextMatch` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this example to the same in dom-testing-library: