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

selector on key-prop doesnt return any matches. #645

Closed
krsmedlund opened this issue Oct 28, 2016 · 12 comments
Closed

selector on key-prop doesnt return any matches. #645

krsmedlund opened this issue Oct 28, 2016 · 12 comments

Comments

@krsmedlund
Copy link

krsmedlund commented Oct 28, 2016

Either its a bug or it probably should be documented that this prop does not work

let wrapper = shallow(<div><div key="foo" /></div>)
expect(wrapper.contains(<div key="foo" />)).toEqual(true) // No error
expect(wrapper.find('[key="foo"]').length).toEqual(1) // Error: Expected 0 to equal 1
expect(wrapper.find({key:"foo"}).length).toEqual(1) // Error: Expected 0 to equal 1
@ljharb
Copy link
Member

ljharb commented Oct 28, 2016

This is intentional. "key" is not a real prop, it's a magic thing React uses to identify array items.

In other words, you need to use a different name than "key".

@aweary
Copy link
Collaborator

aweary commented Nov 1, 2016

You can read more about key here: https://facebook.github.io/react/docs/lists-and-keys.html#keys

@aweary aweary closed this as completed Nov 1, 2016
@untsamphan
Copy link

It would be nice if this is mentioned in the doc ( http://airbnb.io/enzyme/docs/api/selector.html ).

@ljharb
Copy link
Member

ljharb commented Mar 25, 2017

enzyme docs naturally assume that you've previously read the React docs; but sure, a PR to make our docs link to the React docs on "key" and "ref" would be great.

@melinath
Copy link

As someone who has read the React docs and worked with it extensively, I'd like to say:

  1. It is not obvious to me from the selector docs that key would not be usable in a selector, since key isn't mentioned and since enzyme is specifically designed to work with React.
  2. It would make a lot of sense to me (and apparently to other people) if enzyme did work with key, since again enzyme is specifically designed to work with react, but I understand your decision & just wish it were better documented.
  3. The condescending comment about the React docs was really unnecessary and dismissive.

@WalterWeidner
Copy link

WalterWeidner commented Mar 23, 2018

You can do this instead:

wrapper.findWhere(node => node.key() === 'foo');

@lobsterkatie
Copy link

I'd like to second point 2) of @melinath 's post above - key is exactly the kind of thing which we should be able to select on, since the use case for wanting to search on it (you have multiple instances of the same component) is precisely the same case in which react forces you to have it (and forces it to be unique, making it a good selector).

It would great if enzyme would reconsider this decision.

@ljharb
Copy link
Member

ljharb commented Jul 10, 2018

@lobsterkatie per #645 (comment), you can already do this - just using findWhere, not find.

@WalterWeidner
Copy link

One issue I ran into using the findWhere() approach is that you will get errors when the current node isn't an element. I just wrote a function to check for that though.

@ljharb
Copy link
Member

ljharb commented Jul 22, 2018

@WalterWeidner n && n.whatever should handle that?

@Wgil
Copy link

Wgil commented Aug 30, 2018

What about testing the key prop to match a specific value using something like:

expect(
  wrapper.contains([<A key={id} />, <B key={id} />])
).toBeTruthy()

You could simply pass the same id to all the Nodes and it is going to work when it should not.

@kepope3
Copy link

kepope3 commented May 13, 2019

Add an id (or some other identifier) to your div, or even better create a component

let wrapper = shallow(
      <div>
        <div id="fooId" key="foo" />
      </div>
    );

Then use standard find to get the key function instead of findWhere

expect(wrapper.find("#fooId").key()).toBe("foo");

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

9 participants