-
-
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.0.0 "find()" queries not just DOM elements #1174
Comments
I'm seeing this issue as well. After upgrading, some of our // Returned 1 element originally
// Now returns 5, one for each React component with the given name
component.find('[name="close-drawer"]'); Thanks for all the hard work that you all put into this release! |
Thanks for the report @FezVrasta. I think the second approach of risking duplicates is the more sustainable one, since you can always use a more specific selector to filter your results. For example, instead of What do you think @lelandrichardson @ljharb? Are we OK with this behavior in v3? If so we should add a note to the migration guide. |
Thanks for the reply! I'm okay with this, but I'd love a way to match only DOM Nodes, maybe a chained method?
Or anything similar to be able to mimic the old behavior. |
@FezVrasta you can do You could also do...
|
Also, I would love it if someone memorialized this change in the migration guide! |
Maybe we should add |
someone should throw up a PR :) I'm not against the idea. |
Using |
I made #1185 without realizing it was a duplicate of this issue. To merge the discussions, it appears that |
It's fine that they differ; but are you sure that's the case for |
fails on the mount (expected 2, got 3) clause but not the shallow clause This is certainly surprising, and made more surprising by the fact that AFAICT this was not the case in enzyme 2 and I was not able to find this documented anywhere So once |
@ljharb yeah, @pfhayes is correct. Enzyme 2 would only apply to host nodes. I think it makes more sense to apply the selector logic to all nodes. If it applies to all nodes you can use |
So to clarify: it is intended behaviour that What is the decision on |
I would expect, for consistency, that it would also query all nodes. Can you file a separate issue for that? |
If anyone isn't clear on why Using Seeing that the host node is now included I only had to change my selector. I am in favour of something like |
@NathanCH v3.1 has |
I know this isn't related directly to your code but I suggest to everyone to stop using element names directly on your find()s. Use css feature markers. Add a className='ft-some-feature' on it and search on that. Makes your life easier and decouples your tests from the implementation, implementation being whatever element your test is testing. If your tests are bound to finding element names you end up having to import that element plus your tests are coupled to that element name. If someone ever changes that element name your tests will break for the wrong reasons...you don't want your tests to break just because of a rename. People need to stop coupling their tests to the DOM or to Element names as much as possible. |
are you saying shallow should act like mount when you say all nodes? that wouldn't make sense. We need to dive() if we want to go x levels deep. |
The question is about a component that contains both DOM and custom components in its shallow render. Separately, I do not recommend using CSS classNames - this is an abuse of CSS. If you want a feature marker, use |
hmm I know a lot of places that use feature markers..not sure how it's an abuse of CSS. Are you saying it's cluttering CSS? I'm not using css classes that are defined as actual styles.... they're just classes used for tests, it's pretty common. I'm not using classes that have styling in them, they're not even in a stylesheet. In this case it's totally fine. I've also had problems with I'd agree it's an abuse if you were reusing styling based css classes bug that's not what I'm referring to.
can you elaborate? |
I could use data attributes, migh be cleaner in some people's view but others might argue something else such as that I'm abusing data attributes :). it’s 6 of one and 1/2 dozen of another on this one, nobody's gonna win this argument but anyway Thanks @ljharb for bring up the thoughts...good to know. And thanks for answering the initial question of mine above. |
It's an abuse of Regardless, with React and enzyme, feature markers are obsolete - you can |
This should really be added to migration docs, as it clearly is a common migration issue. @ljharb
Assuming that classes are tied only to CSS is a common misconception. That said, I agree with the rest of your recommendation :) |
@hon2a the nature of an element is also something that the component should control, and should not be broadly determinable by parents. |
I just switched to Enzyme 3.0.0 and React 16 and I noticed that some of my tests are failing because Enzyme
.find
is finding more than one element with the given ID.Repro repository here:
https://github.com/FezVrasta/enzyme-id-bug-report
Just run
yarn && yarn test
to reproduce.I don't think this is the expected behavior because:
.find
method is now looking for DOM Elements AND React elements with the given ID instead of just looking at the DOM elements.If this is the expected behavior, how can I obtain the previous result? Is there a
findDOMNode
or something similar?Or maybe an helper function that strips from the selection all the "non DOM" elements?
Thanks!
The text was updated successfully, but these errors were encountered: