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

.key() isn't consistent with unwrapped element .key #1488

Closed
3 of 10 tasks
noinkling opened this issue Jan 24, 2018 · 6 comments
Closed
3 of 10 tasks

.key() isn't consistent with unwrapped element .key #1488

noinkling opened this issue Jan 24, 2018 · 6 comments

Comments

@noinkling
Copy link

noinkling commented Jan 24, 2018

Current behavior

If a key is not specified on an element, Enzyme returns undefined from .key()

Expected behavior

I would expect .key() to return null the same as React does for .key on an (unwrapped) element.

Contrived example: https://codesandbox.io/s/xpj8pvpj2o

My use case is that I'm writing a test for a library that ensures that an element generated from a shorthand prop (on a parent) matches a specific factory-generated counterpart. Part of this process involves the optional generation of a key, so I need to check that the keys match (which isn't covered by .contains() or similar).

As you can see from the example, the assertion works fine when a key is provided/expected, but breaks when it isn't. There are easy workarounds:

  • Use .get() to retrieve the unwrapped element
  • Compare with loose equality
  • Wrap the other element

...but I thought I'd bring it up anyway.

Your environment

API

  • shallow
  • mount
  • render

Version

library version
Enzyme 3.3.0
React 16.0.0

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )
@noinkling
Copy link
Author

I suspect this might be related to #1240 / #1254

@ljharb
Copy link
Member

ljharb commented Jan 25, 2018

It seems like this could be addressed by changing all the || undefineds to || null, but then that might retrigger #1240.

If you'd like to write a PR that attempts to fix this (especially if it maintains consistency across the adapter versions), I'd love to review it.

@bdwain
Copy link
Contributor

bdwain commented Feb 11, 2018

Can't the fix just be isolated to the key() method on the wrapper? If the internal value is undefined, it can return null, otherwise it can return the internal value (which should be a string) anyway.

@ljharb
Copy link
Member

ljharb commented Feb 20, 2018

@bdwain that seems like it might work. Want to try out a PR?

@bdwain
Copy link
Contributor

bdwain commented Feb 23, 2018

yea i can do it.

bdwain added a commit to bdwain/enzyme that referenced this issue Feb 23, 2018
@bdwain
Copy link
Contributor

bdwain commented Feb 23, 2018

made #1536

bdwain added a commit to bdwain/enzyme that referenced this issue Feb 23, 2018
bdwain added a commit to bdwain/enzyme that referenced this issue Feb 23, 2018
ljharb pushed a commit to bdwain/enzyme that referenced this issue Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants