Skip to content

Commit 8e99f3c

Browse files
eps1lonKent C. Dodds
authored and
Kent C. Dodds
committed
fix: Specify baseElement without container (#394)
* Add expected behavior of base element * Fix expected behavior of baseElement * Improve tests
1 parent 3076b11 commit 8e99f3c

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/__tests__/multi-base.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react'
2+
import {render, cleanup} from '../'
3+
4+
// these are created once per test suite and reused for each case
5+
let treeA, treeB
6+
beforeAll(() => {
7+
treeA = document.createElement('div')
8+
treeB = document.createElement('div')
9+
document.body.appendChild(treeA)
10+
document.body.appendChild(treeB)
11+
})
12+
13+
afterAll(() => {
14+
treeA.parentNode.removeChild(treeA)
15+
treeB.parentNode.removeChild(treeB)
16+
})
17+
18+
afterEach(cleanup)
19+
20+
test('baseElement isolates trees from one another', () => {
21+
const {getByText: getByTextInA} = render(<div>Jekyll</div>, {
22+
baseElement: treeA,
23+
})
24+
const {getByText: getByTextInB} = render(<div>Hyde</div>, {
25+
baseElement: treeB,
26+
})
27+
28+
expect(() => getByTextInA('Jekyll')).not.toThrow(
29+
'Unable to find an element with the text: Jekyll.',
30+
)
31+
expect(() => getByTextInB('Jekyll')).toThrow(
32+
'Unable to find an element with the text: Jekyll.',
33+
)
34+
35+
expect(() => getByTextInA('Hyde')).toThrow(
36+
'Unable to find an element with the text: Hyde.',
37+
)
38+
expect(() => getByTextInB('Hyde')).not.toThrow(
39+
'Unable to find an element with the text: Hyde.',
40+
)
41+
})

src/__tests__/render.js

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import React from 'react'
33
import ReactDOM from 'react-dom'
44
import {render, cleanup} from '../'
55

6-
afterEach(cleanup)
7-
86
test('renders div into document', () => {
97
const ref = React.createRef()
108
const {container} = render(<div ref={ref} />)

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ function render(
3030
wrapper: WrapperComponent,
3131
} = {},
3232
) {
33-
if (!container) {
33+
if (!baseElement) {
3434
// default to document.body instead of documentElement to avoid output of potentially-large
3535
// head elements (such as JSS style blocks) in debug output
3636
baseElement = document.body
37-
container = document.body.appendChild(document.createElement('div'))
37+
}
38+
if (!container) {
39+
container = baseElement.appendChild(document.createElement('div'))
3840
}
3941

4042
// we'll add it to the mounted containers regardless of whether it's actually

0 commit comments

Comments
 (0)