File tree 3 files changed +45
-4
lines changed
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'
3
3
import ReactDOM from 'react-dom'
4
4
import { render , cleanup } from '../'
5
5
6
- afterEach ( cleanup )
7
-
8
6
test ( 'renders div into document' , ( ) => {
9
7
const ref = React . createRef ( )
10
8
const { container} = render ( < div ref = { ref } /> )
Original file line number Diff line number Diff line change @@ -30,11 +30,13 @@ function render(
30
30
wrapper : WrapperComponent ,
31
31
} = { } ,
32
32
) {
33
- if ( ! container ) {
33
+ if ( ! baseElement ) {
34
34
// default to document.body instead of documentElement to avoid output of potentially-large
35
35
// head elements (such as JSS style blocks) in debug output
36
36
baseElement = document . body
37
- container = document . body . appendChild ( document . createElement ( 'div' ) )
37
+ }
38
+ if ( ! container ) {
39
+ container = baseElement . appendChild ( document . createElement ( 'div' ) )
38
40
}
39
41
40
42
// 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