-
Notifications
You must be signed in to change notification settings - Fork 47.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
React-test-renderer: support for portal #11565
Comments
Do you want to dig into why this happens? |
@gaearon I would be interested in taking a look if @alansouzati is okay with that. |
sure. I'm ok if you want to investigate that @505aaron. |
I've been running into problems testing portals in a CRA project with const dropContainer = document.body.appendChild(document.createElement("div"))
const Drop = () => createPortal(<div>hello</div>, dropContainer) The error I'm getting is
Is this a separate issue or am I doing something wrong? |
Hello @esturcke, I encountered the same issue. I hit the same error as you. I also tried using a mock ref as documented https://reactjs.org/docs/test-renderer.html#ideas. Test renderer obviously needs to remain separate from react-dom. I think mocking createPortal or extending the test renderer to make it easier to mock createPortal similar to ref mocking. Sorry for the delay, I was on vacation. |
@esturcke @alansouzati Here is a gist that has a sample mock and snapshot test: @gaearon probably has a better idea/plan for supporting createPortal in react-test-renderer. |
so @esturcke ,what did you do ? |
Hi @yossijacob. I've sort of been pushing off trying to deal with it and moved on to working on other things in the hopes that @gaearon and others might point out what I'm doing wrong or add support to |
We've encountered the same problem with I dig a little and found, that I can work on this, but I need a little guidance @gaearon |
i have the same problem @gaearon |
I have same problem with snapshot tests for components which use |
Hi, I was investigating this issue and here is what I found. As @malstoun pointed out, the reason is an incompatibility between A workaround is to tricking both APIs to accept the "new container" (this is very hacky). const ELEMENT_TYPE = 1;
const dropContainer = { children: [], nodeType: ELEMENT_TYPE };
// if necessary, you can include methods like `appendChild` to this object. For anyone investigating this... #12289 is a temporary fix/helper. It will export the const modalTree = toJSON(dropContainer.children[0]);
expect(modalTree).toMatchSnapshot(); Hope this can help on further investigation of this issue. |
Speaking of workarounds.
|
Something else that works is mocking portals in the top of the test file like this: jest.mock('rc-util/lib/Portal') Obviously the |
Here is an example of how I did, you can Mock the describe('Popover', () => {
beforeAll(() => {
ReactDOM.createPortal = jest.fn((element, node) => {
return element
})
})
afterEach(() => {
ReactDOM.createPortal.mockClear()
})
it('should render correctly with Node or Function', () => {
const component = renderer.create(
<Popover active trigger={<button>BUTTON</button>}>this is a Popover</Popover>
)
expect(component.toJSON()).toMatchSnapshot()
})
}) Then your Snaptests will looks like this: // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Popover should render correctly with Node or Function 1`] = `
<div
className="popover"
>
<button
onClick={[Function]}
>
BUTTON
</button>
<div
className="bottom show"
onClick={[Function]}
style={
Object {
"left": 0,
"position": "absolute",
"top": 0,
"zIndex": 800,
}
}
>
this is a Popover
</div>
</div>
`; Any thoughts? |
Currently, two React renderers cannot be used at the same time in the way that is being described. (A exception sort of exists for The reason this fails is mentioned above- the container types are different. I think this should be possible to support in a better way. See PR #12895. |
For context, latest thinking on this is: #12895 (comment) |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
bump |
bump, none of the above solutions worked |
bump |
@chrys-exaucet both of them are in vanilla js, not TypeScript. Since it's a workaround anyway, just make TS to ignore these types like this:
or like this
|
I've just tried to reproduce the issue and all seems to work fine with React 17, specifically:
Was it fixed in 17 or earlier? |
I still have the error mentioned in the below issue. Is there a fix for this when using Material UI? Seems MUI is dependent on this.
|
@cardamo, I experience issues with your specified versions. Here's the related sandbox. |
bump |
1 similar comment
bump |
Bump! |
Folks: Please stop bumping this issue, or we will lock it. The core team is aware of it. I will tag it as "React Core Team" so no bot attempts to close it. In return, please trust us to prioritize it appropriately. |
Thank you. I can say for certain that mocking the portal function works on a pure javascript environment. |
bump! |
@cardamo For your solution, or others doing this: ReactDOM.createPortal = node => node as ReactPortal Where are |
Note: this worked based on a Stack Overflow answer: jest.mock('react-dom', () => ({
// @ts-ignore
...jest.requireActual('react-dom'),
createPortal: node => node
})) |
it really worked! |
You can also auto-mock const originalModule = jest.requireActual('react-dom');
module.exports = {
...originalModule,
createPortal: node => node
};
export {}; |
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
This test
fails with
This test passes if I wrap createPortal in a container.
What is the expected behavior?
The code without the parent container works fine in the browser. So it seems that I'm adding the parent
div
just for the test to pass. I believereact-test-renderer
should support empty returns?Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Lastest
The text was updated successfully, but these errors were encountered: