10
10
// sanity tests to make sure act() works without a mocked scheduler
11
11
12
12
let React ;
13
- let ReactDOM ;
13
+ let ReactDOMClient ;
14
14
let act ;
15
15
let container ;
16
16
let yields ;
17
+ let prevActGlobal ;
17
18
18
19
function clearLog ( ) {
19
20
try {
@@ -23,48 +24,43 @@ function clearLog() {
23
24
}
24
25
}
25
26
26
- function render ( el , dom ) {
27
- ReactDOM . render ( el , dom ) ;
28
- }
29
-
30
- function unmount ( dom ) {
31
- ReactDOM . unmountComponentAtNode ( dom ) ;
32
- }
33
-
34
27
beforeEach ( ( ) => {
28
+ prevActGlobal = global . IS_REACT_ACT_ENVIRONMENT ;
29
+ global . IS_REACT_ACT_ENVIRONMENT = true ;
35
30
jest . resetModules ( ) ;
36
31
jest . unmock ( 'scheduler' ) ;
37
32
yields = [ ] ;
38
33
React = require ( 'react' ) ;
39
- ReactDOM = require ( 'react-dom' ) ;
34
+ ReactDOMClient = require ( 'react-dom/client ' ) ;
40
35
act = React . unstable_act ;
41
36
container = document . createElement ( 'div' ) ;
42
37
document . body . appendChild ( container ) ;
43
38
} ) ;
44
39
45
40
afterEach ( ( ) => {
46
- unmount ( container ) ;
41
+ global . IS_REACT_ACT_ENVIRONMENT = prevActGlobal ;
47
42
document . body . removeChild ( container ) ;
48
43
} ) ;
49
44
50
45
// @gate __DEV__
51
- it ( 'can use act to flush effects' , ( ) => {
46
+ it ( 'can use act to flush effects' , async ( ) => {
52
47
function App ( ) {
53
48
React . useEffect ( ( ) => {
54
49
yields . push ( 100 ) ;
55
50
} ) ;
56
51
return null ;
57
52
}
58
53
59
- act ( ( ) => {
60
- render ( < App /> , container ) ;
54
+ const root = ReactDOMClient . createRoot ( container ) ;
55
+ await act ( ( ) => {
56
+ root . render ( < App /> ) ;
61
57
} ) ;
62
58
63
59
expect ( clearLog ( ) ) . toEqual ( [ 100 ] ) ;
64
60
} ) ;
65
61
66
62
// @gate __DEV__
67
- it ( 'flushes effects on every call' , ( ) => {
63
+ it ( 'flushes effects on every call' , async ( ) => {
68
64
function App ( ) {
69
65
const [ ctr , setCtr ] = React . useState ( 0 ) ;
70
66
React . useEffect ( ( ) => {
@@ -77,8 +73,9 @@ it('flushes effects on every call', () => {
77
73
) ;
78
74
}
79
75
80
- act ( ( ) => {
81
- render ( < App /> , container ) ;
76
+ const root = ReactDOMClient . createRoot ( container ) ;
77
+ await act ( ( ) => {
78
+ root . render ( < App /> ) ;
82
79
} ) ;
83
80
84
81
expect ( clearLog ( ) ) . toEqual ( [ 0 ] ) ;
@@ -103,7 +100,7 @@ it('flushes effects on every call', () => {
103
100
} ) ;
104
101
105
102
// @gate __DEV__
106
- it ( "should keep flushing effects until they're done" , ( ) => {
103
+ it ( "should keep flushing effects until they're done" , async ( ) => {
107
104
function App ( ) {
108
105
const [ ctr , setCtr ] = React . useState ( 0 ) ;
109
106
React . useEffect ( ( ) => {
@@ -114,25 +111,27 @@ it("should keep flushing effects until they're done", () => {
114
111
return ctr ;
115
112
}
116
113
117
- act ( ( ) => {
118
- render ( < App /> , container ) ;
114
+ const root = ReactDOMClient . createRoot ( container ) ;
115
+ await act ( ( ) => {
116
+ root . render ( < App /> ) ;
119
117
} ) ;
120
118
121
119
expect ( container . innerHTML ) . toEqual ( '5' ) ;
122
120
} ) ;
123
121
124
122
// @gate __DEV__
125
- it ( 'should flush effects only on exiting the outermost act' , ( ) => {
123
+ it ( 'should flush effects only on exiting the outermost act' , async ( ) => {
126
124
function App ( ) {
127
125
React . useEffect ( ( ) => {
128
126
yields . push ( 0 ) ;
129
127
} ) ;
130
128
return null ;
131
129
}
130
+ const root = ReactDOMClient . createRoot ( container ) ;
132
131
// let's nest a couple of act() calls
133
- act ( ( ) => {
134
- act ( ( ) => {
135
- render ( < App /> , container ) ;
132
+ await act ( async ( ) => {
133
+ await act ( ( ) => {
134
+ root . render ( < App /> ) ;
136
135
} ) ;
137
136
// the effect wouldn't have yielded yet because
138
137
// we're still inside an act() scope
@@ -150,7 +149,9 @@ it('can handle cascading promises', async () => {
150
149
const [ state , setState ] = React . useState ( 0 ) ;
151
150
async function ticker ( ) {
152
151
await null ;
153
- setState ( x => x + 1 ) ;
152
+ await act ( ( ) => {
153
+ setState ( x => x + 1 ) ;
154
+ } ) ;
154
155
}
155
156
React . useEffect ( ( ) => {
156
157
yields . push ( state ) ;
@@ -159,8 +160,9 @@ it('can handle cascading promises', async () => {
159
160
return state ;
160
161
}
161
162
162
- await act ( async ( ) => {
163
- render ( < App /> , container ) ;
163
+ const root = ReactDOMClient . createRoot ( container ) ;
164
+ await act ( ( ) => {
165
+ root . render ( < App /> ) ;
164
166
} ) ;
165
167
// all 5 ticks present and accounted for
166
168
expect ( clearLog ( ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
0 commit comments