-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
HOC withRouter causes TestUtils renderIntoDocument to 'fail' silently #3652
Comments
Are you running this in a browser with something like Karma? renderIntoDocument doesn't work without a DOM. |
The instance renders just fine. You can't access the refs because that's just how HoCs work. |
Seems like the recommendation of using the HOC is incompatible with some standard unit testing practices and patterns. I can switch to normal react contexts and be just fine then. Do you guys have test patterns for unit testing HOC wrapped components? |
It's the same caveat as with Consider using a library like teaspoon or Enzyme that let you do testing without the use of refs. Those are essentially modern best practices. |
Cheers. Yes I've been looking at enzyme. Time to make the switch... |
@taion React Redux actually does make it available on a |
I think we should add |
Circling back on this topic. I did indeed migrate to enzyme + chai-enzyme, overall a good move as it made testing much cleaner. On the downside though shallow rendering exhibits similar issues as to what I was experiencing with TestUtils in the opening post. It seem that the HOC is affecting the public, maybe even private, API of the react component and related classes. An HOC is just an implementation/variation of the classic decorator/wrapper/adapter pattern, I would expect the original API of the decorated module to be left intact otherwise composing an application of many disparate libraries that depend on the API integrity will be impossible. I pulled together a tiny project that demonstrates the issues https://github.com/jrobison153/withRouterUnitTesting |
Well the point of shallow rendering is that it only goes one level deep, so if you render something with an HoC, you would get the HoC. Again that's just how HoCs work. We'd invite a PR that adds Redux-style |
Thanks for making that connection for me, I neglected the fact that the HOC created a nested render (the obvious is always so obvious in hindsight). Swapping to a full (deep) render everything works as expected. Sounds like for the indefinite future use of withRouter and shallow rendering provided by any library will be incompatible. Cheers! |
Are you thinking something like this |
We're not deprecating |
Description
wrapping a React component with HOC withRouther causes React TestUtils renderIntoDocument to fail silently. I'm using a mocha/chai/sinon test stack, I will try to carve out some time to pull together a gist if needed.
Version
react-router 2.5.2
react 0.14.1
Steps to reproduce
YourComponent.jsx
YourComponentTests.js
Expected Behavior
Component should render as it does w/o using withRouter. Unit test should be able to inspect the rendered component and assert things about refs, simulate clicks, etc...
Actual Behavior
Component is not actually rendered into the document. Accessing refs, finding elements in the document, etc... return undefined.
The text was updated successfully, but these errors were encountered: