-
Notifications
You must be signed in to change notification settings - Fork 810
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
Modals cannot be tested with React's TestUtil #71
Comments
Having trouble testing this as well. |
@baer I guess this is because the modal creates a "portal" by mounting the actual modal in a new div at the bottom of the page. You can see in the To get around this you need to access the Hope that makes sense! |
@jkimbo that sounded promising, unfortunately that didn't do the trick for me. I get an undefined on renderedModal.portal. Do you get the React tree on that ? |
@ArthurCid I've written up a passing test for you using mocha and jsdom: https://gist.github.com/jkimbo/872f42732e6c772bf8e1 |
I'm still having trouble with this. I've tried the
|
@gbrassey you need to set |
wow thanks @jkimbo 😵 🔫 |
@jkimbo I figured out my issue. You example worked fine so it was indeed an issue on my side. var renderedModal = TestUtils.renderIntoDocument(
<MyModule
props1:{stubblabla}
/>
); and calling MyModule.portal, which was obviously wrong, I needed the Modal.portal TestUtils.findRenderedComponentWithType(component, Modal); |
@ArthurCid glad you figured it out! |
There hasn't been much traction on this issue in over a year. I'm assuming that this is no longer an issue, especially given that #136 was merged. If it is, please feel free to re-open. |
I know this issue is about React test utils, but is it possible to make testing work with Enzyme.js? Enzyme.js provides a simpler/cleaner API over React test utils (in my opinion) and it would be handy to be able to utilise that when testing code regarding react-modal. |
@begincalendar Is there a specific issue with using Enzyme? In theory it should "just work" assuming the notes about testing from the read me are followed. If there's a specific issue where it fails, please file an issue for it |
There is no issue per se, it's more of that the React test utils give you DOM elements, whereas Enzyme gives you wrappers and pseudo elements. I.e. The whole library is designed to work with these wrappers/pseudoelements, e.g. for making |
Right. I actually really like Enzyme and have used it frequently in other projects. I don't see any reason that you couldn't use Enzyme when testing a component that uses react-modal. |
But you need to extract the As far as I'm aware, with Enzyme, once you take out the reference to the Am I missing something? |
Hmmm... I'd have to put together an example of this to be sure. I don't have time at the moment, but I'll see if I can't put something together soon. |
I'm also interested on how to test with Enzyme. I've been struggling with this for a while. |
Bump. We're currently struggling this as well. Has anyone figured out a reasonable pattern for testing with enzyme? |
Anyone.. ? |
With enzyme, shallow supports portal components while mount does not. If you absolutely must use mount, you can use ref={(ref) => { this.namedRef = ref; } in order to access the element and then wrap it in an enzyme ReactWrapper. |
Here's an example where I used enzyme to test a component with react-modal. The modal is used to confirm a user wants to go forward, and tests to see if confirm_modal.jsx
confirm_modal.spec.jsx
Hopefully that helps you guys out 👍 |
This blog post has some examples for testing react-modal with enzyme: http://remarkablemark.org/blog/2017/05/17/testing-react-modal/ |
@dy-dx thanks for great solution, but it doesn't work with the last enzyme
Do you have any workaround? |
@ramusus I too am unable to get this to work with enzyme 3 and enzyme-adapter-react-15. If you've come up with a workaround, please share. I've tried react-dom/test-utils I've also tried the examples from the blog post recommended by @dy-dx with limited success. I've been able to get a reference to the portal component but it doesn't seem to contain any HTML. (Yes, I've set I've tried using both |
@ryanhomer, found solution - do not use ReactWrapper or any other hacks - content of modals get rendered into the main wrapper, properly in place where it should be logically (but not in the real DOM). So everything becomes more simple and easy to test, just not forget to do wrapper.update() ... |
@ramusus Are you able to post an example? I'm still have issues rendering modal content without ReactWrapper. |
|
@ramusus The example above no longer works for me. I got this error
I'm using react-modal 3.4.4 and enzyme 3.3.0. Is there anything that I miss? |
react-modal is acting outside of the purview of any of React's test utilities.
I see there are some work arounds that were developed circa React 0.12 but I can't figure out why that is necessary. If I was guessing I would say that this has to do with how you mount to the DOM to properly apply the overlay. If this is true, maybe a small wrapper around those helpers as a part of the release would be a helpful thing for us app devs who want to write lots of tests :)
https://github.com/rackt/react-modal/blob/master/specs/helper.js
The text was updated successfully, but these errors were encountered: