@@ -26,12 +26,10 @@ describe('ReactScope', () => {
26
26
} ) ;
27
27
28
28
describe ( 'ReactDOM' , ( ) => {
29
- let ReactDOM ;
30
29
let ReactDOMClient ;
31
30
let container ;
32
31
33
32
beforeEach ( ( ) => {
34
- ReactDOM = require ( 'react-dom' ) ;
35
33
ReactDOMClient = require ( 'react-dom/client' ) ;
36
34
ReactDOMServer = require ( 'react-dom/server' ) ;
37
35
container = document . createElement ( 'div' ) ;
@@ -44,7 +42,7 @@ describe('ReactScope', () => {
44
42
} ) ;
45
43
46
44
// @gate www
47
- it ( 'DO_NOT_USE_queryAllNodes() works as intended' , ( ) => {
45
+ it ( 'DO_NOT_USE_queryAllNodes() works as intended' , async ( ) => {
48
46
const testScopeQuery = ( type , props ) => true ;
49
47
const TestScope = React . unstable_Scope ;
50
48
const scopeRef = React . createRef ( ) ;
@@ -68,18 +66,28 @@ describe('ReactScope', () => {
68
66
) ;
69
67
}
70
68
71
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
69
+ const root = ReactDOMClient . createRoot ( container ) ;
70
+ await act ( ( ) => {
71
+ root . render ( < Test toggle = { true } /> ) ;
72
+ } ) ;
73
+
72
74
let nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( testScopeQuery ) ;
73
75
expect ( nodes ) . toEqual ( [ divRef . current , spanRef . current , aRef . current ] ) ;
74
- ReactDOM . render ( < Test toggle = { false } /> , container ) ;
76
+ await act ( ( ) => {
77
+ root . render ( < Test toggle = { false } /> ) ;
78
+ } ) ;
79
+
75
80
nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( testScopeQuery ) ;
76
81
expect ( nodes ) . toEqual ( [ aRef . current , divRef . current , spanRef . current ] ) ;
77
- ReactDOM . render ( null , container ) ;
82
+ await act ( ( ) => {
83
+ root . render ( null ) ;
84
+ } ) ;
85
+
78
86
expect ( scopeRef . current ) . toBe ( null ) ;
79
87
} ) ;
80
88
81
89
// @gate www
82
- it ( 'DO_NOT_USE_queryAllNodes() provides the correct host instance' , ( ) => {
90
+ it ( 'DO_NOT_USE_queryAllNodes() provides the correct host instance' , async ( ) => {
83
91
const testScopeQuery = ( type , props ) => type === 'div' ;
84
92
const TestScope = React . unstable_Scope ;
85
93
const scopeRef = React . createRef ( ) ;
@@ -103,7 +111,11 @@ describe('ReactScope', () => {
103
111
) ;
104
112
}
105
113
106
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
114
+ const root = ReactDOMClient . createRoot ( container ) ;
115
+ await act ( ( ) => {
116
+ root . render ( < Test toggle = { true } /> ) ;
117
+ } ) ;
118
+
107
119
let nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( testScopeQuery ) ;
108
120
expect ( nodes ) . toEqual ( [ divRef . current ] ) ;
109
121
let filterQuery = ( type , props , instance ) =>
@@ -115,18 +127,24 @@ describe('ReactScope', () => {
115
127
testScopeQuery ( type , props ) ;
116
128
nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( filterQuery ) ;
117
129
expect ( nodes ) . toEqual ( [ divRef . current , spanRef . current , aRef . current ] ) ;
118
- ReactDOM . render ( < Test toggle = { false } /> , container ) ;
130
+ await act ( ( ) => {
131
+ root . render ( < Test toggle = { false } /> ) ;
132
+ } ) ;
133
+
119
134
filterQuery = ( type , props , instance ) =>
120
135
[ spanRef . current , aRef . current ] . includes ( instance ) ||
121
136
testScopeQuery ( type , props ) ;
122
137
nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( filterQuery ) ;
123
138
expect ( nodes ) . toEqual ( [ aRef . current , divRef . current , spanRef . current ] ) ;
124
- ReactDOM . render ( null , container ) ;
139
+ await act ( ( ) => {
140
+ root . render ( null ) ;
141
+ } ) ;
142
+
125
143
expect ( scopeRef . current ) . toBe ( null ) ;
126
144
} ) ;
127
145
128
146
// @gate www
129
- it ( 'DO_NOT_USE_queryFirstNode() works as intended' , ( ) => {
147
+ it ( 'DO_NOT_USE_queryFirstNode() works as intended' , async ( ) => {
130
148
const testScopeQuery = ( type , props ) => true ;
131
149
const TestScope = React . unstable_Scope ;
132
150
const scopeRef = React . createRef ( ) ;
@@ -150,18 +168,28 @@ describe('ReactScope', () => {
150
168
) ;
151
169
}
152
170
153
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
171
+ const root = ReactDOMClient . createRoot ( container ) ;
172
+ await act ( ( ) => {
173
+ root . render ( < Test toggle = { true } /> ) ;
174
+ } ) ;
175
+
154
176
let node = scopeRef . current . DO_NOT_USE_queryFirstNode ( testScopeQuery ) ;
155
177
expect ( node ) . toEqual ( divRef . current ) ;
156
- ReactDOM . render ( < Test toggle = { false } /> , container ) ;
178
+ await act ( ( ) => {
179
+ root . render ( < Test toggle = { false } /> ) ;
180
+ } ) ;
181
+
157
182
node = scopeRef . current . DO_NOT_USE_queryFirstNode ( testScopeQuery ) ;
158
183
expect ( node ) . toEqual ( aRef . current ) ;
159
- ReactDOM . render ( null , container ) ;
184
+ await act ( ( ) => {
185
+ root . render ( null ) ;
186
+ } ) ;
187
+
160
188
expect ( scopeRef . current ) . toBe ( null ) ;
161
189
} ) ;
162
190
163
191
// @gate www
164
- it ( 'containsNode() works as intended' , ( ) => {
192
+ it ( 'containsNode() works as intended' , async ( ) => {
165
193
const TestScope = React . unstable_Scope ;
166
194
const scopeRef = React . createRef ( ) ;
167
195
const divRef = React . createRef ( ) ;
@@ -194,24 +222,34 @@ describe('ReactScope', () => {
194
222
) ;
195
223
}
196
224
197
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
225
+ const root = ReactDOMClient . createRoot ( container ) ;
226
+ await act ( ( ) => {
227
+ root . render ( < Test toggle = { true } /> ) ;
228
+ } ) ;
229
+
198
230
expect ( scopeRef . current . containsNode ( divRef . current ) ) . toBe ( true ) ;
199
231
expect ( scopeRef . current . containsNode ( spanRef . current ) ) . toBe ( true ) ;
200
232
expect ( scopeRef . current . containsNode ( aRef . current ) ) . toBe ( true ) ;
201
233
expect ( scopeRef . current . containsNode ( outerSpan . current ) ) . toBe ( false ) ;
202
234
expect ( scopeRef . current . containsNode ( emRef . current ) ) . toBe ( false ) ;
203
- ReactDOM . render ( < Test toggle = { false } /> , container ) ;
235
+ await act ( ( ) => {
236
+ root . render ( < Test toggle = { false } /> ) ;
237
+ } ) ;
238
+
204
239
expect ( scopeRef . current . containsNode ( divRef . current ) ) . toBe ( true ) ;
205
240
expect ( scopeRef . current . containsNode ( spanRef . current ) ) . toBe ( true ) ;
206
241
expect ( scopeRef . current . containsNode ( aRef . current ) ) . toBe ( true ) ;
207
242
expect ( scopeRef . current . containsNode ( outerSpan . current ) ) . toBe ( false ) ;
208
243
expect ( scopeRef . current . containsNode ( emRef . current ) ) . toBe ( true ) ;
209
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
244
+ await act ( ( ) => {
245
+ root . render ( < Test toggle = { true } /> ) ;
246
+ } ) ;
247
+
210
248
expect ( scopeRef . current . containsNode ( emRef . current ) ) . toBe ( false ) ;
211
249
} ) ;
212
250
213
251
// @gate www
214
- it ( 'scopes support server-side rendering and hydration' , ( ) => {
252
+ it ( 'scopes support server-side rendering and hydration' , async ( ) => {
215
253
const TestScope = React . unstable_Scope ;
216
254
const scopeRef = React . createRef ( ) ;
217
255
const divRef = React . createRef ( ) ;
@@ -235,14 +273,16 @@ describe('ReactScope', () => {
235
273
'<div><div>DIV</div><span>SPAN</span><a>A</a><div>Outside content!</div></div>' ,
236
274
) ;
237
275
container . innerHTML = html ;
238
- ReactDOM . hydrate ( < Test /> , container ) ;
276
+ await act ( ( ) => {
277
+ ReactDOMClient . hydrateRoot ( container , < Test /> ) ;
278
+ } ) ;
239
279
const testScopeQuery = ( type , props ) => true ;
240
280
const nodes = scopeRef . current . DO_NOT_USE_queryAllNodes ( testScopeQuery ) ;
241
281
expect ( nodes ) . toEqual ( [ divRef . current , spanRef . current , aRef . current ] ) ;
242
282
} ) ;
243
283
244
284
// @gate www
245
- it ( 'getChildContextValues() works as intended' , ( ) => {
285
+ it ( 'getChildContextValues() works as intended' , async ( ) => {
246
286
const TestContext = React . createContext ( ) ;
247
287
const TestScope = React . unstable_Scope ;
248
288
const scopeRef = React . createRef ( ) ;
@@ -260,13 +300,23 @@ describe('ReactScope', () => {
260
300
) ;
261
301
}
262
302
263
- ReactDOM . render ( < Test toggle = { true } /> , container ) ;
303
+ const root = ReactDOMClient . createRoot ( container ) ;
304
+ await act ( ( ) => {
305
+ root . render ( < Test toggle = { true } /> ) ;
306
+ } ) ;
307
+
264
308
let nodes = scopeRef . current . getChildContextValues ( TestContext ) ;
265
309
expect ( nodes ) . toEqual ( [ 1 ] ) ;
266
- ReactDOM . render ( < Test toggle = { false } /> , container ) ;
310
+ await act ( ( ) => {
311
+ root . render ( < Test toggle = { false } /> ) ;
312
+ } ) ;
313
+
267
314
nodes = scopeRef . current . getChildContextValues ( TestContext ) ;
268
315
expect ( nodes ) . toEqual ( [ 1 , 2 ] ) ;
269
- ReactDOM . render ( null , container ) ;
316
+ await act ( ( ) => {
317
+ root . render ( null ) ;
318
+ } ) ;
319
+
270
320
expect ( scopeRef . current ) . toBe ( null ) ;
271
321
} ) ;
272
322
0 commit comments