Skip to content

Commit 263aabd

Browse files
johann-sonntagbauerKent C. Dodds
authored and
Kent C. Dodds
committed
feat(rerender): implement rerender (#65) (#70)
* feat(rerender): implement rerender (#65) * update readme * update examples * tests * typescript typings * Update index.js * Update rerender.js * Update rerender.js * Update README.md Closes #65
1 parent 1cb1774 commit 263aabd

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@
207207
"example",
208208
"ideas"
209209
]
210+
},
211+
{
212+
"login": "johann-sonntagbauer",
213+
"name": "Johann Hubert Sonntagbauer",
214+
"avatar_url": "https://avatars3.githubusercontent.com/u/1239401?v=4",
215+
"profile": "https://github.com/johann-sonntagbauer",
216+
"contributions": [
217+
"code",
218+
"doc",
219+
"test"
220+
]
210221
}
211222
]
212223
}

README.md

+22-31
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222
[![Join the community on Spectrum][spectrum-badge]][spectrum]
@@ -167,6 +167,24 @@ The containing DOM node of your rendered React Element (rendered using
167167

168168
> Tip: To get the root element of your rendered element, use `container.firstChild`.
169169
170+
#### `rerender`
171+
172+
It'd probably be better if you test the component that's doing the prop updating
173+
to ensure that the props are being updated correctly (see
174+
[the Guiding Principles section](#guiding-principles)). That said, if you'd
175+
prefer to update the props of a rendered component in your test, this function
176+
can be used to update props of the rendered component.
177+
178+
```javascript
179+
const {rerender} = render(<NumberDisplay number={1} />)
180+
181+
// re-render the same component with different props
182+
rerender(<NumberDisplay number={2} />)
183+
```
184+
185+
[Open the tests](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/update-props.js)
186+
for a full example of this.
187+
170188
#### `unmount`
171189

172190
This will cause the rendered component to be unmounted. This is useful for
@@ -274,7 +292,8 @@ const usernameInputElement = getByTestId('username-input')
274292
275293
### `renderIntoDocument`
276294

277-
Render into `document.body`. Should be used with [cleanup](#cleanup)
295+
Render into `document.body`. Should be used with [cleanup](#cleanup).
296+
`renderIntoDocument` will return the same object as [render](#render)
278297

279298
```javascript
280299
renderIntoDocument(<div>)
@@ -527,32 +546,6 @@ to just stick with `data-testid`s (which is not bad anyway).
527546

528547
<details>
529548

530-
<summary>How do I update the props of a rendered component?</summary>
531-
532-
It'd probably be better if you test the component that's doing the prop updating
533-
to ensure that the props are being updated correctly (see
534-
[the Guiding Principles section](#guiding-principles)). That said, if you'd
535-
prefer to update the props of a rendered component in your test, the easiest
536-
way to do that is:
537-
538-
```javascript
539-
const {container, getByTestId} = render(<NumberDisplay number={1} />)
540-
expect(getByTestId('number-display').textContent).toBe('1')
541-
542-
// re-render the same component with different props
543-
// but pass the same container in the options argument.
544-
// which will cause a re-render of the same instance (normal React behavior).
545-
render(<NumberDisplay number={2} />, {container})
546-
expect(getByTestId('number-display').textContent).toBe('2')
547-
```
548-
549-
[Open the tests](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/update-props.js)
550-
for a full example of this.
551-
552-
</details>
553-
554-
<details>
555-
556549
<summary>If I can't use shallow rendering, how do I mock out components in tests?</summary>
557550

558551
In general, you should avoid mocking out components (see
@@ -759,13 +752,11 @@ light-weight, simple, and understandable.
759752
Thanks goes to these people ([emoji key][emojis]):
760753

761754
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
762-
763755
<!-- prettier-ignore -->
764756
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
765757
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
766758
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Tests") |
767-
| [<img src="https://avatars1.githubusercontent.com/u/29602306?v=4" width="100px;"/><br /><sub><b>Michal Baranowski</b></sub>](https://twitter.com/baranovskim)<br />[📝](#blog-mbaranovski "Blogposts") [✅](#tutorial-mbaranovski "Tutorials") | [<img src="https://avatars3.githubusercontent.com/u/13985684?v=4" width="100px;"/><br /><sub><b>Arthur Puthin</b></sub>](https://github.com/aputhin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/21194045?v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/20430611?v=4" width="100px;"/><br /><sub><b>Thiago Galvani</b></sub>](http://ilegra.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/19828824?v=4" width="100px;"/><br /><sub><b>Christian</b></sub>](http://Chriswcs.github.io)<br />[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💬](#question-alexkrolick "Answering Questions") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [💡](#example-alexkrolick "Examples") [🤔](#ideas-alexkrolick "Ideas, Planning, & Feedback") |
768-
759+
| [<img src="https://avatars1.githubusercontent.com/u/29602306?v=4" width="100px;"/><br /><sub><b>Michal Baranowski</b></sub>](https://twitter.com/baranovskim)<br />[📝](#blog-mbaranovski "Blogposts") [✅](#tutorial-mbaranovski "Tutorials") | [<img src="https://avatars3.githubusercontent.com/u/13985684?v=4" width="100px;"/><br /><sub><b>Arthur Puthin</b></sub>](https://github.com/aputhin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/21194045?v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/20430611?v=4" width="100px;"/><br /><sub><b>Thiago Galvani</b></sub>](http://ilegra.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/19828824?v=4" width="100px;"/><br /><sub><b>Christian</b></sub>](http://Chriswcs.github.io)<br />[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💬](#question-alexkrolick "Answering Questions") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [💡](#example-alexkrolick "Examples") [🤔](#ideas-alexkrolick "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/1239401?v=4" width="100px;"/><br /><sub><b>Johann Hubert Sonntagbauer</b></sub>](https://github.com/johann-sonntagbauer)<br />[🎨](#design-johann-sonntagbauer "Design") |
769760
<!-- ALL-CONTRIBUTORS-LIST:END -->
770761

771762
This project follows the [all-contributors][all-contributors] specification.

examples/__tests__/update-props.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ class NumberDisplay extends React.Component {
2020
}
2121

2222
test('calling render with the same component on the same container does not remount', () => {
23-
const {container, getByTestId} = render(<NumberDisplay number={1} />)
23+
const {getByTestId, rerender} = render(<NumberDisplay number={1} />)
2424
expect(getByTestId('number-display').textContent).toBe('1')
2525

2626
// re-render the same component with different props
27-
// but pass the same container in the options argument.
28-
// which will cause a re-render of the same instance (normal React behavior).
29-
render(<NumberDisplay number={2} />, {container})
27+
rerender(<NumberDisplay number={2} />)
3028
expect(getByTestId('number-display').textContent).toBe('2')
3129

3230
expect(getByTestId('instance-id').textContent).toBe('1')

src/__tests__/rerender.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import {render} from '../'
3+
import 'jest-dom/extend-expect'
4+
5+
test('rerender will re-render the element', () => {
6+
const Greeting = props => <div>{props.message}</div>
7+
const {container, rerender} = render(<Greeting message="hi" />)
8+
expect(container.firstChild).toHaveTextContent('hi')
9+
rerender(<Greeting message="hey" />)
10+
expect(container.firstChild).toHaveTextContent('hey')
11+
})

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ function render(ui, {container = document.createElement('div')} = {}) {
77
return {
88
container,
99
unmount: () => ReactDOM.unmountComponentAtNode(container),
10+
rerender: rerenderUi => {
11+
render(rerenderUi, {container})
12+
// Intentionally do not return anything to avoid unnecessarily complicating the API.
13+
// folks can use all the same utilities we return in the first place that are bound to the container
14+
},
1015
...bindElementToQueries(container),
1116
}
1217
}

typings/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type TextMatch = string | RegExp | TextMatchFunction
55

66
interface RenderResult {
77
container: HTMLDivElement
8+
rerender: (ui: React.ReactElement<any>) => void
89
unmount: VoidFunction
910
queryByTestId: (id: TextMatch) => HTMLElement | null
1011
getByTestId: (id: TextMatch) => HTMLElement

0 commit comments

Comments
 (0)