diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md
index f6a0575d..9e04f824 100644
--- a/.github/ISSUE_TEMPLATE.md
+++ b/.github/ISSUE_TEMPLATE.md
@@ -43,10 +43,10 @@ tutorial to learn how: http://kcd.im/pull-request
-->
-* `react-testing-library` version:
-* `react` version:
-* `node` version:
-* `npm` (or `yarn`) version:
+- `react-testing-library` version:
+- `react` version:
+- `node` version:
+- `npm` (or `yarn`) version:
Relevant code or config
diff --git a/.github/ISSUE_TEMPLATE/Bug_Report.md b/.github/ISSUE_TEMPLATE/Bug_Report.md
index de5aaa11..f1debab9 100644
--- a/.github/ISSUE_TEMPLATE/Bug_Report.md
+++ b/.github/ISSUE_TEMPLATE/Bug_Report.md
@@ -22,10 +22,10 @@ tutorial to learn how: http://kcd.im/pull-request
-->
-* `react-testing-library` version:
-* `react` version:
-* `node` version:
-* `npm` (or `yarn`) version:
+- `react-testing-library` version:
+- `react` version:
+- `node` version:
+- `npm` (or `yarn`) version:
### Relevant code or config:
diff --git a/.github/ISSUE_TEMPLATE/Question.md b/.github/ISSUE_TEMPLATE/Question.md
index 595d5278..52790875 100644
--- a/.github/ISSUE_TEMPLATE/Question.md
+++ b/.github/ISSUE_TEMPLATE/Question.md
@@ -12,11 +12,9 @@ and feature requests so we recommend not using this medium to ask them here š
## ā Support Forums
-* React Spectrum
- https://spectrum.chat/react-testing-library
-* Reactiflux on Discord
- https://www.reactiflux.com
-* Stack Overflow
+- React Spectrum https://spectrum.chat/react-testing-library
+- Reactiflux on Discord https://www.reactiflux.com
+- Stack Overflow
https://stackoverflow.com/questions/tagged/react-testing-library
**ISSUES WHICH ARE QUESTIONS WILL BE CLOSED**
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index aa0dc2b8..ee765ccf 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -34,9 +34,11 @@ merge of your pull request!
-* [ ] Documentation
-* [ ] Tests
-* [ ] Ready to be merged
-* [ ] Added myself to contributors table
+- [ ] Documentation
+- [ ] Tests
+- [ ] Ready to be merged
+
+- [ ] Added myself to contributors table
+
diff --git a/.prettierrc b/.prettierrc
index fb31ee19..f3685197 100644
--- a/.prettierrc
+++ b/.prettierrc
@@ -6,5 +6,6 @@
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": false,
- "jsxBracketSameLine": false
+ "jsxBracketSameLine": false,
+ "proseWrap": "always"
}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 06d221aa..2a675299 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,5 @@
# CHANGELOG
-The changelog is automatically updated using [semantic-release](https://github.com/semantic-release/semantic-release).
-You can see it on the [releases page](../../releases).
+The changelog is automatically updated using
+[semantic-release](https://github.com/semantic-release/semantic-release). You
+can see it on the [releases page](../../releases).
diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
index afe24327..070cb5f6 100644
--- a/CODE_OF_CONDUCT.md
+++ b/CODE_OF_CONDUCT.md
@@ -5,30 +5,30 @@
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
-size, disability, ethnicity, gender identity and expression, level of experience,
-nationality, personal appearance, race, religion, or sexual identity and
-orientation.
+size, disability, ethnicity, gender identity and expression, level of
+experience, nationality, personal appearance, race, religion, or sexual identity
+and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
-* Using welcoming and inclusive language
-* Being respectful of differing viewpoints and experiences
-* Gracefully accepting constructive criticism
-* Focusing on what is best for the community
-* Showing empathy towards other community members
+- Using welcoming and inclusive language
+- Being respectful of differing viewpoints and experiences
+- Gracefully accepting constructive criticism
+- Focusing on what is best for the community
+- Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
-* The use of sexualized language or imagery and unwelcome sexual attention or
+- The use of sexualized language or imagery and unwelcome sexual attention or
advances
-* Trolling, insulting/derogatory comments, and personal or political attacks
-* Public or private harassment
-* Publishing others' private information, such as a physical or electronic
+- Trolling, insulting/derogatory comments, and personal or political attacks
+- Public or private harassment
+- Publishing others' private information, such as a physical or electronic
address, without explicit permission
-* Other conduct which could reasonably be considered inappropriate in a
+- Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
@@ -37,11 +37,11 @@ Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
-Project maintainers have the right and responsibility to remove, edit, or
-reject comments, commits, code, wiki edits, issues, and other contributions
-that are not aligned to this Code of Conduct, or to ban temporarily or
-permanently any contributor for other behaviors that they deem inappropriate,
-threatening, offensive, or harmful.
+Project maintainers have the right and responsibility to remove, edit, or reject
+comments, commits, code, wiki edits, issues, and other contributions that are
+not aligned to this Code of Conduct, or to ban temporarily or permanently any
+contributor for other behaviors that they deem inappropriate, threatening,
+offensive, or harmful.
## Scope
@@ -58,8 +58,9 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at kent+coc@doddsfamily.us. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
-obligated to maintain confidentiality with regard to the reporter of an incident.
-Further details of specific enforcement policies may be posted separately.
+obligated to maintain confidentiality with regard to the reporter of an
+incident. Further details of specific enforcement policies may be posted
+separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
@@ -67,8 +68,8 @@ members of the project's leadership.
## Attribution
-This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
-available at [http://contributor-covenant.org/version/1/4][version]
+This Code of Conduct is adapted from the [Contributor Covenant][homepage],
+version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 2cb6bdbf..8642e953 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -2,8 +2,8 @@
Thanks for being willing to contribute!
-**Working on your first Pull Request?** You can learn how from this _free_ series
-[How to Contribute to an Open Source Project on GitHub][egghead]
+**Working on your first Pull Request?** You can learn how from this _free_
+series [How to Contribute to an Open Source Project on GitHub][egghead]
## Project setup
@@ -20,33 +20,31 @@ Thanks for being willing to contribute!
> git branch --set-upstream-to=upstream/master master
> ```
>
-> This will add the original repository as a "remote" called "upstream,"
-> Then fetch the git information from that remote, then set your local `master`
-> branch to use the upstream master branch whenever you run `git pull`.
-> Then you can make all of your pull request branches based on this `master`
-> branch. Whenever you want to update your version of `master`, do a regular
-> `git pull`.
+> This will add the original repository as a "remote" called "upstream," Then
+> fetch the git information from that remote, then set your local `master`
+> branch to use the upstream master branch whenever you run `git pull`. Then you
+> can make all of your pull request branches based on this `master` branch.
+> Whenever you want to update your version of `master`, do a regular `git pull`.
## Add yourself as a contributor
-This project follows the [all contributors][all-contributors] specification.
-To add yourself to the table of contributors on the `README.md`, please use the
+This project follows the [all contributors][all-contributors] specification. To
+add yourself to the table of contributors on the `README.md`, please use the
automated script as part of your PR:
```console
npm run add-contributor
```
-Follow the prompt and commit `.all-contributorsrc` and `README.md` in the PR.
-If you've already added yourself to the list and are making
-a new type of contribution, you can run it again and select the added
-contribution type.
+Follow the prompt and commit `.all-contributorsrc` and `README.md` in the PR. If
+you've already added yourself to the list and are making a new type of
+contribution, you can run it again and select the added contribution type.
## Committing and Pushing changes
Please make sure to run the tests before you commit your changes. You can run
-`npm run test:update` which will update any snapshots that need updating.
-Make sure to include those changes (if they exist) in your commit.
+`npm run test:update` which will update any snapshots that need updating. Make
+sure to include those changes (if they exist) in your commit.
### opt into git hooks
@@ -67,6 +65,7 @@ Please checkout the [the open issues][issues]
Also, please watch the repo and respond to questions/bug reports/feature
requests! Thanks!
-[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
+[egghead]:
+ https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
[all-contributors]: https://github.com/kentcdodds/all-contributors
[issues]: https://github.com/kentcdodds/react-testing-library/issues
diff --git a/README.md b/README.md
index 7625525e..112f86af 100644
--- a/README.md
+++ b/README.md
@@ -12,13 +12,11 @@
[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
-[![version][version-badge]][package]
-[![downloads][downloads-badge]][npmtrends]
+[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
[](#contributors)
-[![PRs Welcome][prs-badge]][prs]
-[![Code of Conduct][coc-badge]][coc]
+[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Join the community on Spectrum][spectrum-badge]][spectrum]
[![Watch on GitHub][github-watch-badge]][github-watch]
@@ -28,9 +26,9 @@
## The problem
You want to write maintainable tests for your React components. As a part of
-this goal, you want your tests to avoid including implementation details of
-your components and rather focus on making your tests give you the confidence
-for which they are intended. As part of this, you want your testbase to be
+this goal, you want your tests to avoid including implementation details of your
+components and rather focus on making your tests give you the confidence for
+which they are intended. As part of this, you want your testbase to be
maintainable in the long run so refactors of your components (changes to
implementation but not functionality) don't break your tests and slow you and
your team down.
@@ -39,10 +37,11 @@ your team down.
The `react-testing-library` is a very light-weight solution for testing React
components. It provides light utility functions on top of `react-dom` and
-`react-dom/test-utils`, in a way that encourages better testing practices.
-It's primary guiding principle is:
+`react-dom/test-utils`, in a way that encourages better testing practices. It's
+primary guiding principle is:
-> [The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
+> [The more your tests resemble the way your software is used, the more
+> confidence they can give you.][guiding-principle]
So rather than dealing with instances of rendered react components, your tests
will work with actual DOM nodes. The utilities this library provides facilitate
@@ -66,8 +65,8 @@ facilitate testing implementation details). Read more about this in
**What this library is not**:
1. A test runner or framework
-2. Specific to a testing framework (though we recommend Jest as our
- preference, the library works with any framework)
+2. Specific to a testing framework (though we recommend Jest as our preference,
+ the library works with any framework)
> NOTE: This library is built on top of
> [`dom-testing-library`](https://github.com/kentcdodds/dom-testing-library)
@@ -81,13 +80,11 @@ facilitate testing implementation details). Read more about this in
- [Installation](#installation)
- [Usage](#usage)
- [`render`](#render)
- - [`renderIntoDocument`](#renderintodocument)
- [`cleanup`](#cleanup)
- - [`Simulate`](#simulate)
- - [`wait`](#wait)
- - [`waitForElement`](#waitforelement)
+- [`dom-testing-library` APIs](#dom-testing-library-apis)
- [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
- - [`prettyDOM`](#prettydom)
+ - [`waitForElement`](#waitforelement)
+ - [`wait`](#wait)
- [`TextMatch`](#textmatch)
- [`query` APIs](#query-apis)
- [`queryAll` and `getAll` APIs](#queryall-and-getall-apis)
@@ -124,12 +121,15 @@ You may also be interested in installing `jest-dom` so you can use
```javascript
// __tests__/fetch.js
import React from 'react'
-import {render, Simulate, wait} from 'react-testing-library'
+import {render, fireEvent, cleanup, waitForElement} from 'react-testing-library'
// this add custom jest matchers from jest-dom
import 'jest-dom/extend-expect'
import axiosMock from 'axios' // the mock lives in a __mocks__ directory
import Fetch from '../fetch' // see the tests for a full implementation
+// automatically unmount and cleanup DOM after the test is finished.
+afterEach(cleanup)
+
test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
// Arrange
axiosMock.get.mockResolvedValueOnce({data: {greeting: 'hello there'}})
@@ -137,11 +137,13 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
const {getByText, getByTestId, container} = render()
// Act
- Simulate.click(getByText('Load Greeting'))
+ fireEvent.click(getByText('Load Greeting'))
// let's wait for our mocked `get` request promise to resolve
// wait will wait until the callback doesn't throw an error
- await wait(() => getByTestId('greeting-text'))
+ const greetingTextNode = await waitForElement(() =>
+ getByTestId('greeting-text'),
+ )
// Assert
expect(axiosMock.get).toHaveBeenCalledTimes(1)
@@ -155,6 +157,46 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
### `render`
+Defined as:
+
+```typescript
+function render(
+ ui: React.ReactElement,
+ options?: {
+ /* You wont often use this, expand below for docs on options */
+ },
+): RenderResult
+```
+
+Render into a container which is appended to `document.body`. It should be used
+with [cleanup](#cleanup):
+
+```javascript
+import {render, cleanup} from 'react-testing-library'
+
+afterEach(cleanup)
+
+render()
+```
+
+
+
+Expand to see documentation on the options
+
+You wont often need to specify options, but if you ever do, here are the
+available options which you could provide as a second argument to `render`.
+
+**container**: By default, `react-testing-library` will create a `div` and
+append that div to the `document.body` and this is where your react component
+will be rendered. If you provide your own HTMLElement `container` via this
+option, it will not be appended to the `document.body` automatically.
+
+**baseElement**: If the `container` is specified, then this defaults to that,
+otherwise this defaults to `document.documentElement`. This is used as the base
+element for the queries as well as what is printed when you use `debug()`.
+
+
+
In the example above, the `render` method returns an object that has a few
properties:
@@ -164,11 +206,13 @@ The containing DOM node of your rendered React Element (rendered using
`ReactDOM.render`). It's a `div`. This is a regular DOM node, so you can call
`container.querySelector` etc. to inspect the children.
-> Tip: To get the root element of your rendered element, use `container.firstChild`.
+> Tip: To get the root element of your rendered element, use
+> `container.firstChild`.
>
> NOTE: When that root element is a
-> [React Fragment](https://reactjs.org/docs/fragments.html), `container.firstChild`
-> will only get the first child of that Fragment, not the Fragment itself.
+> [React Fragment](https://reactjs.org/docs/fragments.html),
+> `container.firstChild` will only get the first child of that Fragment, not the
+> Fragment itself.
#### `debug`
@@ -183,9 +227,11 @@ debug()
//
//
Hello World
//
+// you can also pass an element: debug(getByTestId('messages'))
```
-Learn more about [`prettyDOM`](#prettydom) below.
+This is a simple wrapper around `prettyDOM` which is also exposed and comes from
+[`dom-testing-library`](https://github.com/kentcdodds/dom-testing-library/blob/master/README.md#prettydom).
#### `rerender`
@@ -264,8 +310,8 @@ const inputNode = getByLabelText('username', {selector: 'input'})
#### `getByPlaceholderText(text: TextMatch): HTMLElement`
-This will search for all elements with a placeholder attribute and find one
-that matches the given [`TextMatch`](#textmatch).
+This will search for all elements with a placeholder attribute and find one that
+matches the given [`TextMatch`](#textmatch).
```javascript
import {render} from 'react-testing-library'
@@ -296,7 +342,9 @@ text. Note that it only supports elements which accept an `alt` attribute:
[``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img),
[``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input),
and [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
-(intentionally excluding [`