File tree Expand file tree Collapse file tree 3 files changed +45
-4
lines changed Expand file tree Collapse file tree 3 files changed +45
-4
lines changed Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change @@ -3,8 +3,6 @@ import React from 'react'
33import ReactDOM from 'react-dom'
44import { render , cleanup } from '../'
55
6- afterEach ( cleanup )
7-
86test ( 'renders div into document' , ( ) => {
97 const ref = React . createRef ( )
108 const { container} = render ( < div ref = { ref } /> )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments