Skip to content
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

component.prop("id") returned with component.find("#foo") #1214

Closed
nikglavin opened this issue Oct 3, 2017 · 10 comments
Closed

component.prop("id") returned with component.find("#foo") #1214

nikglavin opened this issue Oct 3, 2017 · 10 comments

Comments

@nikglavin
Copy link

nikglavin commented Oct 3, 2017

Is this a bug report?

Yes

Environment

[email protected]

Steps to Reproduce

When a component is mounted with JSDOM the selector library does not differentiate props.id and element.id

using react router as an example

const test = mount(<Link id="foo" />)
const fooLink = test.find("#foo) <-- returns 2 nodes (<Link /> + <a>)

Expected Behavior

I would not expect a custom component with the prop id to be returned when querying with a CSS selector.

  • test.findWhere(n => n.prop("id") === foo) <-- expect to return 2 nodes
  • test.find("#foo") <-- expect to return a single node
@ljharb
Copy link
Member

ljharb commented Oct 3, 2017

The selector API intentionally hits all nodes, host and composite - it's not a DOM, so the rule about "IDs are unique" doesn't really apply.

What you probably want is #1179 - ie, test.hostNodes().find('#foo')?

@nikglavin
Copy link
Author

That will do the trick, thanks for your quick response

@nikglavin nikglavin reopened this Oct 3, 2017
@ljharb
Copy link
Member

ljharb commented Oct 3, 2017

hostNodes is now released in v3.1

@ljharb ljharb closed this as completed Oct 3, 2017
@conartist6
Copy link
Contributor

This just wasted several hours of my time.

@conartist6
Copy link
Contributor

Isn't it pretty clearly wrong? There's one api for finding prop values, and a separate api for matching CSS selectors. Why would it be acceptable that they are mixed? A React component can't match a CSS selector because CSS selectors match html elements, which a React component isn't.

I don't disagree that the solution is to use hostNodes(), but enzyme should be doing that internally when a string selector is given to find. I'd strongly recommend that this issue be reopened.

@conartist6
Copy link
Contributor

conartist6 commented Nov 16, 2018

What's more, hostNodes().find("#id") doesn't even work for me. 0 results. (I had 2 results as described in this issue if i just do .find("#id"))
[email protected]
[email protected]
[email protected]
[email protected]

@ljharb
Copy link
Member

ljharb commented Nov 16, 2018

enzyme is independent of react and of html. What would the selector api do with, say, react native?

@ljharb
Copy link
Member

ljharb commented Nov 16, 2018

As far as the specific thing that doesn’t work for you, please file an issue and we’ll see if we can straighten it out.

@conartist6
Copy link
Contributor

I have opened #1907 for the problems which occur regardless of this issue.

@conartist6
Copy link
Contributor

Perhaps all that needs to be done is for there to be a convenience API, something like findHost which was a shortcut for hostNodes().find() At least having such a thing be present in documentation would give it more visibility and allow the find documentation to call out the circumstance in which using it would be appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants