Skip to content

Commit f57243d

Browse files
committed
Merge pull request react-bootstrap#891 from AlexKVal/customPropsSpec
Refactor CustomPropTypes tests.
2 parents 60ddc91 + 98e6d1f commit f57243d

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

Diff for: test/utils/CustomPropTypesSpec.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,36 @@ import React from 'react';
22
import ReactTestUtils from 'react/lib/ReactTestUtils';
33
import CustomPropTypes from '../../src/utils/CustomPropTypes';
44

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+
519
describe('CustomPropTypes', function() {
620

721
describe('mountable', function () {
822
function validate(prop) {
923
return CustomPropTypes.mountable({p: prop}, 'p', 'Component');
1024
}
11-
function validateRequired(prop) {
12-
return CustomPropTypes.mountable.isRequired({p: prop}, 'p', 'Component');
13-
}
25+
26+
isChainableAndUndefinedOK(CustomPropTypes.mountable);
1427

1528
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');
1932
});
33+
2034
it('Should return undefined with mountable values', function() {
21-
assert.isUndefined(validate());
22-
assert.isUndefined(validate(null));
2335
assert.isUndefined(validate(document.createElement('div')));
2436
assert.isUndefined(validate(document.body));
2537
assert.isUndefined(validate(ReactTestUtils.renderIntoDocument(<div />)));
@@ -31,10 +43,7 @@ describe('CustomPropTypes', function() {
3143
return CustomPropTypes.elementType({p: prop}, 'p', 'TestComponent');
3244
}
3345

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);
3847

3948
it('Should validate OK with elementType values', function() {
4049
assert.isUndefined(validate('span'));
@@ -59,17 +68,16 @@ describe('CustomPropTypes', function() {
5968
function validate(prop) {
6069
return CustomPropTypes.keyOf(obj)({p: prop}, 'p', 'Component');
6170
}
62-
function validateRequired(prop) {
63-
return CustomPropTypes.keyOf(obj).isRequired({p: prop}, 'p', 'Component');
64-
}
71+
72+
isChainableAndUndefinedOK(CustomPropTypes.keyOf(obj));
6573

6674
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"]');
7078
});
71-
it('Should return undefined with key values', function() {
72-
assert.isUndefined(validate());
79+
80+
it('Should validate OK with key values', function() {
7381
assert.isUndefined(validate('foo'));
7482
obj.bar = 2;
7583
assert.isUndefined(validate('bar'));
@@ -83,16 +91,16 @@ describe('CustomPropTypes', function() {
8391
return CustomPropTypes.singlePropFrom(propList)(testProps, 'value', 'Component');
8492
}
8593

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 () {
8795
const testProps = {value: 5};
8896

8997
assert.isUndefined(validate(testProps));
9098
});
9199

92100
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');
96104
});
97105
});
98106

0 commit comments

Comments
 (0)