From 5f68542529ad26dbf0f220e42695a6d8a4d72384 Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Mon, 18 Dec 2017 16:31:44 +0000 Subject: [PATCH] [fix] Touchable use with react-test-renderer The object returned by 'ReactDOM.findDOMNode' when rendered by 'react-test-renderer' doesn't match the DOM API for an element. Only attempt to bind the listener if 'addEventListener' is present on the object. Fix #720 --- src/components/Touchable/Touchable.js | 31 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/components/Touchable/Touchable.js b/src/components/Touchable/Touchable.js index cee3b6d4..71762726 100644 --- a/src/components/Touchable/Touchable.js +++ b/src/components/Touchable/Touchable.js @@ -316,27 +316,32 @@ const LONG_PRESS_ALLOWED_MOVEMENT = 10; * @lends Touchable.prototype */ const TouchableMixin = { + // HACK (part 1): basic support for touchable interactions using a keyboard componentDidMount: function() { this._touchableNode = findNodeHandle(this); - this._touchableBlurListener = e => { - if (this._isTouchableKeyboardActive) { - if ( - this.state.touchable.touchState && - this.state.touchable.touchState !== States.NOT_RESPONDER - ) { - this.touchableHandleResponderTerminate({ nativeEvent: e }); + if (this._touchableNode && this._touchableNode.addEventListener) { + this._touchableBlurListener = e => { + if (this._isTouchableKeyboardActive) { + if ( + this.state.touchable.touchState && + this.state.touchable.touchState !== States.NOT_RESPONDER + ) { + this.touchableHandleResponderTerminate({ nativeEvent: e }); + } + this._isTouchableKeyboardActive = false; } - this._isTouchableKeyboardActive = false; - } - }; - this._touchableNode.addEventListener('blur', this._touchableBlurListener); + }; + this._touchableNode.addEventListener('blur', this._touchableBlurListener); + } }, /** * Clear all timeouts on unmount */ componentWillUnmount: function() { - this._touchableNode.removeEventListener('blur', this._touchableBlurListener); + if (this._touchableNode && this._touchableNode.addEventListener) { + this._touchableNode.removeEventListener('blur', this._touchableBlurListener); + } this.touchableDelayTimeout && clearTimeout(this.touchableDelayTimeout); this.longPressDelayTimeout && clearTimeout(this.longPressDelayTimeout); this.pressOutDelayTimeout && clearTimeout(this.pressOutDelayTimeout); @@ -791,7 +796,7 @@ const TouchableMixin = { } }, - // HACK: basic support for touchable interactions using a keyboard (including + // HACK (part 2): basic support for touchable interactions using a keyboard (including // delays and longPress) touchableHandleKeyEvent: function(e: Event) { const ENTER = 13;