-
-
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
Enzyme 3: Mount rendering finds the same element twice with React.cloneElement #1253
Comments
In this case, it's because it's finding both |
I don't think so. My result is the same even if my wrapper is const wrapper = mount(
<div>
<Dummy id="find-me"/>
</div>,
); Sorry, should have given a clearer example. Updated the original example. |
Maybe I'm not being clear. Try |
<div>
<Component id="find-me">
<div id="find-me" />
</Component>
</div> Now I can see why is it finding two nodes. |
Hmm - |
Yes. |
Just to confirm: what version of enzyme, react, and adapter are you using? Can you upgrade them all to the latest (if they're not already there) and try again? |
I'm on |
In that case, I'd say it's a bug in |
<div>
<Component id="find-me" />
</div> |
Is it possible to prioritize this issue? From my point of view it is a blocker for existing projects that want to upgrade to react16 |
I also have Mount Rendering problem that render component twice. |
@newnewnet can you file a new issue for that? |
Any updates on this issue? |
If there were updates, they'd be posted here. PRs are always welcome. |
It seems after reading the thread here that the bug has nothing to do with the title of the issue but rather the behavior of I am not sure this is a bug. Wouldn't this be expected behavior? Based on my limited knowledge of the codebase, I would expect If this is not expected behavior I imagine it would be an enhancement of some kind. |
Yes, I think that is expected behavior. You need @nithinpeter what happens if you try |
Chiming in that I'm experiencing a similar issue, but hostNodes returns nothing for me. import React from 'react';
import { Form } from 'components/views';
import { mount } from 'enzyme';
let wrapper;
let onSubmitSpy;
beforeEach(() => {
onSubmitSpy = sinon.spy();
wrapper = mount(
<Form id="form" onSubmit={onSubmitSpy} className={formClass}>
<input
type="text"
id="text_field"
name="text_field"
/>
<select id="select" name="select">
<option value="option1">option1</option>
<option value="option2">option2</option>
</select>
<input
id="radio1"
name="radio"
type="radio"
value="1"
/>
<input
id="radio2"
name="radio"
type="radio"
value="2"
defaultChecked
/>
<button type="submit" id="submit_button"></button>
</Form>
);
}); Without hostNodes console.log(wrapper.debug()); Results in <Form id="form" onSubmit={[Function: proxy]} className="form_class_name">
<form id="form" className="form_class_name" onSubmit={[Function]}>
<input type="text" id="text_field" name="text_field" />
<select id="select" name="select">
<option value="option1">
option1
</option>
<option value="option2">
option2
</option>
</select>
<input id="radio1" name="radio" type="radio" value="1" />
<input id="radio2" name="radio" type="radio" value="2" defaultChecked={true} />
<button type="submit" id="submit_button" />
</form>
</Form> With hostNodes console.log(wrapper.hostNodes().debug()); Results in
I'm guessing that returning an empty string is not the expected behavior here? |
@hankthewhale in fact this is expected; If you instead try |
This seems addressed, so I'm going to close it; happy to reopen if something actionable comes up. |
@ljharb good call. I did see exactly that and it totally makes sense. |
Found the solution here: enzymejs/enzyme#1253
This behavior seems non-optimal. As you could previous to Enzyme 3, you should be able to do something like: What is the value of rendering the React Component along with the divs? |
@BrennerSpear The value is that the tree is consistent between mount and shallow, and provides access to both the component as well as the nodes it renders. |
Got it. Thank you. And thank you for the example. I'm trying to think of when you would want the React Components to also be rendered to test against them. I guess if you're dynamically rendering what is to be passed into them and would like to check against that rather than the actual divs.. |
That, as well as if you want to be able to access instance methods for any particular reason, like if you want to forcibly setState somewhere inside the tree. |
The following worked for me 👍
|
I just hit up against this, it seems you can work around this to get the HTML element by being specific about the selector.
|
@chrisrymer alternatively, |
@ljharb awesome work responding to the community! Is it possible to make a patch in Just so you know, about the issues with actual DOM elements and React Elements when upgrading. For me, Thanks |
No, it's not, and that would be a poor choice because it's too magic and implicit. I'd suggest writing a codemod that changes your entire codebase all at once (also, definitely upgrade to enzyme 3 and get everything passing, before upgrading your react version) |
@ljharb you mean upgrade only enzyme and use React 15 adapter, and then upgrade React? And also, you suggest the codemode to find all Thanks for the quick reply. |
@guilherme6191 yes, precisely that - get enzyme 3 working on React 15 with the appropriate adapter first, before trying to upgrade React. For the codemod, yes, that's probably the simplest approach - at least, all |
hostNodes fixed it for me |
If I clone an element and try to find the same element after mount rendering, Enzyme finds the element twice.
Please see my component and test below:
Component
Test
The text was updated successfully, but these errors were encountered: