From 36b29e4f7885bd0ee0ebf323ab55173a2ef92669 Mon Sep 17 00:00:00 2001 From: Jimmy Jia Date: Mon, 10 Aug 2015 15:12:45 -0400 Subject: [PATCH] Only issue each deprecation warning once --- src/CollapsibleMixin.js | 7 +------ src/FadeMixin.js | 7 +------ src/utils/deprecationWarning.js | 8 ++++++++ test/utils/deprecationWarningSpec.js | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 test/utils/deprecationWarningSpec.js diff --git a/src/CollapsibleMixin.js b/src/CollapsibleMixin.js index c1abcbaada..65d0c720e7 100644 --- a/src/CollapsibleMixin.js +++ b/src/CollapsibleMixin.js @@ -2,8 +2,6 @@ import React from 'react'; import TransitionEvents from './utils/TransitionEvents'; import deprecationWarning from './utils/deprecationWarning'; -let warned = false; - const CollapsibleMixin = { propTypes: { @@ -25,10 +23,7 @@ const CollapsibleMixin = { }, componentWillMount(){ - if ( !warned ){ - deprecationWarning('CollapsibleMixin', 'Collapse Component'); - warned = true; - } + deprecationWarning('CollapsibleMixin', 'Collapse Component'); }, componentWillUpdate(nextProps, nextState){ diff --git a/src/FadeMixin.js b/src/FadeMixin.js index b4dfe666f2..c96e024a3a 100644 --- a/src/FadeMixin.js +++ b/src/FadeMixin.js @@ -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() { diff --git a/src/utils/deprecationWarning.js b/src/utils/deprecationWarning.js index e92b75837d..e4115967c9 100644 --- a/src/utils/deprecationWarning.js +++ b/src/utils/deprecationWarning.js @@ -1,6 +1,13 @@ 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) { @@ -8,4 +15,5 @@ export default function deprecationWarning(oldname, newname, link) { } warning(false, message); + warned[warnKey] = true; } diff --git a/test/utils/deprecationWarningSpec.js b/test/utils/deprecationWarningSpec.js new file mode 100644 index 0000000000..dcc8a704ee --- /dev/null +++ b/test/utils/deprecationWarningSpec.js @@ -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(); + }); +});