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

react-test-renderer: the findByType method doesn't work with memo components #17301

Open
bensampaio opened this issue Nov 7, 2019 · 8 comments

Comments

@bensampaio
Copy link

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
Whenever I try something like:

ReactTestRenderer.create(<SomeComponent />).root.findByType(SomeMemoComponent);

I get the following error: No instances found with node type: "undefined". The only way I found for this to work was to reference the type property of memo components like this:

ReactTestRenderer.create(<SomeComponent />).root.findByType(SomeMemoComponent.type);

I am fine with this solution but then flow complains that type doesn't exist so I find myself fixing this with $FlowFixMe all over the place.

What is the expected behavior?

I would expect that passing a memo component to findByType would work. Or that flow would recognize the type property of memo components. I think both should work, specially the first option.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

No, it never worked as far as I know.

@threepointone
Copy link
Contributor

I think this is a bug, likely fixed by #17278

@milesj
Copy link
Contributor

milesj commented Nov 14, 2019

If you debug the result tree, the memo component does not exist, only the component that it wrapped. I believe this is by design? Since the tree is the "reconciled" tree. Would need to dig further.

Edit: Tested on v16.12. The PR above didn't fix this.

@bensampaio
Copy link
Author

@milesj indeed, v16.12 didn't fix this problem. If findByType can't work with memo components then I would expect flow to at least recognize the type property of memo components.

@ianschmitz
Copy link

ianschmitz commented Nov 18, 2019

I've found a workaround for those that need it.

Pin react, react-dom, react-test-renderer to 16.11.0, and most importantly add a dependency to react-is@16.11.0.

@henryqdineen
Copy link
Contributor

I am having the same issue. One thing that I found is that if SomeMemoComponent.defaultProps is defined it does work as expected. It looks like there is some distinction between MemoComponent and SimpleMemoComponent when building the tree.

@martijnsenden
Copy link

Also having this issue. @henryqdineen I tried defining the defaultProps, but that did not help for me.

@gaearon
Copy link
Collaborator

gaearon commented Mar 23, 2020

See my comment on #17700 (comment).

@chris-feist
Copy link

I ended up creating my own test predicate until this is resolved:

const MyComponent = () => (<div />);
const MemoMyComponent = React.memo(MyComponent);

const elementByType = (type) => (element) => (
  element.type === type // Match non-memo'd
  || element.type === type.type // Match memo'd
);

const rendered = renderer.create((
  <MyComponent />
));

rendered.root.find(elementByType(MyComponent)) 

const renderedWithMemo = renderer.create((
  <MemoMyComponent />
));

renderedWithMemo.root.find(elementByType(MemoMyComponent)) 

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

Successfully merging a pull request may close this issue.

8 participants