@@ -2,24 +2,36 @@ import React from 'react';
2
2
import ReactTestUtils from 'react/lib/ReactTestUtils' ;
3
3
import CustomPropTypes from '../../src/utils/CustomPropTypes' ;
4
4
5
+ function isChainableAndUndefinedOK ( validatorUnderTest ) {
6
+ it ( 'Should validate OK with undefined or null values' , function ( ) {
7
+ assert . isUndefined ( validatorUnderTest ( { } , 'p' , 'Component' ) ) ;
8
+ assert . isUndefined ( validatorUnderTest ( { p : null } , 'p' , 'Component' ) ) ;
9
+ } ) ;
10
+
11
+ it ( 'Should be able to chain' , function ( ) {
12
+ let err = validatorUnderTest . isRequired ( { } , 'p' , 'Component' ) ;
13
+ assert . instanceOf ( err , Error ) ;
14
+ assert . include ( err . message , 'Required prop' ) ;
15
+ assert . include ( err . message , 'was not specified in' ) ;
16
+ } ) ;
17
+ }
18
+
5
19
describe ( 'CustomPropTypes' , function ( ) {
6
20
7
21
describe ( 'mountable' , function ( ) {
8
22
function validate ( prop ) {
9
23
return CustomPropTypes . mountable ( { p : prop } , 'p' , 'Component' ) ;
10
24
}
11
- function validateRequired ( prop ) {
12
- return CustomPropTypes . mountable . isRequired ( { p : prop } , 'p' , 'Component' ) ;
13
- }
25
+
26
+ isChainableAndUndefinedOK ( CustomPropTypes . mountable ) ;
14
27
15
28
it ( 'Should return error with non mountable values' , function ( ) {
16
- assert . instanceOf ( validateRequired ( ) , Error ) ;
17
- assert . instanceOf ( validateRequired ( null ) , Error ) ;
18
- assert . instanceOf ( validate ( { } ) , Error ) ;
29
+ let err = validate ( { } ) ;
30
+ assert . instanceOf ( err , Error ) ;
31
+ assert . include ( err . message , 'expected a DOM element or an object that has a `render` method' ) ;
19
32
} ) ;
33
+
20
34
it ( 'Should return undefined with mountable values' , function ( ) {
21
- assert . isUndefined ( validate ( ) ) ;
22
- assert . isUndefined ( validate ( null ) ) ;
23
35
assert . isUndefined ( validate ( document . createElement ( 'div' ) ) ) ;
24
36
assert . isUndefined ( validate ( document . body ) ) ;
25
37
assert . isUndefined ( validate ( ReactTestUtils . renderIntoDocument ( < div /> ) ) ) ;
@@ -31,10 +43,7 @@ describe('CustomPropTypes', function() {
31
43
return CustomPropTypes . elementType ( { p : prop } , 'p' , 'TestComponent' ) ;
32
44
}
33
45
34
- it ( 'Should validate OK with undifined or null values' , function ( ) {
35
- assert . isUndefined ( validate ( ) ) ;
36
- assert . isUndefined ( validate ( null ) ) ;
37
- } ) ;
46
+ isChainableAndUndefinedOK ( CustomPropTypes . elementType ) ;
38
47
39
48
it ( 'Should validate OK with elementType values' , function ( ) {
40
49
assert . isUndefined ( validate ( 'span' ) ) ;
@@ -59,17 +68,16 @@ describe('CustomPropTypes', function() {
59
68
function validate ( prop ) {
60
69
return CustomPropTypes . keyOf ( obj ) ( { p : prop } , 'p' , 'Component' ) ;
61
70
}
62
- function validateRequired ( prop ) {
63
- return CustomPropTypes . keyOf ( obj ) . isRequired ( { p : prop } , 'p' , 'Component' ) ;
64
- }
71
+
72
+ isChainableAndUndefinedOK ( CustomPropTypes . keyOf ( obj ) ) ;
65
73
66
74
it ( 'Should return error with non-key values' , function ( ) {
67
- assert . instanceOf ( validateRequired ( ) , Error ) ;
68
- assert . instanceOf ( validateRequired ( null ) , Error ) ;
69
- assert . instanceOf ( validate ( 'bar' ) , Error ) ;
75
+ let err = validate ( 'bar' ) ;
76
+ assert . instanceOf ( err , Error ) ;
77
+ assert . include ( err . message , 'expected one of ["foo"]' ) ;
70
78
} ) ;
71
- it ( 'Should return undefined with key values' , function ( ) {
72
- assert . isUndefined ( validate ( ) ) ;
79
+
80
+ it ( 'Should validate OK with key values' , function ( ) {
73
81
assert . isUndefined ( validate ( 'foo' ) ) ;
74
82
obj . bar = 2 ;
75
83
assert . isUndefined ( validate ( 'bar' ) ) ;
@@ -83,16 +91,16 @@ describe('CustomPropTypes', function() {
83
91
return CustomPropTypes . singlePropFrom ( propList ) ( testProps , 'value' , 'Component' ) ;
84
92
}
85
93
86
- it ( 'Should return undefined if only one listed prop in used' , function ( ) {
94
+ it ( 'Should validate OK if only one listed prop in used' , function ( ) {
87
95
const testProps = { value : 5 } ;
88
96
89
97
assert . isUndefined ( validate ( testProps ) ) ;
90
98
} ) ;
91
99
92
100
it ( 'Should return error if multiple of the listed properties have values' , function ( ) {
93
- const testProps = { value : 5 , children : 5 } ;
94
-
95
- validate ( testProps ) . should . be . instanceOf ( Error ) ;
101
+ let err = validate ( { value : 5 , children : 5 } ) ;
102
+ assert . instanceOf ( err , Error ) ;
103
+ assert . include ( err . message , 'only one of the following may be provided: value and children' ) ;
96
104
} ) ;
97
105
} ) ;
98
106
0 commit comments