-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
wrappedComponent doesn't actually pass context values down in tests #2189
Comments
|
Is there an ETA on that? |
My workaround is to add the provider in the test:
and changing it to a |
That should be |
I created module for workaround https://www.npmjs.com/package/shallow-with-context. The module works well in our projects. |
@ljharb Status update? |
Wow, 1.5 years later and it seems like this has stalled. No wonder some people have been saying |
@heath-freenome that's really not called for or helpful. PRs are quite welcome, and if your company's business depends on this project that's maintained for free by a single developer, perhaps there's some business value in investing resources in improving it. |
@ljharb Sorry about that. I'm just frustrated. For the most part, I've switched the bulk of my implementations to using the |
I would love to have full contextType support, but the release of npm 7 broke tests on master, so I'm scrambling to fix that so everything else is unblocked. In the meantime, a PR would be very helpful. |
I don't know enough about enzyme to be helpful in anyway right now. I'm also debating solving this current problem using a hooks approach |
Hooks don't work in class components, so i'm not sure that's going to help you much :-/ |
The idea is to avoid needing a class component... Or at least contexts inside of components |
Right now, i have a dirty solution using
diff --git a/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js b/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js
index 5fc24a5..cfb2bbb 100644
--- a/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js
+++ b/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js
@@ -847,7 +847,13 @@ var ReactSixteenAdapter = /*#__PURE__*/function (_EnzymeAdapter) {
});
var _renderedEl = renderedEl,
Component = _renderedEl.type;
- var context = (0, _enzymeAdapterUtils.getMaskedContext)(Component.contextTypes, unmaskedContext);
+ var context;
+ if (Component.contextType) {
+ var Provider = adapter.getProviderFromConsumer(Component.contextType);
+ context = providerValues.has(Provider) ? providerValues.get(Provider) : getProviderDefaultValue(Provider);
+ } else {
+ context = (0, _enzymeAdapterUtils.getMaskedContext)(Component.contextTypes, unmaskedContext);
+ }
if (isMemo(el.type)) {
var _el$type = el.type,
diff --git a/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js b/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js
index df5c5d4..081bd45 100644
--- a/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js
+++ b/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js
@@ -770,7 +770,7 @@ function () {
var previousElement = this._element;
this._rendering = true;
this._element = element;
- this._context = getMaskedContext(elementType.contextTypes, context); // Inner memo component props aren't currently validated in createElement.
+ this._context = elementType.contextType ? context : getMaskedContext(elementType.contextTypes, context);
if (reactIs.isMemo(element) && elementType.propTypes) {
currentlyValidatingElement = element; I'm not sure if this is the correct semantics, as i've never used the legacy thanks for the hard work ljharb! |
@forivall with a test case, that seems like most of a PR to enzyme already; we don't have to wait for react-test-renderer to update - we can patch it at runtime in the adapter (we already do things like this for a few cases where react itself is broken). |
awesome. i'll submit a PR when i have a few more extra cycles in the next few days (hopefully i remember). |
More than happy to help with the final parts of the fix once the tests are good (and failing) |
…request context via setting .contextType, according to patches posted in enzymejs#2189 (comment). Adds changes to ReactSixteenAdapter and simple test case.
Any updates? |
…request context via setting .contextType, according to patches posted in enzymejs#2189 (comment). Adds changes to ReactSixteenAdapter and simple test case.
Add support for passing context to React class based components that request context via setting .contextType, according to patches posted in enzymejs#2189 (comment). Adds changes to ReactSixteenAdapter and simple test case. Co-authored-by: Kevin Read <me@kevin-read.com> Co-authored-by: Pablo Palacios <pablo.palacios@holidaycheck.com>
Add support for passing context to React class based components that request context via setting .contextType, according to patches posted in enzymejs#2189 (comment). Adds changes to ReactSixteenAdapter and simple test case. Co-authored-by: Kevin Read <me@kevin-read.com> Co-authored-by: Pablo Palacios <pablo.palacios@holidaycheck.com>
Given the following:
Current behavior
Foo
✕ the first is someValues.value1 (17ms)
✕ the second is someValues.value2 (13ms)
● Foo › the first is someValues.value1
● Foo › the second is someValues.value2
Expected behavior
The two tests pass. It seems like the values set into the context in FooProvider don't actually get passed to Foo
Your environment
node: 12.4.0
API
Version
Adapter
The text was updated successfully, but these errors were encountered: