@@ -20,8 +20,6 @@ var ReactPropTypesSecret;
20
20
21
21
var Component ;
22
22
var MyComponent ;
23
- var requiredMessage =
24
- 'Required prop `testProp` was not specified in `testComponent`.' ;
25
23
26
24
function typeCheckFail ( declaration , value , message ) {
27
25
var props = { testProp : value } ;
@@ -37,6 +35,46 @@ function typeCheckFail(declaration, value, message) {
37
35
expect ( error . message ) . toBe ( message ) ;
38
36
}
39
37
38
+ function typeCheckFailRequiredValues ( declaration ) {
39
+ var specifiedButIsNullMsg = 'The prop `testProp` is marked as required in ' +
40
+ '`testComponent`, but its value is `null`.' ;
41
+ var unspecifiedMsg = 'The prop `testProp` is marked as required in ' +
42
+ '`testComponent`, but its value is \`undefined\`.' ;
43
+ var props1 = { testProp : null } ;
44
+ var error1 = declaration (
45
+ props1 ,
46
+ 'testProp' ,
47
+ 'testComponent' ,
48
+ ReactPropTypeLocations . prop ,
49
+ null ,
50
+ ReactPropTypesSecret
51
+ ) ;
52
+ expect ( error1 instanceof Error ) . toBe ( true ) ;
53
+ expect ( error1 . message ) . toBe ( specifiedButIsNullMsg ) ;
54
+ var props2 = { testProp : undefined } ;
55
+ var error2 = declaration (
56
+ props2 ,
57
+ 'testProp' ,
58
+ 'testComponent' ,
59
+ ReactPropTypeLocations . prop ,
60
+ null ,
61
+ ReactPropTypesSecret
62
+ ) ;
63
+ expect ( error2 instanceof Error ) . toBe ( true ) ;
64
+ expect ( error2 . message ) . toBe ( unspecifiedMsg ) ;
65
+ var props3 = { } ;
66
+ var error3 = declaration (
67
+ props3 ,
68
+ 'testProp' ,
69
+ 'testComponent' ,
70
+ ReactPropTypeLocations . prop ,
71
+ null ,
72
+ ReactPropTypesSecret
73
+ ) ;
74
+ expect ( error3 instanceof Error ) . toBe ( true ) ;
75
+ expect ( error3 . message ) . toBe ( unspecifiedMsg ) ;
76
+ }
77
+
40
78
function typeCheckPass ( declaration , value ) {
41
79
var props = { testProp : value } ;
42
80
var error = declaration (
@@ -146,8 +184,7 @@ describe('ReactPropTypes', function() {
146
184
} ) ;
147
185
148
186
it ( 'should warn for missing required values' , function ( ) {
149
- typeCheckFail ( PropTypes . string . isRequired , null , requiredMessage ) ;
150
- typeCheckFail ( PropTypes . string . isRequired , undefined , requiredMessage ) ;
187
+ typeCheckFailRequiredValues ( PropTypes . string . isRequired ) ;
151
188
} ) ;
152
189
153
190
it ( 'should warn if called manually in development' , function ( ) {
@@ -213,8 +250,7 @@ describe('ReactPropTypes', function() {
213
250
} ) ;
214
251
215
252
it ( 'should warn for missing required values' , function ( ) {
216
- typeCheckFail ( PropTypes . any . isRequired , null , requiredMessage ) ;
217
- typeCheckFail ( PropTypes . any . isRequired , undefined , requiredMessage ) ;
253
+ typeCheckFailRequiredValues ( PropTypes . any . isRequired ) ;
218
254
} ) ;
219
255
220
256
it ( 'should warn if called manually in development' , function ( ) {
@@ -307,15 +343,8 @@ describe('ReactPropTypes', function() {
307
343
} ) ;
308
344
309
345
it ( 'should warn for missing required values' , function ( ) {
310
- typeCheckFail (
311
- PropTypes . arrayOf ( PropTypes . number ) . isRequired ,
312
- null ,
313
- requiredMessage
314
- ) ;
315
- typeCheckFail (
316
- PropTypes . arrayOf ( PropTypes . number ) . isRequired ,
317
- undefined ,
318
- requiredMessage
346
+ typeCheckFailRequiredValues (
347
+ PropTypes . arrayOf ( PropTypes . number ) . isRequired
319
348
) ;
320
349
} ) ;
321
350
@@ -406,8 +435,7 @@ describe('ReactPropTypes', function() {
406
435
} ) ;
407
436
408
437
it ( 'should warn for missing required values' , function ( ) {
409
- typeCheckFail ( PropTypes . element . isRequired , null , requiredMessage ) ;
410
- typeCheckFail ( PropTypes . element . isRequired , undefined , requiredMessage ) ;
438
+ typeCheckFailRequiredValues ( PropTypes . element . isRequired ) ;
411
439
} ) ;
412
440
413
441
it ( 'should warn if called manually in development' , function ( ) {
@@ -493,12 +521,7 @@ describe('ReactPropTypes', function() {
493
521
} ) ;
494
522
495
523
it ( 'should warn for missing required values' , function ( ) {
496
- typeCheckFail (
497
- PropTypes . instanceOf ( String ) . isRequired , null , requiredMessage
498
- ) ;
499
- typeCheckFail (
500
- PropTypes . instanceOf ( String ) . isRequired , undefined , requiredMessage
501
- ) ;
524
+ typeCheckFailRequiredValues ( PropTypes . instanceOf ( String ) . isRequired ) ;
502
525
} ) ;
503
526
504
527
it ( 'should warn if called manually in development' , function ( ) {
@@ -601,16 +624,7 @@ describe('ReactPropTypes', function() {
601
624
} ) ;
602
625
603
626
it ( 'should warn for missing required values' , function ( ) {
604
- typeCheckFail (
605
- PropTypes . node . isRequired ,
606
- null ,
607
- 'Required prop `testProp` was not specified in `testComponent`.'
608
- ) ;
609
- typeCheckFail (
610
- PropTypes . node . isRequired ,
611
- undefined ,
612
- 'Required prop `testProp` was not specified in `testComponent`.'
613
- ) ;
627
+ typeCheckFailRequiredValues ( PropTypes . node . isRequired ) ;
614
628
} ) ;
615
629
616
630
it ( 'should accept empty array for required props' , function ( ) {
@@ -724,15 +738,8 @@ describe('ReactPropTypes', function() {
724
738
} ) ;
725
739
726
740
it ( 'should warn for missing required values' , function ( ) {
727
- typeCheckFail (
728
- PropTypes . objectOf ( PropTypes . number ) . isRequired ,
729
- null ,
730
- requiredMessage
731
- ) ;
732
- typeCheckFail (
733
- PropTypes . objectOf ( PropTypes . number ) . isRequired ,
734
- undefined ,
735
- requiredMessage
741
+ typeCheckFailRequiredValues (
742
+ PropTypes . objectOf ( PropTypes . number ) . isRequired
736
743
) ;
737
744
} ) ;
738
745
@@ -804,16 +811,7 @@ describe('ReactPropTypes', function() {
804
811
} ) ;
805
812
806
813
it ( 'should warn for missing required values' , function ( ) {
807
- typeCheckFail (
808
- PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired ,
809
- null ,
810
- requiredMessage
811
- ) ;
812
- typeCheckFail (
813
- PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired ,
814
- undefined ,
815
- requiredMessage
816
- ) ;
814
+ typeCheckFailRequiredValues ( PropTypes . oneOf ( [ 'red' , 'blue' ] ) . isRequired ) ;
817
815
} ) ;
818
816
819
817
it ( 'should warn if called manually in development' , function ( ) {
@@ -882,15 +880,8 @@ describe('ReactPropTypes', function() {
882
880
} ) ;
883
881
884
882
it ( 'should warn for missing required values' , function ( ) {
885
- typeCheckFail (
886
- PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) . isRequired ,
887
- null ,
888
- requiredMessage
889
- ) ;
890
- typeCheckFail (
891
- PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) . isRequired ,
892
- undefined ,
893
- requiredMessage
883
+ typeCheckFailRequiredValues (
884
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) . isRequired
894
885
) ;
895
886
} ) ;
896
887
@@ -950,7 +941,8 @@ describe('ReactPropTypes', function() {
950
941
typeCheckFail (
951
942
PropTypes . shape ( { key : PropTypes . number . isRequired } ) ,
952
943
{ } ,
953
- 'Required prop `testProp.key` was not specified in `testComponent`.'
944
+ 'The prop `testProp.key` is marked as required in `testComponent`, ' +
945
+ 'but its value is `undefined`.'
954
946
) ;
955
947
} ) ;
956
948
@@ -961,7 +953,8 @@ describe('ReactPropTypes', function() {
961
953
secondKey : PropTypes . number . isRequired ,
962
954
} ) ,
963
955
{ } ,
964
- 'Required prop `testProp.key` was not specified in `testComponent`.'
956
+ 'The prop `testProp.key` is marked as required in `testComponent`, ' +
957
+ 'but its value is `undefined`.'
965
958
) ;
966
959
} ) ;
967
960
@@ -983,15 +976,8 @@ describe('ReactPropTypes', function() {
983
976
} ) ;
984
977
985
978
it ( 'should warn for missing required values' , function ( ) {
986
- typeCheckFail (
987
- PropTypes . shape ( { key : PropTypes . number } ) . isRequired ,
988
- null ,
989
- requiredMessage
990
- ) ;
991
- typeCheckFail (
992
- PropTypes . shape ( { key : PropTypes . number } ) . isRequired ,
993
- undefined ,
994
- requiredMessage
979
+ typeCheckFailRequiredValues (
980
+ PropTypes . shape ( { key : PropTypes . number } ) . isRequired
995
981
) ;
996
982
} ) ;
997
983
0 commit comments