From 477b1ad93ef1d50cd711e4d5a16fc91bbc669433 Mon Sep 17 00:00:00 2001 From: AlexKVal Date: Thu, 20 Aug 2015 17:11:12 +0300 Subject: [PATCH] Add 'deprecated' wrapper for property deprecation warning ```js propTypes: { collapsable: deprecated(React.PropTypes.bool, 'Use "collapsible" instead.') ``` --- src/utils/CustomPropTypes.js | 11 +++++++++++ test/utils/CustomPropTypesSpec.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/utils/CustomPropTypes.js b/src/utils/CustomPropTypes.js index cb57a653cb..808de84c2e 100644 --- a/src/utils/CustomPropTypes.js +++ b/src/utils/CustomPropTypes.js @@ -1,9 +1,20 @@ import React from 'react'; +import warning from 'react/lib/warning'; const 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) { diff --git a/test/utils/CustomPropTypesSpec.js b/test/utils/CustomPropTypesSpec.js index 4254569ea8..92e542b366 100644 --- a/test/utils/CustomPropTypesSpec.js +++ b/test/utils/CustomPropTypesSpec.js @@ -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() { @@ -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`'); + }); + }); });