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

Commit ac1f221

Browse files
committed
Merge pull request #24 from taion/deprecated-warn-once
Emit each deprecation warning only once
2 parents 05ff9ac + e11b3c2 commit ac1f221

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/deprecated.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import warning from 'warning';
22

3+
let warned = {};
4+
35
export default function deprecated(propType, explanation) {
46
return function validate(props, propName, componentName) {
57
if (props[propName] != null) {
6-
warning(false, `"${propName}" property of "${componentName}" has been deprecated.\n${explanation}`);
8+
const message = `"${propName}" property of "${componentName}" has been deprecated.\n${explanation}`;
9+
if (!warned[message]) {
10+
warning(false, message);
11+
warned[message] = true;
12+
}
713
}
814

915
return propType(props, propName, componentName);
1016
};
1117
}
18+
19+
function _resetWarned() {
20+
warned = {};
21+
}
22+
23+
deprecated._resetWarned = _resetWarned;

test/deprecatedSpec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export function shouldError(about) {
1010

1111
describe('deprecated', function() {
1212
beforeEach(function() {
13+
deprecated._resetWarned();
14+
1315
// because 'warning' package uses console.error instead of console.warn
1416
sinon.stub(console, 'error');
1517
});
@@ -22,16 +24,23 @@ describe('deprecated', function() {
2224
return deprecated(React.PropTypes.string, 'Read more at link')({pName: prop}, 'pName', 'ComponentName');
2325
}
2426

25-
it('Should warn about deprecation and validate OK', function() {
27+
it('should warn about deprecation and validate OK', function() {
2628
const err = validate('value');
2729
shouldError('"pName" property of "ComponentName" has been deprecated.\nRead more at link');
2830
assert.notInstanceOf(err, Error);
2931
});
3032

31-
it('Should warn about deprecation and throw validation error when property value is not OK', function() {
33+
it('should warn about deprecation and throw validation error when property value is not OK', function() {
3234
const err = validate({});
3335
shouldError('"pName" property of "ComponentName" has been deprecated.\nRead more at link');
3436
assert.instanceOf(err, Error);
3537
assert.include(err.message, 'Invalid undefined `pName` of type `object` supplied to `ComponentName`');
3638
});
39+
40+
it('should not emit the same warning more than once', function() {
41+
validate('value');
42+
validate('value');
43+
console.error.should.have.been.calledOnce;
44+
shouldError('deprecated');
45+
});
3746
});

0 commit comments

Comments
 (0)