Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/renderers/testing/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
Expand All @@ -76,7 +81,9 @@ class ReactTestComponent {
transaction: ReactTestReconcileTransaction,
context: Object,
) {
var options = transaction.getTestOptions();
this._currentElement = nextElement;
this._hostNode = options.createNodeMock(nextElement);
Copy link
Contributor Author

@aweary aweary Oct 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon not sure how to address this, but in receiveComponent the transaction is not pulled from the pool with the test options like it is during the initial mount. This means this fails since options ends up being true instead of the options object we'd want.

Injection makes this kind of a hard case, so I'm wondering what you think we can do here. It seems like we'd have to either attach the test options to the instance and access it there or do more injection, but both of those sound less than ideal.

Copy link
Collaborator

@gaearon gaearon Oct 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. 😞

Can we use this._hostContainerInfo for this instead? It's a thing that DOM renderer uses, for example, for DOM nesting validation. Both composite and host instances already store it so it's always available and passed all the way down. It is the fourth argument to ReactReconciler.mountComponent(), third argument to mountComponent() implementations, and I think it's currently null in the test renderer.

// $FlowFixMe https://github.com/facebook/flow/issues/1805
this.updateChildren(nextElement.props.children, transaction, context);
}
Expand Down
43 changes: 43 additions & 0 deletions src/renderers/testing/__tests__/ReactTestRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

var React = require('React');
var findDOMNode = require('findDOMNode');
var ReactTestRenderer = require('ReactTestRenderer');

describe('ReactTestRenderer', () => {
Expand Down Expand Up @@ -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 <h1>Grand child</h1>;
}
}

class Child extends React.Component {

componentDidMount() {
nodes.push(findDOMNode(this.ref));
}

render() {
return (
<ul>
<li>
<GrandChild ref={n => this.ref = n} />
</li>
</ul>
);
}
}

class Parent extends React.Component {
componentDidMount() {
nodes.push(findDOMNode(this.ref));
}

render() {
return <Child ref={n => this.ref = n}/>;
}
}
ReactTestRenderer.create(<Parent />, { createNodeMock });
expect(nodes).toEqual(['h1', 'ul']);
});

});