Skip to content

Commit

Permalink
Merge pull request react-bootstrap#1127 from taion/deprecation-warnin…
Browse files Browse the repository at this point in the history
…g-once

Only issue each deprecation warning once
  • Loading branch information
jquense committed Aug 10, 2015
2 parents 7e8ed85 + 36b29e4 commit a5c2ac9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/CollapsibleMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';
import TransitionEvents from './utils/TransitionEvents';
import deprecationWarning from './utils/deprecationWarning';

let warned = false;

const CollapsibleMixin = {

propTypes: {
Expand All @@ -25,10 +23,7 @@ const CollapsibleMixin = {
},

componentWillMount(){
if ( !warned ){
deprecationWarning('CollapsibleMixin', 'Collapse Component');
warned = true;
}
deprecationWarning('CollapsibleMixin', 'Collapse Component');
},

componentWillUpdate(nextProps, nextState){
Expand Down
7 changes: 1 addition & 6 deletions src/FadeMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ function getElementsAndSelf (root, classes){
return els;
}

let warned = false;

export default {
componentWillMount(){
if ( !warned ){
deprecationWarning('FadeMixin', 'Fade Component');
warned = true;
}
deprecationWarning('FadeMixin', 'Fade Component');
},

_fadeIn() {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/deprecationWarning.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import warning from 'react/lib/warning';

const warned = {};

export default function deprecationWarning(oldname, newname, link) {
const warnKey = `${oldname}\n${newname}`;
if (warned[warnKey]) {
return;
}

let message = `${oldname} is deprecated. Use ${newname} instead.`;

if (link) {
message += `\nYou can read more about it at ${link}`;
}

warning(false, message);
warned[warnKey] = true;
}
16 changes: 16 additions & 0 deletions test/utils/deprecationWarningSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import deprecationWarning from '../../src/utils/deprecationWarning';

describe('deprecationWarning', function () {
it('warns exactly once', function () {
// console.warn has already been stubbed out by test setup.

deprecationWarning('foo', 'bar');
expect(console.warn).to.have.been.calledOnce;

deprecationWarning('foo', 'bar');
expect(console.warn).to.have.been.calledOnce;

// Reset the stub to avoid unhandled warnings.
console.warn.reset();
});
});

0 comments on commit a5c2ac9

Please sign in to comment.