Skip to content

Commit

Permalink
fix: Specify baseElement without container (#394)
Browse files Browse the repository at this point in the history
* Add expected behavior of base element

* Fix expected behavior of baseElement

* Improve tests
  • Loading branch information
eps1lon authored and Kent C. Dodds committed Jun 24, 2019
1 parent 3076b11 commit 8e99f3c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/__tests__/multi-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'
import {render, cleanup} from '../'

// these are created once per test suite and reused for each case
let treeA, treeB
beforeAll(() => {
treeA = document.createElement('div')
treeB = document.createElement('div')
document.body.appendChild(treeA)
document.body.appendChild(treeB)
})

afterAll(() => {
treeA.parentNode.removeChild(treeA)
treeB.parentNode.removeChild(treeB)
})

afterEach(cleanup)

test('baseElement isolates trees from one another', () => {
const {getByText: getByTextInA} = render(<div>Jekyll</div>, {
baseElement: treeA,
})
const {getByText: getByTextInB} = render(<div>Hyde</div>, {
baseElement: treeB,
})

expect(() => getByTextInA('Jekyll')).not.toThrow(
'Unable to find an element with the text: Jekyll.',
)
expect(() => getByTextInB('Jekyll')).toThrow(
'Unable to find an element with the text: Jekyll.',
)

expect(() => getByTextInA('Hyde')).toThrow(
'Unable to find an element with the text: Hyde.',
)
expect(() => getByTextInB('Hyde')).not.toThrow(
'Unable to find an element with the text: Hyde.',
)
})
2 changes: 0 additions & 2 deletions src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import React from 'react'
import ReactDOM from 'react-dom'
import {render, cleanup} from '../'

afterEach(cleanup)

test('renders div into document', () => {
const ref = React.createRef()
const {container} = render(<div ref={ref} />)
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ function render(
wrapper: WrapperComponent,
} = {},
) {
if (!container) {
if (!baseElement) {
// default to document.body instead of documentElement to avoid output of potentially-large
// head elements (such as JSS style blocks) in debug output
baseElement = document.body
container = document.body.appendChild(document.createElement('div'))
}
if (!container) {
container = baseElement.appendChild(document.createElement('div'))
}

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

0 comments on commit 8e99f3c

Please sign in to comment.