Skip to content

Commit

Permalink
Merge pull request #6605 from gaearon/fix-warning-condition
Browse files Browse the repository at this point in the history
Fix the warning condition in ReactDebugTool and ReactDOMDebugTool
  • Loading branch information
gaearon committed Apr 25, 2016
2 parents f4e608f + 1f97103 commit 7dbc95f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/isomorphic/ReactDebugTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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
Expand Down Expand Up @@ -70,6 +70,9 @@ var ReactDebugTool = {
onUnmountComponent(internalInstance) {
emitEvent('onUnmountComponent', internalInstance);
},
onTestEvent() {
emitEvent('onTestEvent');
},
};

ReactDebugTool.addDevtool(ReactInvalidSetStateWarningDevTool);
Expand Down
75 changes: 75 additions & 0 deletions src/isomorphic/__tests__/ReactDebugTool-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
5 changes: 4 additions & 1 deletion src/renderers/dom/shared/ReactDOMDebugTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +59,9 @@ var ReactDOMDebugTool = {
onDeleteValueForProperty(node, name) {
emitEvent('onDeleteValueForProperty', node, name);
},
onTestEvent() {
emitEvent('onTestEvent');
},
};

ReactDOMDebugTool.addDevtool(ReactDOMUnknownPropertyDevtool);
Expand Down
75 changes: 75 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 7dbc95f

Please sign in to comment.