15
15
// that do use JSX syntax. We should port them to React.createElement, and also
16
16
// confirm there's a corresponding test that uses JSX syntax.
17
17
18
- let PropTypes ;
19
18
let React ;
20
19
let ReactDOMClient ;
21
20
let act ;
@@ -28,7 +27,6 @@ describe('ReactElementValidator', () => {
28
27
beforeEach ( ( ) => {
29
28
jest . resetModules ( ) ;
30
29
31
- PropTypes = require ( 'prop-types' ) ;
32
30
ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
33
31
ReactFeatureFlags . replayFailedUnitOfWorkWithInvokeGuardedCallback = false ;
34
32
React = require ( 'react' ) ;
@@ -221,26 +219,19 @@ describe('ReactElementValidator', () => {
221
219
React . createElement ( ComponentClass , null , [ { } , { } ] ) ;
222
220
} ) ;
223
221
224
- it ( 'should give context for PropType errors in nested components.' , async ( ) => {
225
- // In this test, we're making sure that if a proptype error is found in a
226
- // component, we give a small hint as to which parent instantiated that
227
- // component as per warnings about key usage in ReactElementValidator.
228
- function MyComp ( props ) {
229
- return React . createElement ( 'div' , null , 'My color is ' + props . color ) ;
222
+ it ( 'should give context for errors in nested components.' , async ( ) => {
223
+ function MyComp ( ) {
224
+ return [ React . createElement ( 'div' ) ] ;
230
225
}
231
- MyComp . propTypes = {
232
- color : PropTypes . string ,
233
- } ;
234
226
function ParentComp ( ) {
235
- return React . createElement ( MyComp , { color : 123 } ) ;
227
+ return React . createElement ( MyComp ) ;
236
228
}
237
229
await expect ( async ( ) => {
238
230
const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
239
231
await act ( ( ) => root . render ( React . createElement ( ParentComp ) ) ) ;
240
232
} ) . toErrorDev (
241
- 'Warning: Failed prop type: ' +
242
- 'Invalid prop `color` of type `number` supplied to `MyComp`, ' +
243
- 'expected `string`.\n' +
233
+ 'Each child in a list should have a unique "key" prop. ' +
234
+ 'See https://reactjs.org/link/warning-keys for more information.\n' +
244
235
' in MyComp (at **)\n' +
245
236
' in ParentComp (at **)' ,
246
237
) ;
@@ -328,125 +319,6 @@ describe('ReactElementValidator', () => {
328
319
] ) ;
329
320
} ) ;
330
321
331
- it ( 'should check default prop values' , async ( ) => {
332
- class Component extends React . Component {
333
- static propTypes = { prop : PropTypes . string . isRequired } ;
334
- static defaultProps = { prop : null } ;
335
- render ( ) {
336
- return React . createElement ( 'span' , null , this . props . prop ) ;
337
- }
338
- }
339
-
340
- await expect ( async ( ) => {
341
- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
342
- await act ( ( ) => root . render ( React . createElement ( Component ) ) ) ;
343
- } ) . toErrorDev (
344
- 'Warning: Failed prop type: The prop `prop` is marked as required in ' +
345
- '`Component`, but its value is `null`.\n' +
346
- ' in Component' ,
347
- ) ;
348
- } ) ;
349
-
350
- it ( 'should not check the default for explicit null' , async ( ) => {
351
- class Component extends React . Component {
352
- static propTypes = { prop : PropTypes . string . isRequired } ;
353
- static defaultProps = { prop : 'text' } ;
354
- render ( ) {
355
- return React . createElement ( 'span' , null , this . props . prop ) ;
356
- }
357
- }
358
-
359
- await expect ( async ( ) => {
360
- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
361
- await act ( ( ) =>
362
- root . render ( React . createElement ( Component , { prop : null } ) ) ,
363
- ) ;
364
- } ) . toErrorDev (
365
- 'Warning: Failed prop type: The prop `prop` is marked as required in ' +
366
- '`Component`, but its value is `null`.\n' +
367
- ' in Component' ,
368
- ) ;
369
- } ) ;
370
-
371
- it ( 'should check declared prop types' , async ( ) => {
372
- class Component extends React . Component {
373
- static propTypes = {
374
- prop : PropTypes . string . isRequired ,
375
- } ;
376
- render ( ) {
377
- return React . createElement ( 'span' , null , this . props . prop ) ;
378
- }
379
- }
380
-
381
- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
382
- await expect ( async ( ) => {
383
- await act ( ( ) => root . render ( React . createElement ( Component ) ) ) ;
384
- await act ( ( ) => root . render ( React . createElement ( Component , { prop : 42 } ) ) ) ;
385
- } ) . toErrorDev ( [
386
- 'Warning: Failed prop type: ' +
387
- 'The prop `prop` is marked as required in `Component`, but its value ' +
388
- 'is `undefined`.\n' +
389
- ' in Component' ,
390
- 'Warning: Failed prop type: ' +
391
- 'Invalid prop `prop` of type `number` supplied to ' +
392
- '`Component`, expected `string`.\n' +
393
- ' in Component' ,
394
- ] ) ;
395
-
396
- // Should not error for strings
397
- await act ( ( ) =>
398
- root . render ( React . createElement ( Component , { prop : 'string' } ) ) ,
399
- ) ;
400
- } ) ;
401
-
402
- it ( 'should warn if a PropType creator is used as a PropType' , async ( ) => {
403
- class Component extends React . Component {
404
- static propTypes = {
405
- myProp : PropTypes . shape ,
406
- } ;
407
- render ( ) {
408
- return React . createElement ( 'span' , null , this . props . myProp . value ) ;
409
- }
410
- }
411
-
412
- await expect ( async ( ) => {
413
- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
414
- await act ( ( ) =>
415
- root . render ( React . createElement ( Component , { myProp : { value : 'hi' } } ) ) ,
416
- ) ;
417
- } ) . toErrorDev (
418
- 'Warning: Component: type specification of prop `myProp` is invalid; ' +
419
- 'the type checker function must return `null` or an `Error` but ' +
420
- 'returned a function. You may have forgotten to pass an argument to ' +
421
- 'the type checker creator (arrayOf, instanceOf, objectOf, oneOf, ' +
422
- 'oneOfType, and shape all require an argument).' ,
423
- ) ;
424
- } ) ;
425
-
426
- it ( 'should warn if component declares PropTypes instead of propTypes' , async ( ) => {
427
- class MisspelledPropTypesComponent extends React . Component {
428
- static PropTypes = {
429
- prop : PropTypes . string ,
430
- } ;
431
- render ( ) {
432
- return React . createElement ( 'span' , null , this . props . prop ) ;
433
- }
434
- }
435
-
436
- await expect ( async ( ) => {
437
- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
438
- await act ( ( ) =>
439
- root . render (
440
- React . createElement ( MisspelledPropTypesComponent , { prop : 'Hi' } ) ,
441
- ) ,
442
- ) ;
443
- } ) . toErrorDev (
444
- 'Warning: Component MisspelledPropTypesComponent declared `PropTypes` ' +
445
- 'instead of `propTypes`. Did you misspell the property assignment?' ,
446
- { withoutStack : true } ,
447
- ) ;
448
- } ) ;
449
-
450
322
it ( 'warns for fragments with illegal attributes' , async ( ) => {
451
323
class Foo extends React . Component {
452
324
render ( ) {
0 commit comments