From fe450e84ba0940421ffa6d3ec9ecc50a72494a01 Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Mon, 31 Oct 2016 10:30:11 -0500 Subject: [PATCH] Set _hostNode in mountComponent / receiveComponent _hostNode is required for findDOMNode to work. Initial mount works fine since the transaction instance is pulled from the pool with the test options, but for updates test options aren't available. Needs work. --- src/renderers/testing/ReactTestRenderer.js | 7 +++ .../__tests__/ReactTestRenderer-test.js | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/renderers/testing/ReactTestRenderer.js b/src/renderers/testing/ReactTestRenderer.js index 47184fea0c1b..635791008aa3 100644 --- a/src/renderers/testing/ReactTestRenderer.js +++ b/src/renderers/testing/ReactTestRenderer.js @@ -53,11 +53,13 @@ class ReactTestComponent { _currentElement: ReactElement; _renderedChildren: null | Object; _topLevelWrapper: null | ReactInstance; + _hostNode: null | Object; constructor(element: ReactElement) { this._currentElement = element; this._renderedChildren = null; this._topLevelWrapper = null; + this._hostNode = null; } mountComponent( @@ -66,7 +68,10 @@ class ReactTestComponent { nativeContainerInfo: ?null, context: Object, ) { + var options = transaction.getTestOptions(); var element = this._currentElement; + this._hostNode = options.createNodeMock(element); + // $FlowFixMe https://github.com/facebook/flow/issues/1805 this.mountChildren(element.props.children, transaction, context); } @@ -76,7 +81,9 @@ class ReactTestComponent { transaction: ReactTestReconcileTransaction, context: Object, ) { + var options = transaction.getTestOptions(); this._currentElement = nextElement; + this._hostNode = options.createNodeMock(nextElement); // $FlowFixMe https://github.com/facebook/flow/issues/1805 this.updateChildren(nextElement.props.children, transaction, context); } diff --git a/src/renderers/testing/__tests__/ReactTestRenderer-test.js b/src/renderers/testing/__tests__/ReactTestRenderer-test.js index 7ca6c201396c..b5935b782c96 100644 --- a/src/renderers/testing/__tests__/ReactTestRenderer-test.js +++ b/src/renderers/testing/__tests__/ReactTestRenderer-test.js @@ -12,6 +12,7 @@ 'use strict'; var React = require('React'); +var findDOMNode = require('findDOMNode'); var ReactTestRenderer = require('ReactTestRenderer'); describe('ReactTestRenderer', () => { @@ -364,4 +365,46 @@ describe('ReactTestRenderer', () => { ]); }); + it('works with findDOMNode', () => { + let nodes = []; + function createNodeMock(element) { + return element.type; + } + + class GrandChild extends React.Component { + render() { + return

Grand child

; + } + } + + class Child extends React.Component { + + componentDidMount() { + nodes.push(findDOMNode(this.ref)); + } + + render() { + return ( + + ); + } + } + + class Parent extends React.Component { + componentDidMount() { + nodes.push(findDOMNode(this.ref)); + } + + render() { + return this.ref = n}/>; + } + } + ReactTestRenderer.create(, { createNodeMock }); + expect(nodes).toEqual(['h1', 'ul']); + }); + });