-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Support Portal components #252
Comments
If the react element is actually rendered outside of the parent component, then this seems out of enzyme's scope. |
I understand it might be at the moment |
Any news on this? I'm running in the exact same issues except that I'm not using the Material lib, but something custom build. However, the subtree is rendered with |
I would suggest putting up your own PR. I would assume if airbnb doesn't have this issue, they probably won't prioritize doing it themselves. But they would probably accept a PR! |
I thought I'd chime in with my 👍. I'm running into this issue when trying to test a component that uses |
When you |
@aweary I'm going to see if there's something I can patch in and make a quick/easy PR that might fix the issue. I just didn't have the time earlier today. FWIW - It looks like the React Test Utils are able to handle this case by themselves: |
@CrshOverride seems like they handle it by attached the subtree to a |
@aweary It also looks like there's a way to utilize I have a feeling that nobody wants to add a top level method for fetching and wrapping rendered components from the DOM though. |
Is there any progress on testing portal components with enzyme? |
@nvdbleek In my case, I was able to take advantage of the implementation details of React Bootstrap's modal and access the portal component from there. I created a simple helper to create a |
@CrshOverride Hi. Could you provide details of your implementation? |
In order to get it to work, you need to attach a ref to an element -- specifically, the root node that will be transported to a new location in the DOM (i.e. the child of your portal element). import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
export default class MyModal extends PureComponent {
static propTypes = {
children: PropTypes.node,
}
render() {
return (
<Portal>
<div ref={ref => { this.modalRef = ref }}>
{this.props.children}
</div>
</Portal>
)
}
} You can retrieve the ref using the import React from 'react'
import { mount, ReactWrapper } from 'enzyme'
const modal = new ReactWrapper(
mount(
<MyModal>
<p>Hello world</p>
</MyModal>
)
.instance()
.modalRef,
true
)
expect(modal.html()).to.match(/Hello world/) |
@mmiller42 approach works. Just be sure that you attach the ref to the Modal.Body or Modal.Header as attaching to Modal isn't going to work. Thanks! |
Closing this as duplicate of #535 |
@blainekasten I believe this should be kept open unless I missed something here |
@leoselig Good point. It is technically different. I'll update the title and re-open. FWIW, We're not likely to support an |
Absolutely reasonable, did not know about portal component support in fiber - awesome! |
- For testing new modal confirmations, need to use a ref to access rendered modal content - per enzymejs/enzyme#252 (comment)
This cost me so much time that I created a StackOverflow question with a fairly general answer, based on |
The good news is React 16 will feature a Portals api. Likely meaning Enzyme should be able to hook into that API and become testable. |
Since React 16 is now released, I would like to upvote this issue |
@r-tock Did you verify that it still does not work? Since enzyme uses the official |
Yeah, we had to disable tests because they were failing on mount() of a portal wrapped component with a simple div underneath it |
Supporting the React 16 Portals api in Enzyme 3 is mentioned in this much more recent issue: #1150 |
What's the status on this? I see that enzyme-adapter-react-16 has been updated to support portals here: #1345 But even after updating all my NPM dependencies, including getting those changes in the react adapter, this issue is still not fixed. Are there more changes required in other enzyme code to properly use the new portal support in mount()? |
Looks like my problem is that I'm using Blueprintjs, and their Portal component does not yet make use of ReactDOM.createPortal. In case anyone else stumbles across this while using Blueprint, I have created an issue in their project here: palantir/blueprint#1860 |
As far as I understand the issue, it was fixed along the way. I think that we can close it. |
Agreed, no longer an issue in Enzyme 3 + React 16, should be closed. |
We are currently trying to test a component that uses Material UI's SelectField (a dropdown menu). They use their own Popover component (https://github.com/callemall/material-ui/tree/master/src/DropDownMenu) to bypass the typical
overflow: hidden;
issue where parts of the overlay is clipped off. Essentially, they are mounting the children (the menu items of the dropdown) into the body.Unfortunately, enzyme cannot find them this way. E.g.
Are we missing any way to deal with this?
The text was updated successfully, but these errors were encountered: