Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Set _hostNode in mountComponent / receiveComponent #8164

Closed

Conversation

aweary
Copy link
Contributor

@aweary aweary commented Oct 31, 2016

Adds support for findDOMNode with ReactTestRenderer, initially mentioned here.

_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.
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.

@gaearon gaearon added this to the 15.4.0 milestone Nov 1, 2016
@gaearon
Copy link
Collaborator

gaearon commented Nov 1, 2016

@spicyj Can we wait with 15.4.0 until this is addressed? I'll try to make sure this gets done by Friday.

@aweary
Copy link
Contributor Author

aweary commented Nov 1, 2016

@gaearon I can look at this again today and likely get it finished 👍

@sophiebits
Copy link
Collaborator

Do you imagine that people use ReactTestRenderer.findDOMNode? If so, let's add that and change the test to use that.

@gaearon
Copy link
Collaborator

gaearon commented Nov 1, 2016

@spicyj I think it should work with ReactDOM.findDOMNode because third party components may be using it with refs.

@sophiebits
Copy link
Collaborator

Hm, my gut reaction is that we should not support that. The renderers should be independent.

@aweary
Copy link
Contributor Author

aweary commented Nov 1, 2016

If we didn't support that then you wouldn't be able to use the test renderer with components that utilize findDOMNode, which includes a number of popular third party component libraries

@sophiebits
Copy link
Collaborator

Do they have to? Can we get them to change? :)

@aweary
Copy link
Contributor Author

aweary commented Nov 1, 2016

Can we get them to change? :)

In an ideal world 😀. I think we should support it as long as using findDOMNode is supported, but I'll defer to you guys.

@gaearon
Copy link
Collaborator

gaearon commented Nov 1, 2016

Not likely: jsx-eslint/eslint-plugin-react#678 (includes comments from React Bootstrap maintainers).

@gaearon gaearon removed this from the 15.4.0 milestone Nov 3, 2016
@aweary
Copy link
Contributor Author

aweary commented Nov 3, 2016

So do we want to move forward with this?

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2016

We need to cut 15.4.0 soon so if you don't see a way to fix this, we'll proceed without it.
Unfortunately nobody on the core team has the time to look into this now.

@aweary
Copy link
Contributor Author

aweary commented Nov 3, 2016

@gaearon I'm pretty sure I can fix it, I just wasn't sure there was agreement on if we should support this or not.

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2016

I'm a bit confused about this solution. Can't user-defined createNodeMock just return an object with nodeType === 1 and then findDOMNode() would pass it through?

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2016

I don't think adding support to test renderer for findDOMNode() is the right solution. The right solution is to fool findDOMNode() into thinking mock node is a DOM node (which is the desired behavior anyway).

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2016

Nevermind, I understand why this wouldn't work for composites now.

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2016

I looked at it again and I'm inclined to agree with @spicyj this is not supported.

For example, we may want to ship ReactDOM with Fiber under the hood, in which case it is by definition incompatible with test renderer. Even if we couple them now with some convention, it's going to break in weird ways eventually, and we wouldn't want to do it when people rely on it in tests.

So unfortunately it seems like snapshot testing can't be used with libraries that rely on findDOMNode(compositeInstance).

@gaearon gaearon closed this Nov 3, 2016
@aweary
Copy link
Contributor Author

aweary commented Nov 3, 2016

@gaearon thanks for expanding, that makes sense to me too. It wouldn't make sense to support it now if it's fundamentally incompatible with Fiber.

At least we tried 😄

@singpolyma-shopify
Copy link

So, any component using findDOMNode makes it so my React app cannot be tested?

@gaearon
Copy link
Collaborator

gaearon commented Mar 21, 2017

Not testable specifically with snapshot testing. You can still use other testing strategies (e.g. shallow rendering, or rendering into jsdom).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants