Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Emit each deprecation warning only once #24

Merged
merged 1 commit into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import warning from 'warning';

let warned = {};

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

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

function _resetWarned() {
warned = {};
}

deprecated._resetWarned = _resetWarned;
13 changes: 11 additions & 2 deletions test/deprecatedSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export function shouldError(about) {

describe('deprecated', function() {
beforeEach(function() {
deprecated._resetWarned();

// because 'warning' package uses console.error instead of console.warn
sinon.stub(console, 'error');
});
Expand All @@ -22,16 +24,23 @@ describe('deprecated', function() {
return deprecated(React.PropTypes.string, 'Read more at link')({pName: prop}, 'pName', 'ComponentName');
}

it('Should warn about deprecation and validate OK', function() {
it('should warn about deprecation and validate OK', function() {
const err = validate('value');
shouldError('"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() {
it('should warn about deprecation and throw validation error when property value is not OK', function() {
const err = validate({});
shouldError('"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`');
});

it('should not emit the same warning more than once', function() {
validate('value');
validate('value');
console.error.should.have.been.calledOnce;
shouldError('deprecated');
});
});