11
11
12
12
const React = require ( 'react' ) ;
13
13
const ReactDOM = require ( 'react-dom' ) ;
14
+ const ReactDOMClient = require ( 'react-dom/client' ) ;
15
+ const act = require ( 'internal-test-utils' ) . act ;
14
16
15
17
describe ( 'ReactMount' , ( ) => {
16
- it ( 'should destroy a react root upon request' , ( ) => {
18
+ it ( 'should destroy a react root upon request' , async ( ) => {
17
19
const mainContainerDiv = document . createElement ( 'div' ) ;
18
20
document . body . appendChild ( mainContainerDiv ) ;
19
21
20
22
const instanceOne = < div className = "firstReactDiv" /> ;
21
23
const firstRootDiv = document . createElement ( 'div' ) ;
22
24
mainContainerDiv . appendChild ( firstRootDiv ) ;
23
- ReactDOM . render ( instanceOne , firstRootDiv ) ;
25
+ const firstRoot = ReactDOMClient . createRoot ( firstRootDiv ) ;
26
+ await act ( ( ) => {
27
+ firstRoot . render ( instanceOne ) ;
28
+ } ) ;
24
29
25
30
const instanceTwo = < div className = "secondReactDiv" /> ;
26
31
const secondRootDiv = document . createElement ( 'div' ) ;
27
32
mainContainerDiv . appendChild ( secondRootDiv ) ;
28
- ReactDOM . render ( instanceTwo , secondRootDiv ) ;
33
+ const secondRoot = ReactDOMClient . createRoot ( secondRootDiv ) ;
34
+ await act ( ( ) => {
35
+ secondRoot . render ( instanceTwo ) ;
36
+ } ) ;
29
37
30
38
// Test that two react roots are rendered in isolation
31
39
expect ( firstRootDiv . firstChild . className ) . toBe ( 'firstReactDiv' ) ;
32
40
expect ( secondRootDiv . firstChild . className ) . toBe ( 'secondReactDiv' ) ;
33
41
34
42
// Test that after unmounting each, they are no longer in the document.
35
- ReactDOM . unmountComponentAtNode ( firstRootDiv ) ;
43
+ await act ( ( ) => {
44
+ firstRoot . unmount ( ) ;
45
+ } ) ;
36
46
expect ( firstRootDiv . firstChild ) . toBeNull ( ) ;
37
- ReactDOM . unmountComponentAtNode ( secondRootDiv ) ;
38
- expect ( secondRootDiv . firstChild ) . toBeNull ( ) ;
47
+ await act ( ( ) => {
48
+ secondRoot . unmount ( ) ;
49
+ } ) ;
39
50
} ) ;
40
51
41
52
it ( 'should warn when unmounting a non-container root node' , ( ) => {
@@ -46,6 +57,7 @@ describe('ReactMount', () => {
46
57
< div />
47
58
</ div >
48
59
) ;
60
+ // Cannot be migrated to createRoot until we remove unmountComponentAtNode i.e. remove this test.
49
61
ReactDOM . render ( component , mainContainerDiv ) ;
50
62
51
63
// Test that unmounting at a root node gives a helpful warning
@@ -69,6 +81,7 @@ describe('ReactMount', () => {
69
81
</ div >
70
82
</ div >
71
83
) ;
84
+ // Cannot be migrated to createRoot until we remove unmountComponentAtNode i.e. remove this test.
72
85
ReactDOM . render ( component , mainContainerDiv ) ;
73
86
74
87
// Test that unmounting at a non-root node gives a different warning
0 commit comments