From 49383f384f6a1604e3959e3958b3c95be4a569fb Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 25 Apr 2016 20:16:52 +0100 Subject: [PATCH] Merge pull request #6605 from gaearon/fix-warning-condition Fix the warning condition in ReactDebugTool and ReactDOMDebugTool (cherry picked from commit 7dbc95f379780d0802b482abffcafac11dcd9d08) --- src/isomorphic/ReactDebugTool.js | 5 +- .../__tests__/ReactDebugTool-test.js | 75 +++++++++++++++++++ src/renderers/dom/shared/ReactDOMDebugTool.js | 5 +- .../__tests__/ReactDOMDebugTool-test.js | 75 +++++++++++++++++++ 4 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/isomorphic/__tests__/ReactDebugTool-test.js create mode 100644 src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js diff --git a/src/isomorphic/ReactDebugTool.js b/src/isomorphic/ReactDebugTool.js index 9c084ad52ff8e..f88513b387157 100644 --- a/src/isomorphic/ReactDebugTool.js +++ b/src/isomorphic/ReactDebugTool.js @@ -28,7 +28,7 @@ function emitEvent(handlerFunctionName, arg1, arg2, arg3, arg4, arg5) { } } catch (e) { warning( - !handlerDoesThrowForEvent[handlerFunctionName], + handlerDoesThrowForEvent[handlerFunctionName], 'exception thrown by devtool while handling %s: %s', handlerFunctionName, e.message @@ -253,6 +253,9 @@ var ReactDebugTool = { checkDebugID(debugID); emitEvent('onUnmountComponent', debugID); }, + onTestEvent() { + emitEvent('onTestEvent'); + }, }; if (__DEV__) { diff --git a/src/isomorphic/__tests__/ReactDebugTool-test.js b/src/isomorphic/__tests__/ReactDebugTool-test.js new file mode 100644 index 0000000000000..bae09516f5186 --- /dev/null +++ b/src/isomorphic/__tests__/ReactDebugTool-test.js @@ -0,0 +1,75 @@ +/** + * Copyright 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails react-core + */ + +'use strict'; + +describe('ReactDebugTool', function() { + var ReactDebugTool; + + beforeEach(function() { + jest.resetModuleRegistry(); + ReactDebugTool = require('ReactDebugTool'); + }); + + it('should add and remove devtools', () => { + var handler1 = jasmine.createSpy('spy'); + var handler2 = jasmine.createSpy('spy'); + var devtool1 = {onTestEvent: handler1}; + var devtool2 = {onTestEvent: handler2}; + + ReactDebugTool.addDevtool(devtool1); + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(1); + expect(handler2.calls.length).toBe(0); + + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(2); + expect(handler2.calls.length).toBe(0); + + ReactDebugTool.addDevtool(devtool2); + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(3); + expect(handler2.calls.length).toBe(1); + + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(2); + + ReactDebugTool.removeDevtool(devtool1); + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(3); + + ReactDebugTool.removeDevtool(devtool2); + ReactDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(3); + }); + + it('warns once when an error is thrown in devtool', () => { + spyOn(console, 'error'); + ReactDebugTool.addDevtool({ + onTestEvent() { + throw new Error('Hi.'); + }, + }); + + ReactDebugTool.onTestEvent(); + expect(console.error.calls.length).toBe(1); + expect(console.error.argsForCall[0][0]).toContain( + 'exception thrown by devtool while handling ' + + 'onTestEvent: Hi.' + ); + + ReactDebugTool.onTestEvent(); + expect(console.error.calls.length).toBe(1); + }); +}); diff --git a/src/renderers/dom/shared/ReactDOMDebugTool.js b/src/renderers/dom/shared/ReactDOMDebugTool.js index 9fcc3763fadb1..25d72c7ee005f 100644 --- a/src/renderers/dom/shared/ReactDOMDebugTool.js +++ b/src/renderers/dom/shared/ReactDOMDebugTool.js @@ -27,7 +27,7 @@ function emitEvent(handlerFunctionName, arg1, arg2, arg3, arg4, arg5) { } } catch (e) { warning( - !handlerDoesThrowForEvent[handlerFunctionName], + handlerDoesThrowForEvent[handlerFunctionName], 'exception thrown by devtool while handling %s: %s', handlerFunctionName, e.message @@ -59,6 +59,9 @@ var ReactDOMDebugTool = { onDeleteValueForProperty(node, name) { emitEvent('onDeleteValueForProperty', node, name); }, + onTestEvent() { + emitEvent('onTestEvent'); + }, }; ReactDOMDebugTool.addDevtool(ReactDOMUnknownPropertyDevtool); diff --git a/src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js b/src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js new file mode 100644 index 0000000000000..b4a94ec8375a4 --- /dev/null +++ b/src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js @@ -0,0 +1,75 @@ +/** + * Copyright 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails react-core + */ + +'use strict'; + +describe('ReactDOMDebugTool', function() { + var ReactDOMDebugTool; + + beforeEach(function() { + jest.resetModuleRegistry(); + ReactDOMDebugTool = require('ReactDOMDebugTool'); + }); + + it('should add and remove devtools', () => { + var handler1 = jasmine.createSpy('spy'); + var handler2 = jasmine.createSpy('spy'); + var devtool1 = {onTestEvent: handler1}; + var devtool2 = {onTestEvent: handler2}; + + ReactDOMDebugTool.addDevtool(devtool1); + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(1); + expect(handler2.calls.length).toBe(0); + + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(2); + expect(handler2.calls.length).toBe(0); + + ReactDOMDebugTool.addDevtool(devtool2); + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(3); + expect(handler2.calls.length).toBe(1); + + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(2); + + ReactDOMDebugTool.removeDevtool(devtool1); + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(3); + + ReactDOMDebugTool.removeDevtool(devtool2); + ReactDOMDebugTool.onTestEvent(); + expect(handler1.calls.length).toBe(4); + expect(handler2.calls.length).toBe(3); + }); + + it('warns once when an error is thrown in devtool', () => { + spyOn(console, 'error'); + ReactDOMDebugTool.addDevtool({ + onTestEvent() { + throw new Error('Hi.'); + }, + }); + + ReactDOMDebugTool.onTestEvent(); + expect(console.error.calls.length).toBe(1); + expect(console.error.argsForCall[0][0]).toContain( + 'exception thrown by devtool while handling ' + + 'onTestEvent: Hi.' + ); + + ReactDOMDebugTool.onTestEvent(); + expect(console.error.calls.length).toBe(1); + }); +});