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 TestUtils findRenderedComponentWithType can't find connected components #1279

Closed
rommguy opened this issue May 7, 2019 · 3 comments
Closed

Comments

@rommguy
Copy link

rommguy commented May 7, 2019

First - Thank you very much, you are doing amazing work!

Bug report:

What is the current behavior?
I'm using TestUtils from react-dom/test-utils

After rendering a component tree that contains a connected component, the connected component instance can't be found with TestUtils.findRenderedComponentWithType

Example -

In my component

class MyCompInner extends Component {
...
}             
const MyComp = connect(mapStateToProps ...)(MyCompInner)

In the test

// someWrapper is an instance that was rendered with ReactDOM.render. It renders the connected MyComp in its render method
// method

const myCompInstance = TestUtils.findRenderedComponentWithType(someWrapper, MyComp) 
// fails with Error: Did not find exactly one match (found: 0) for componentType:[object Object]      

const myCompInnerInstance = TestUtils.findRenderedComponentWithType(someWrapper, MyCompInner) 
// works ok

Please let me know if you need the full code example or this is enough to understand.

What is the expected behavior?
It should be possible to find the connected component instance, not just the inner component instance, as it was in previous versions.

Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?

The tests run in node, with jest (standard CRA project)
I've just upgraded react-redux from 5.0.7 to 7.0.2 and this started happening
I'm using version react and react-dom version 16.8.4

@timdorr
Copy link
Member

timdorr commented May 7, 2019

See facebook/react#13455. The basic recommendation is not to use test-utils with these newer kinds of object elements (React.memo, forwardRef, etc). You should switch to using react-testing-library.

@timdorr timdorr closed this as completed May 7, 2019
@rommguy
Copy link
Author

rommguy commented May 9, 2019

Thanks you @timdorr.
I see now this issue isn't related to react-redux. I do believe React team should provide a solution for this - some basic way to traverse the tree.
I'll check react-testing-library, but at first glance it seems problematic, since the API is synchronous and React rendering isn't (at least not guaranteed to be).

@JakeSidSmith
Copy link

This issue's pretty old now, but in case anyone is looking for a somewhat hacky workaround, you can find one here

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

No branches or pull requests

3 participants