Skip to content

Commit

Permalink
Merge pull request react-bootstrap#1188 from AlexKVal/deprecated-wrapper
Browse files Browse the repository at this point in the history
Add 'deprecated' wrapper for property deprecation warning
  • Loading branch information
taion committed Aug 20, 2015
2 parents b73a806 + 477b1ad commit 34784b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/utils/CustomPropTypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import React from 'react';
import warning from 'react/lib/warning';

const ANONYMOUS = '<<anonymous>>';

const CustomPropTypes = {

deprecated(propType, explanation){
return function(props, propName, componentName){
if (props[propName] != null) {
warning(false, `"${propName}" property of "${componentName}" has been deprecated.\n${explanation}`);
}

return propType(props, propName, componentName);
};
},

isRequiredForA11y(propType){
return function(props, propName, componentName){
if (props[propName] === null) {
Expand Down
20 changes: 20 additions & 0 deletions test/utils/CustomPropTypesSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import CustomPropTypes from '../../src/utils/CustomPropTypes';
import {shouldWarn} from '../helpers';

function isChainableAndUndefinedOK(validatorUnderTest) {
it('Should validate OK with undefined or null values', function() {
Expand Down Expand Up @@ -184,4 +185,23 @@ describe('CustomPropTypes', function() {
assert.include(err.message, 'accessible for users using assistive technologies such as screen readers');
});
});

describe('deprecated', function () {
function validate(prop) {
return CustomPropTypes.deprecated(React.PropTypes.string, 'Read more at link')({pName: prop}, 'pName', 'ComponentName');
}

it('Should warn about deprecation and validate OK', function() {
let err = validate('value');
shouldWarn('"pName" property of "ComponentName" has been deprecated.\nRead more at link');
assert.notInstanceOf(err, Error);
});

it('Should warn about deprecation and throw validation error when property value is not OK', function() {
let err = validate({});
shouldWarn('"pName" property of "ComponentName" has been deprecated.\nRead more at link');
assert.instanceOf(err, Error);
assert.include(err.message, 'Invalid undefined `pName` of type `object` supplied to `ComponentName`');
});
});
});

0 comments on commit 34784b4

Please sign in to comment.