-
-
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
componentDidUpdate is not called for shallow rendered component #1452
Comments
Currently, enzyme supports all lifecycle methods only when the update occurs through I used to create a patch to support this case but we should investigate that the patch doesn't break anything. @ljharb What do you think? |
In general, If instead, this did |
The result is same. |
|
@craigkovatch The issue you've mentioned is about not to call |
@koba04 i'm not sure why we'd need to override setState; wouldn't that cause the component to rerender normally, and enzyme could be aware of that? |
@ljharb Current lifecycle methods support is hooking update methods ShallowWrapper provides, which are |
@koba04 you are right, the issue I saw was inside of render() after a wrapper.update(). I will investigate the details and file a separate issue if necessary. |
Hey. Do you have any updates on this? |
@smoholkar |
@koba04 Here's one example
|
@smoholkar You might be misunderstanding what When you use |
@koba04 I'm hitting this issue too. I have an Here is a simple gist: https://gist.github.com/redonkulus/f60372c4b1091f5a8440364e69688cc6 |
@redonkulus Yes, it's the same issue as this. See also #1452 (comment) |
I am having the same issue as well. |
Any update on this? |
Can someone provide a simple test case (that does not use simulate) that illustrates the problem? |
I've created a simple test case for this. |
Thanks; https://travis-ci.org/airbnb/enzyme/jobs/400283980 that shows that it's failing on React 16 specifically. |
Yes, ReactShallowRenderer called |
ok, i've added a branch for someone to build off of - i'm not sure how to handle this in a way that is a) not React-specific outside adapters, b) not a breaking change to all the adapters (altho this might be necessary). |
@koba04, this makes so much sense now. I was banging my head trying to figure out why a component that called setState itself wasn't calling the componentDidUpdate function, but if I called it myself it was working, however the prevState and this.state values were the same. I believe this is a breaking change from enzyme 2 because it used to allow component functions to call setState and the componentDidUpdate lifecycle method would get called. We didn't need an external
The workaround that I've discovered is to unit test the |
This change was caused by facebook/react#10372, which means this change isn't intentional for Enzyme. Replacing |
@koba04, thanks for the PR and comment regarding replacing
The fix that I used was to split the tests into two, one that spied on the instance's setState to make sure the function call used setState, and another that executed wrapper.setState() with the same information that the function set, to make sure the componentDidUpdate worked. Using 2 different tests works from a unit test standpoint, but it's still not ideal for integration-style tests (which is what this project is using). I commented on your PR and approved it, however I am not an enzyme contributor so my approval might not mean much. Thanks! |
Could you create a test to reproduce it? |
@koba04, using the same example as above, the way that I implemented it is this:
Please note that your new code should make this problem irrelevant. The fix that I used was to split the test:
|
stub instance.setState when the component is mounting Fixes enzymejs#1452.
npm package versions
enzyme: 3.2.0
enzyme-adapter-react-16: 1.1.1
react: 16.2.0
react-dom: 16.2.0
Problem
When changing the state inside a component,
componentDidUpdate
is not called.Expected behaviour
When changing the state inside a component,
componentDidUpdate
should get called.Code example
Console output
The text was updated successfully, but these errors were encountered: