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

Enables finding nodes created by forwardRef #2

Open
wants to merge 4 commits into
base: forward-ref
Choose a base branch
from

Conversation

petegleeson
Copy link

@petegleeson petegleeson commented Apr 30, 2018

Hi mate, I have made some further changes based on your initial work to support forwardRef.

While I was able to render forwardRef nodes in my tests, I ran into problems when trying to find those components. This is because forwardRef nodes are skipped in the toTree method. I explained the problem in more detail on this issue.

These changes make forwardRef nodes look more similar to functional or class-based react nodes.

We currently have a large codebase using Enzyme, so hopefully tomorrow I'll get a chance to test this fork out against that codebase.

@@ -113,11 +113,21 @@ function toTree(vnode) {
}
case HostText: // 6
return node.memoizedProps;
case ForwardRef: {
Copy link
Author

Choose a reason for hiding this comment

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

This is the crux of the change. I realise that this toTree function is largely adapted from toTree in react-test-renderer. This creates a divergence in behaviour between the two libraries which sounds scary. Ideally, this change would go into react-test-renderer and then enzyme could use the toTree function directly.

return {
nodeType: 'function',
type: node.type,
props: { ...node.pendingProps },
Copy link
Owner

Choose a reason for hiding this comment

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

why pendingProps? is that all that's there on this fiber?

Copy link

Choose a reason for hiding this comment

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

Please don't use pendingProps anywhere.

In fact all of the fields Enzyme uses are subject to breakage. But I guess we talked about this before.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I was really just feeling around in the dark here. I used pendingProps because that looked like it had the information I needed. Is it any worse than memoizedProps that the other tags use?

Copy link

@gaearon gaearon May 2, 2018

Choose a reason for hiding this comment

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

pendingProps get cleared by React after rendering. So yes, it's worse and shouldn't be read.

@@ -113,11 +113,21 @@ function toTree(vnode) {
}
case HostText: // 6
return node.memoizedProps;
case ForwardRef: {
return {
nodeType: 'function',
Copy link
Owner

Choose a reason for hiding this comment

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

hmm we should probably have a new type for this, but that would require much wider enzyme changes cc @aweary I think this is ok for now?

// The node type can be defined in three different places.
// Where the type is depends on the node and rendering strategy.
// This function looks in the three spots and returns the type.
const getNodeType = (node) => {
Copy link

Choose a reason for hiding this comment

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

I think you can use typeOf from react-is for this, no?

Copy link
Owner

Choose a reason for hiding this comment

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

I think i tried that and it didn't work...the node here isn't an element, which i think is what is expected by react-is

Copy link

Choose a reason for hiding this comment

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

Aah okay

@petegleeson
Copy link
Author

@jquense has there been any attempts to make an adapter based on react-test-renderer? Having to make changes for every new node type React ships feels like a losing battle. I'll spike something today and see where I get to.

@ljharb ljharb force-pushed the forward-ref branch 3 times, most recently from 62a06f6 to f818a17 Compare July 22, 2018 22:09
@ljharb ljharb force-pushed the forward-ref branch 3 times, most recently from f0c7b6a to 6be05e9 Compare August 5, 2018 23:18
@ljharb ljharb force-pushed the forward-ref branch 2 times, most recently from 7fc51c1 to d7a4ab3 Compare August 7, 2018 07:20
@ljharb ljharb force-pushed the forward-ref branch 3 times, most recently from b90bc58 to 965b785 Compare August 16, 2018 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants