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

dive() breaks when using the new context API. #1647

Closed
Tracked by #1553
stellarhoof opened this issue May 4, 2018 · 22 comments · Fixed by #1966
Closed
Tracked by #1553

dive() breaks when using the new context API. #1647

stellarhoof opened this issue May 4, 2018 · 22 comments · Fixed by #1966

Comments

@stellarhoof
Copy link

stellarhoof commented May 4, 2018

Describe the bug

The new React context elements (https://reactjs.org/docs/context.html) report their type as objects instead of functions (custom react components) or strings (native react components), which breaks dive(). I haven't tested whether other methods of ShallowWrapper break.

To Reproduce

https://codesandbox.io/s/zxj4898zxp?module=%2Fsrc%2FApp.test.js

Expected behavior

shallow and dive should implement actual render semantics when handling the new context API:

  1. Automatically update current context whenever a context provider is encountered and remove the provider node from the tree.
  2. Automatically pass the current context to childs of context consumer nodes and remove the consumer node from the tree.

I'm sure the above are not the exact semantics, but at least the most intuitive.

@ljharb ljharb added this to Needs Triage in React 16 via automation Jun 26, 2018
@ljharb
Copy link
Member

ljharb commented Jun 26, 2018

If you're using the latest version of react-test-renderer (you may need to rm -rf node_modules && npm install, or mess with your lockfile), is this still a problem?

@anthonator
Copy link

@ljharb I can confirm that I am seeing this error when using the latest version of react-test-renderer. I'm also using @KevinGrandon's enzyme-react-adapter-future. Not sure if that's relevant.

Also, just for reference, this is the error I'm seeing:

TypeError: ShallowWrapper::dive() can only be called on components

@anthonator
Copy link

Also, just for reference, this is what debug outputs on the dive just before receiving the above error:

<[object Object] value={{...}}>
  ...snip...
</[object Object]>

@ljharb
Copy link
Member

ljharb commented Jul 5, 2018

@anthonator thanks - could you provide a complete minimal test case?

@ljharb ljharb moved this from Needs Triage to Context in React 16 Jul 5, 2018
@anthonator
Copy link

@ljharb I believe @stellarhoof already provided a test case. Is there something more you need?

@anthonator
Copy link

@ljharb https://codesandbox.io/s/9l1rk4l20y?module=%2Fsrc%2FApp.test.js

Let me know if this is what you're looking for.

@joeldenning
Copy link
Contributor

+1 I'm seeing the same problem.

@paddotk
Copy link

paddotk commented Aug 15, 2018

I just upgraded to Enzyme 3.4.1 but still getting the same error (TypeError: ShallowWrapper::dive() can only be called on components). Did the fix work for anyone else?

@ljharb
Copy link
Member

ljharb commented Aug 15, 2018

@paddotk it's not been fixed yet.

@kseebrinegar

This comment has been minimized.

@ljharb

This comment has been minimized.

@kseebrinegar

This comment has been minimized.

@OzzieOrca
Copy link

Is there any way we can help with this? I tried digging into the code a little while ago and found it hard to follow. If I remember correctly, bypassing this check (and the error message it throws) https://github.com/airbnb/enzyme/blob/0bbdc988b149b8e75ff8cb530ab740b0459ce43d/packages/enzyme/src/ShallowWrapper.js#L1411 casued issues later on. I'm not really sure what needs to be done. I could try playing with it again.

I would love to move forward with an upgrade and this is the only thing holding us back as we've used dive a lot of places in our tests.

@ljharb
Copy link
Member

ljharb commented Nov 21, 2018

@OzzieOrca the issue, I think, is a bit deeper than that. Although shallow takes a context option, that context is not filtered down to match the contextTypes of the component; getChildContext() is not taken into account, and the new child context is also not provided to the dived component and filtered down by contextTypes.

@OzzieOrca
Copy link

That's helpful, thanks. I might take a look at it again sometime soon unless someone more knowledgeable is able to jump on it.

@alexandrudanpop
Copy link

Is there a workaround for this issue? Getting this when testing redux connected components.

@ljharb
Copy link
Member

ljharb commented Feb 5, 2019

@alexandrudanpop downgrade to react-redux < 6, iirc.

@OzzieOrca
Copy link

OzzieOrca commented Feb 5, 2019

Unless I'm missing something redux is only at 4.0.1. https://github.com/reduxjs/redux/releases

Nvm. Assuming you meant react-redux. I should try that...

@csvan
Copy link

csvan commented Feb 15, 2019

I would love to help here if possible, can I get some pointers on where to start? Currently, this completely blocks us from moving to either react-redux 6.0 or styled-components 4.0, both which use the new Context API and completely break under shallow.

@ljharb
Copy link
Member

ljharb commented Feb 16, 2019

@csvan the two pieces I'm waiting for are #1960 and then #1966; after those go in, I suspect it'll be relatively short work to get full createContext support.

@csvan
Copy link

csvan commented Mar 28, 2019

@ljharb I apologise for being a nag, but what is the status on those PR:s being merged? Can we assist in any way?

@ljharb
Copy link
Member

ljharb commented Apr 2, 2019

I've just landed #1960, and rebased #1966 (which needs some more work). I'm hopeful to make a release soon.

context interaction automation moved this from To do / needs triage to Done / Closed Apr 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
React 16
  
Context
context interaction
  
Done / Closed
Development

Successfully merging a pull request may close this issue.

9 participants