11
11
12
12
describe ( 'ReactDOMOption' , ( ) => {
13
13
let React ;
14
- let ReactDOM ;
14
+ let ReactDOMClient ;
15
15
let ReactDOMServer ;
16
16
let ReactTestUtils ;
17
+ let act ;
17
18
18
19
beforeEach ( ( ) => {
19
20
jest . resetModules ( ) ;
20
21
React = require ( 'react' ) ;
21
- ReactDOM = require ( 'react-dom' ) ;
22
+ ReactDOMClient = require ( 'react-dom/client ' ) ;
22
23
ReactDOMServer = require ( 'react-dom/server' ) ;
23
24
ReactTestUtils = require ( 'react-dom/test-utils' ) ;
25
+ act = require ( 'internal-test-utils' ) . act ;
24
26
} ) ;
25
27
26
28
it ( 'should flatten children to a string' , ( ) => {
@@ -182,19 +184,28 @@ describe('ReactDOMOption', () => {
182
184
expect ( node . innerHTML ) . toBe ( 'foobar' ) ;
183
185
} ) ;
184
186
185
- it ( 'should set attribute for empty value' , ( ) => {
187
+ it ( 'should set attribute for empty value' , async ( ) => {
186
188
const container = document . createElement ( 'div' ) ;
187
- const option = ReactDOM . render ( < option value = "" /> , container ) ;
189
+ const root = ReactDOMClient . createRoot ( container ) ;
190
+ let option ;
191
+ await act ( ( ) => {
192
+ root . render ( < option value = "" /> ) ;
193
+ } ) ;
194
+ option = container . firstChild ;
188
195
expect ( option . hasAttribute ( 'value' ) ) . toBe ( true ) ;
189
196
expect ( option . getAttribute ( 'value' ) ) . toBe ( '' ) ;
190
197
191
- ReactDOM . render ( < option value = "lava" /> , container ) ;
198
+ await act ( ( ) => {
199
+ root . render ( < option value = "lava" /> ) ;
200
+ } ) ;
201
+ option = container . firstChild ;
192
202
expect ( option . hasAttribute ( 'value' ) ) . toBe ( true ) ;
193
203
expect ( option . getAttribute ( 'value' ) ) . toBe ( 'lava' ) ;
194
204
} ) ;
195
205
196
- it ( 'should allow ignoring `value` on option' , ( ) => {
206
+ it ( 'should allow ignoring `value` on option' , async ( ) => {
197
207
const a = 'a' ;
208
+ let node ;
198
209
const stub = (
199
210
< select value = "giraffe" onChange = { ( ) => { } } >
200
211
< option > monkey</ option >
@@ -204,15 +215,22 @@ describe('ReactDOMOption', () => {
204
215
) ;
205
216
const options = stub . props . children ;
206
217
const container = document . createElement ( 'div' ) ;
207
- const node = ReactDOM . render ( stub , container ) ;
218
+ const root = ReactDOMClient . createRoot ( container ) ;
219
+ await act ( ( ) => {
220
+ root . render ( stub ) ;
221
+ } ) ;
222
+ node = container . firstChild ;
208
223
209
224
expect ( node . selectedIndex ) . toBe ( 1 ) ;
210
225
211
- ReactDOM . render ( < select value = "gorilla" > { options } </ select > , container ) ;
226
+ await act ( ( ) => {
227
+ root . render ( < select value = "gorilla" > { options } </ select > ) ;
228
+ } ) ;
229
+ node = container . firstChild ;
212
230
expect ( node . selectedIndex ) . toEqual ( 2 ) ;
213
231
} ) ;
214
232
215
- it ( 'generates a warning and hydration error when an invalid nested tag is used as a child' , ( ) => {
233
+ it ( 'generates a warning and hydration error when an invalid nested tag is used as a child' , async ( ) => {
216
234
const ref = React . createRef ( ) ;
217
235
const children = (
218
236
< select readOnly = { true } value = "bar" >
@@ -229,16 +247,27 @@ describe('ReactDOMOption', () => {
229
247
expect ( container . firstChild . getAttribute ( 'value' ) ) . toBe ( null ) ;
230
248
expect ( container . firstChild . getAttribute ( 'defaultValue' ) ) . toBe ( null ) ;
231
249
232
- const option = container . firstChild . firstChild ;
250
+ let option = container . firstChild . firstChild ;
233
251
expect ( option . nodeName ) . toBe ( 'OPTION' ) ;
234
252
235
253
expect ( option . textContent ) . toBe ( 'BarFooBaz' ) ;
236
254
expect ( option . selected ) . toBe ( true ) ;
237
255
238
- expect ( ( ) => ReactDOM . hydrate ( children , container ) ) . toErrorDev ( [
239
- 'Text content did not match. Server: "FooBaz" Client: "Foo"' ,
240
- 'validateDOMNesting(...): <div> cannot appear as a child of <option>.' ,
241
- ] ) ;
256
+ await expect ( async ( ) => {
257
+ await act ( async ( ) => {
258
+ ReactDOMClient . hydrateRoot ( container , children , {
259
+ onRecoverableError : ( ) => { } ,
260
+ } ) ;
261
+ } ) ;
262
+ } ) . toErrorDev (
263
+ [
264
+ 'Warning: Text content did not match. Server: "FooBaz" Client: "Foo"' ,
265
+ 'Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>' ,
266
+ 'Warning: validateDOMNesting(...): <div> cannot appear as a child of <option>' ,
267
+ ] ,
268
+ { withoutStack : 1 } ,
269
+ ) ;
270
+ option = container . firstChild . firstChild ;
242
271
243
272
expect ( option . textContent ) . toBe ( 'BarFooBaz' ) ;
244
273
expect ( option . selected ) . toBe ( true ) ;
0 commit comments