Skip to content

fix: Specify baseElement without container #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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