Skip to content

Commit cb14d2d

Browse files
ehteshamkafeelKent C. Dodds
authored and
Kent C. Dodds
committed
feat(render): return baseElement from render (#127)
* fix(render) return baseElement from render * Update README.md * tests: improve portals tests * Update render.js * Update render.js
1 parent 71517cf commit cb14d2d

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

.all-contributorsrc

+10
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@
360360
"contributions": [
361361
"doc"
362362
]
363+
},
364+
{
365+
"login": "ehteshamkafeel",
366+
"name": "Ehtesham Kafeel",
367+
"avatar_url": "https://avatars1.githubusercontent.com/u/1213123?v=4",
368+
"profile": "https://github.com/ehteshamkafeel",
369+
"contributions": [
370+
"code",
371+
"doc"
372+
]
363373
}
364374
]
365375
}

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
1616
[![MIT License][license-badge]][license]
1717

18-
[![All Contributors](https://img.shields.io/badge/all_contributors-36-orange.svg?style=flat-square)](#contributors)
18+
[![All Contributors](https://img.shields.io/badge/all_contributors-37-orange.svg?style=flat-square)](#contributors)
1919
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
2020
[![Join the community on Spectrum][spectrum-badge]][spectrum]
2121

@@ -215,6 +215,19 @@ The containing DOM node of your rendered React Element (rendered using
215215
> `container.firstChild` will only get the first child of that Fragment, not the
216216
> Fragment itself.
217217

218+
#### `baseElement`
219+
220+
The containing DOM node where your React Element is rendered in the container.
221+
If you don't specify the `baseElement` in the options of `render`, it will
222+
default to `documentElement`.
223+
224+
This is useful when the component you want to test renders something outside the
225+
container div, e.g. when you want to snapshot test your portal component which
226+
renders it's HTML directly in the body.
227+
228+
> Note: the queries returned by the `render` looks into baseElement, so you can
229+
> use queries to test your portal component without the baseElement.
230+
218231
#### `debug`
219232

220233
This method is a shortcut for `console.log(prettyDOM(container))`.
@@ -946,7 +959,7 @@ Thanks goes to these people ([emoji key][emojis]):
946959
| [<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 />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Tests") |
947960
| [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars2.githubusercontent.com/u/10080111?v=4" width="100px;"/><br /><sub><b>Ryan Vice</b></sub>](http://www.vicesoftware.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7942604?v=4" width="100px;"/><br /><sub><b>Ian Wilson</b></sub>](https://ianwilson.io)<br />[📝](#blog-iwilsonq "Blogposts") [✅](#tutorial-iwilsonq "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/1635491?v=4" width="100px;"/><br /><sub><b>Daniel</b></sub>](https://github.com/InExtremaRes)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [<img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;"/><br /><sub><b>Giorgio Polvara</b></sub>](https://twitter.com/Gpx)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [🤔](#ideas-Gpx "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars0.githubusercontent.com/u/8203211?v=4" width="100px;"/><br /><sub><b>Sam Horton</b></sub>](https://twitter.com/SavePointSam)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=SavePointSam "Documentation") [💡](#example-SavePointSam "Examples") [🤔](#ideas-SavePointSam "Ideas, Planning, & Feedback") |
948961
| [<img src="https://avatars2.githubusercontent.com/u/10452163?v=4" width="100px;"/><br /><sub><b>Richard Kotze (mobile)</b></sub>](http://www.richardkotze.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=rkotze "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/10819833?v=4" width="100px;"/><br /><sub><b>Brahian E. Soto Mercedes</b></sub>](https://github.com/sotobuild)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=sotobuild "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/7151559?v=4" width="100px;"/><br /><sub><b>Benoit de La Forest</b></sub>](https://github.com/bdelaforest)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=bdelaforest "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/6624197?v=4" width="100px;"/><br /><sub><b>Salah</b></sub>](https://github.com/thesalah)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thesalah "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=thesalah "Tests") | [<img src="https://avatars2.githubusercontent.com/u/370054?v=4" width="100px;"/><br /><sub><b>Adam Gordon</b></sub>](http://gordonizer.com)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Aicfantv "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=icfantv "Code") | [<img src="https://avatars2.githubusercontent.com/u/471278?v=4" width="100px;"/><br /><sub><b>Matija Marohnić</b></sub>](https://silvenon.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=silvenon "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba</b></sub>](https://github.com/Dajust)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=Dajust "Documentation") |
949-
| [<img src="https://avatars2.githubusercontent.com/u/5286559?v=4" width="100px;"/><br /><sub><b>Mark Pollmann</b></sub>](https://markpollmann.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=MarkPollmann "Documentation") |
962+
| [<img src="https://avatars2.githubusercontent.com/u/5286559?v=4" width="100px;"/><br /><sub><b>Mark Pollmann</b></sub>](https://markpollmann.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=MarkPollmann "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/1213123?v=4" width="100px;"/><br /><sub><b>Ehtesham Kafeel</b></sub>](https://github.com/ehteshamkafeel)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=ehteshamkafeel "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=ehteshamkafeel "Documentation") |
950963

951964
<!-- ALL-CONTRIBUTORS-LIST:END -->
952965

src/__tests__/render.js

+45-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,58 @@
1+
import 'jest-dom/extend-expect'
12
import React from 'react'
3+
import ReactDOM from 'react-dom'
24
import {render, cleanup} from '../'
35

46
afterEach(cleanup)
57

6-
it('renders button into document', () => {
8+
test('renders div into document', () => {
79
const ref = React.createRef()
810
const {container} = render(<div ref={ref} />)
911
expect(container.firstChild).toBe(ref.current)
1012
})
1113

12-
it('access portal elements inside body', () => {
13-
const {getByText} = render(<div />)
14-
const portalComponent = document.createElement('div')
15-
portalComponent.appendChild(document.createTextNode('Hello World'))
16-
document.body.appendChild(portalComponent)
17-
expect(getByText('Hello World')).not.toBeNull()
18-
document.body.removeChild(portalComponent)
14+
test('works great with react portals', () => {
15+
class MyPortal extends React.Component {
16+
constructor(...args) {
17+
super(...args)
18+
this.portalNode = document.createElement('div')
19+
this.portalNode.dataset.testid = 'my-portal'
20+
}
21+
componentDidMount() {
22+
document.body.appendChild(this.portalNode)
23+
}
24+
componentWillUnmount() {
25+
this.portalNode.parentNode.removeChild(this.portalNode)
26+
}
27+
render() {
28+
return ReactDOM.createPortal(
29+
<Greet greeting="Hello" subject="World" />,
30+
this.portalNode,
31+
)
32+
}
33+
}
34+
35+
function Greet({greeting, subject}) {
36+
return (
37+
<div>
38+
<strong>
39+
{greeting} {subject}
40+
</strong>
41+
</div>
42+
)
43+
}
44+
45+
const {unmount, getByTestId, getByText} = render(<MyPortal />)
46+
expect(getByText('Hello World')).toBeInTheDOM()
47+
const portalNode = getByTestId('my-portal')
48+
expect(document.body.contains(portalNode)).toBe(true)
49+
unmount()
50+
expect(document.body.contains(portalNode)).toBe(false)
51+
})
52+
53+
test('returns baseElement which defaults to document.documentElement', () => {
54+
const {baseElement} = render(<div />)
55+
expect(baseElement).toBe(document.documentElement)
1956
})
2057

2158
it('cleansup document', () => {

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function render(ui, {container, baseElement = container} = {}) {
1818
ReactDOM.render(ui, container)
1919
return {
2020
container,
21+
baseElement,
2122
// eslint-disable-next-line no-console
2223
debug: (el = baseElement) => console.log(prettyDOM(el)),
2324
unmount: () => ReactDOM.unmountComponentAtNode(container),

0 commit comments

Comments
 (0)