Throw for Misue of LocalEventTrapMixin
Summary: If `getDOMNode()` returns null in `LocalEventTrapMixin`, `listener` will also be null and `accumulateInto` will throw. This changes `LocalEventTrapMixin` to throw a more helpful error message. Test Plan: Ran unit test successfully: ``` npm test LocalEventTrapMixin-test.js ```
yungsters committed
Jan 21, 2015 at 14:12 UTC
a7aca51f1ea901460ea5f8ab9401f4c8637c1947
2 files changed
+54
-1
src/browser/ui/dom/components/LocalEventTrapMixin.js
+8
-1
@@ -24,10 +24,17 @@ function remove(event) {
24
var LocalEventTrapMixin = {
25
trapBubbledEvent(topLevelType, handlerBaseName) {
26
invariant(this.isMounted(), 'Must be mounted to trap events');
27
+ // If a component renders to null or if another component fatals and causes
28
+ // the state of the tree to be corrupted, `node` here can be null.
29
+ var node = this.getDOMNode();
30
+ invariant(
31
+ node,
32
+ 'LocalEventTrapMixin.trapBubbledEvent(...): Requires node to be rendered.'
33
+ );
34
var listener = ReactBrowserEventEmitter.trapBubbledEvent(
35
topLevelType,
36
handlerBaseName,
30
- this.getDOMNode()
37
+ node
38
);
39
this._localEventListeners =
40
accumulateInto(this._localEventListeners, listener);
src/browser/ui/dom/components/__tests__/LocalEventTrapMixin-test.js
new
+46
@@ -0,0 +1,46 @@
1
+/**
2
+ * Copyright 2013-2014, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @emails react-core
10
+ */
11
+
12
+'use strict';
13
+
14
+describe('LocalEventTrapMixin', function() {
15
+ var EventConstants;
16
+ var LocalEventTrapMixin;
17
+ var React;
18
+ var ReactTestUtils;
19
+
20
+ beforeEach(function() {
21
+ EventConstants = require('EventConstants');
22
+ LocalEventTrapMixin = require('LocalEventTrapMixin');
23
+ React = require('React');
24
+ ReactTestUtils = require('ReactTestUtils');
25
+ });
26
+
27
+ it('does not fatal when trapping bubbled state on null', function() {
28
+ var BadImage = React.createClass({
29
+ mixins: [LocalEventTrapMixin],
30
+ render: function() {
31
+ return null;
32
+ },
33
+ componentDidMount: function() {
34
+ this.trapBubbledEvent(EventConstants.topLevelTypes.topLoad, 'load');
35
+ }
36
+ });
37
+
38
+ expect(function() {
39
+ ReactTestUtils.renderIntoDocument(<BadImage />);
40
+ }).toThrow(
41
+ 'Invariant Violation: ' +
42
+ 'LocalEventTrapMixin.trapBubbledEvent(...): ' +
43
+ 'Requires node to be rendered.'
44
+ );
45
+ });
46
+});