-
-
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
.setProps()
doesn't trigger componentDidUpdate lifecycle method
#34
Comments
@blainekasten thanks for that info. Funny - I didn't even know The deprecation doesn't affect us in this case, since |
At the moment I'm doing const prevProp = wrapper.props()
wrapper.setProps({
current_step: 2,
})
// forces an update call to componentDidUpdate - https://github.com/airbnb/enzyme/issues/34
wrapper.instance().componentDidUpdate(prevProp) |
+1, how correctly to solve the problem?) |
I'm betting this will be resolved by #318 |
I'm also performing similar hacks to what @CurtisHumphrey mentioned above. I wonder what the preferred way of achieving this is, if not with |
#318 is now merged. Do we know if this is still a problem? Can any of the affected devs here confirm? |
hi @blainekasten I'm using the latest release 2.4.1 and it still hasn't fixed the issue. I ended up having to use |
@bricejlin shallow(<Foo />, { lifecycleExperimental: true }) |
Closing since this should be resolved by using |
In react-native
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sorry if this was off-topic. I can discuss this further offline if you want. I've been thinking about this for a while now and would like to know more about why you think this is bad. But here are some things to consider.
|
The sum of your unit tests is not equal to integration tests. You might be able to “get by” with more focused unit tests and fewer wrappers, but those aren’t better tests - those are more contrived since, unlike the wrappers, they don’t mimic production usage. |
Solved it by using wrapper.setProps({
user: {
test: {error: "test"}
}
});
wrapper.find('.smth').simulate('click'); Works as a charm, setprops are set, you just need to call a function again that triggers the component o re-render, it will trigger the componentDidUpdate function |
The latest update seems to broke this again. if (lifecycles.componentDidUpdate && typeof instance.componentDidUpdate === 'function' && (!state || (0, _Utils.shallowEqual)(state, _this3.instance().state))) {
instance.componentDidUpdate(prevProps, state, snapshot);
} The last condition make it that only state change will trigger instance.componentUpdate, |
@LiangMingChen could you file a new issue about this, and fill in the entire issue template? That would really help it get fixed quickly. |
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.
Sorry to bring an old thread back up, but I'm running into a similar issue detailed here, and I can't seem to find any other solutions when searching. Here's my test:
When I uncomment out |
@Jmac1523 try adding |
@ljharb I tried, unfortunately to no avail. Is the test less-effective or even ineffective with |
@Jmac1523 can you file a new issue? |
sure, thanks for getting back to me! |
@Jmac1523 that instance call will only work on state based components. I suppose lifecycle methods only exist in those but then there is the challenge of testing the useEffect method (on update, render, etc.). IMHO there needs to be some support for this from the Enzyme team. shallow doesn't buy you much as Enzyme shallow isn't going to be tied into your lifecycle methods. |
Err...I know, I never said I was using Hooks |
@Jmac1523 I realize that but with so many components being developed with hooks and moving away from state based it's worth noting. If you find any leads on this let me know. I'm doing some research on a state based approach now. This is something that needs attention from the Enzyme team. |
@Jmac1523 and everyone else. This solution seems to work nicely for testing state-based component lifecycle methods:
This is hitting that component during runtime: Ultimately what I'm doing here is setting the table (JSX) on the DOM then just loading in a table with modified properties. Here's the table in Jest:
|
When using
shallow
, the.setProps()
method doesn't trigger the componentDidUpdate lifecycle method. However,.setState()
does trigger it. It seems like they both should (or both shouldn't).simple example
The text was updated successfully, but these errors were encountered: