Add ability to dive() and shallow-render-as-root Consumers and Providers#1966
Conversation
| render(el, context) { | ||
| render(el, context, { | ||
| providerValues = new Map(), | ||
| } = {}) { |
There was a problem hiding this comment.
I'd like to make the third argument an object. This way we can easily pass additional params as enzyme/react evolves without introducing breaking changes.
|
enzyme doesn't yet have any support for createContext, so there shouldn't be any mention of "Consumer" or "Provider" in documentation until that's done. Providers, prior to createContext being a feature, are components that provide child context. |
|
I'm a bit confused. There are a couple of tests (this one and this one) that specifically deal with the I do think the gotcha about needing to |
041cdc0 to
d2e3db0
Compare
d2e3db0 to
6c2c3ac
Compare
|
related reduxjs/react-redux#1161 |
|
Btw I don’t think this implementation is quite right yet. It effectively auto-dives through as many Consumers/Providers as are necessary until it reaches a non Consumer/Provider. I think the correct behavior is for a Consumer/Provider to behave like any other component. Does that make sense? I’ll take a look at implementing what I believe is the correct functionality in a couple of days. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I definitely don't want a "diveUntil" API as part of shallow; that's something I'm planning to tackle as an entirely separate top-level API after we're caught up with React's featureset. I'm not even confident that enzyme should auto-dive Providers and Consumers yet, to be honest. |
This comment has been minimized.
This comment has been minimized.
|
@ljharb I don't think we should auto-dive Providers/Consumers. I think instead you should be able to manually |
6c2c3ac to
f627d1e
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@minznerjosh it makes sense that diving a Consumer/Provider should work as if it was a regular custom component with a function child - altho it's a bit surprising that it doesn't already do that without any enzyme changes. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@ljharb just wondering what you meant by:
but then you also said:
seems contradictory? |
…ateContext()` providers and consumers - add `isContextConsumer` - add `getProviderFromConsumer`
…providers and consumers
f627d1e to
5566ebe
Compare
|
No worries! 😊 I was interpreting your earlier comments as “I’m not going to review this yet because there are major architectural/thematic flaws that need to be fixed before we get into the nitty-gritty of a code review.” I’m hopeful this implementation does what you expect too! So now I’m going to chill out and wait for you to review. 😎 |
|
Guys, any progress on submitting this PR? It is a big blocker for migration to react-redux v6 😭 |
|
We too are blocked on this one, any update? |
7b08b1d to
d35a5fa
Compare
|
@minznerjosh i've freshly rebased this. Can we make sure to add test cases for both shallow and mount to cover this functionality, including both consumers and providers as the root, or with |
|
@ljharb yup I'll give this PR some love tomorrow! |
…`Provider` as root
d35a5fa to
492cd0d
Compare
|
@ljharb Updated!
Looking forward to your review! |
This comment has been minimized.
This comment has been minimized.
e80e978 to
7360912
Compare
- [new] `Utils` add `findElement`, `getNodeFromRootFinder`, `wrapWithWrappingComponent`, `getWrappingComponentMountRenderer`; add `RootFinder` (#1966) - [new] add `getDerivedStateFromError` support (#2036) - [fix] `shallow`/`mount`: `renderProp`: avoid warning when render prop returns `null` (#2076) - [build] include source maps - [deps] update `eslint` - [dev deps] update `eslint`, `semver`, `rimraf`, `react-is`, `html-element-map`, `chai`, `eslint-plugin-mocha`
- [new] support shallow rendering `createContext()` providers and consumers - add `isContextConsumer`, `getProviderFromConsumer` (#1966) - [new] add `wrapWithWrappingComponent`, `isCustomComponent` (#1960) - [refactor] use `react-is` predicates more - [deps] update `react-is` - [dev deps] update `eslint` - [build] include source maps
- [new] Add support for wrapping `Profiler` (#2055) - [new] support shallow rendering `createContext()` providers and consumers - add `isContextConsumer`, `getProviderFromConsumer` (#1966) - [new] add `wrapWithWrappingComponent`, `isCustomComponent` (#1960) - [new] add `getDerivedStateFromError` support (#2036) - [fix] avoid invariant violation in provider (#2083) - [fix] properly fix finding memo(SFC) components (#2081) - [fix] properly render memoized SFCs - [fix] `shallow`: avoid wrapping component for hooks - [deps] update `react-is` - [dev deps] update `eslint` - [refactor] use `react-is` predicates more - [build] include source maps
Fixes #1958
Fixes #1908
Fixes #1647
I think the most controversial choice I've made here is only collecting context when a
<Provider />is the root (either because it is.dive()edor passed directly toshallow(<Provider />)).If we wanted to reliably make a
<Consumer />'svaluebe that of the closest<Provider />, we'd need to recursively.dive()from the top until we find that<Provider />. I don't like that idea because we could end up rendering components that the end user did not want rendered.I think, if the user does want all their
<Provider />s' values to be automatically collected, that's wherewrappingComponent(from #1960) will come into play.