@@ -406,6 +406,97 @@ describe('React Component Types', function() {
406
406
} ) ;
407
407
} ) ;
408
408
409
+ describe ( 'ObjectOf Type' , function ( ) {
410
+ it ( 'should support the objectOf propTypes' , function ( ) {
411
+ typeCheckPass ( PropTypes . objectOf ( PropTypes . number ) , { a : 1 , b : 2 , c : 3 } ) ;
412
+ typeCheckPass (
413
+ PropTypes . objectOf ( PropTypes . string ) ,
414
+ { a : 'a' , b : 'b' , c : 'c' }
415
+ ) ;
416
+ typeCheckPass (
417
+ PropTypes . objectOf ( PropTypes . oneOf ( [ 'a' , 'b' ] ) ) ,
418
+ { a : 'a' , b : 'b' }
419
+ ) ;
420
+ } ) ;
421
+
422
+ it ( 'should support objectOf with complex types' , function ( ) {
423
+ typeCheckPass (
424
+ PropTypes . objectOf ( PropTypes . shape ( { a : PropTypes . number . isRequired } ) ) ,
425
+ { a : { a : 1 } , b : { a : 2 } }
426
+ ) ;
427
+
428
+ function Thing ( ) { }
429
+ typeCheckPass (
430
+ PropTypes . objectOf ( PropTypes . instanceOf ( Thing ) ) ,
431
+ { a : new Thing ( ) , b : new Thing ( ) }
432
+ ) ;
433
+ } ) ;
434
+
435
+ it ( 'should warn with invalid items in the object' , function ( ) {
436
+ typeCheckFail (
437
+ PropTypes . objectOf ( PropTypes . number ) ,
438
+ { a : 1 , b : 2 , c : 'b' } ,
439
+ 'Invalid prop `c` of type `string` supplied to `testComponent`, ' +
440
+ 'expected `number`.'
441
+ ) ;
442
+ } ) ;
443
+
444
+ it ( 'should warn with invalid complex types' , function ( ) {
445
+ function Thing ( ) { }
446
+ var name = Thing . name || '<<anonymous>>' ;
447
+
448
+ typeCheckFail (
449
+ PropTypes . objectOf ( PropTypes . instanceOf ( Thing ) ) ,
450
+ { a : new Thing ( ) , b : 'xyz' } ,
451
+ 'Invalid prop `b` supplied to `testComponent`, expected instance of `' +
452
+ name + '`.'
453
+ ) ;
454
+ } ) ;
455
+
456
+ it ( 'should warn when passed something other than an object' , function ( ) {
457
+ typeCheckFail (
458
+ PropTypes . objectOf ( PropTypes . number ) ,
459
+ [ 1 , 2 ] ,
460
+ 'Invalid prop `testProp` of type `array` supplied to `testComponent`, ' +
461
+ 'expected an object.'
462
+ ) ;
463
+ typeCheckFail (
464
+ PropTypes . objectOf ( PropTypes . number ) ,
465
+ 123 ,
466
+ 'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
467
+ 'expected an object.'
468
+ ) ;
469
+ typeCheckFail (
470
+ PropTypes . objectOf ( PropTypes . number ) ,
471
+ 'string' ,
472
+ 'Invalid prop `testProp` of type `string` supplied to `testComponent`, ' +
473
+ 'expected an object.'
474
+ ) ;
475
+ } ) ;
476
+
477
+ it ( 'should not warn when passing an empty object' , function ( ) {
478
+ typeCheckPass ( PropTypes . objectOf ( PropTypes . number ) , { } ) ;
479
+ } ) ;
480
+
481
+ it ( "should be implicitly optional and not warn without values" , function ( ) {
482
+ typeCheckPass ( PropTypes . objectOf ( PropTypes . number ) , null ) ;
483
+ typeCheckPass ( PropTypes . objectOf ( PropTypes . number ) , undefined ) ;
484
+ } ) ;
485
+
486
+ it ( "should warn for missing required values" , function ( ) {
487
+ typeCheckFail (
488
+ PropTypes . objectOf ( PropTypes . number ) . isRequired ,
489
+ null ,
490
+ requiredMessage
491
+ ) ;
492
+ typeCheckFail (
493
+ PropTypes . objectOf ( PropTypes . number ) . isRequired ,
494
+ undefined ,
495
+ requiredMessage
496
+ ) ;
497
+ } ) ;
498
+ } ) ;
499
+
409
500
describe ( 'OneOf Types' , function ( ) {
410
501
it ( "should warn for invalid strings" , function ( ) {
411
502
typeCheckFail (
@@ -446,10 +537,14 @@ describe('OneOf Types', function() {
446
537
447
538
it ( "should warn for missing required values" , function ( ) {
448
539
typeCheckFail (
449
- PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired , null , requiredMessage
540
+ PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired ,
541
+ null ,
542
+ requiredMessage
450
543
) ;
451
544
typeCheckFail (
452
- PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired , undefined , requiredMessage
545
+ PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired ,
546
+ undefined ,
547
+ requiredMessage
453
548
) ;
454
549
} ) ;
455
550
} ) ;
0 commit comments