You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling setProps on a shallow wrapper may result in not calling componentDidUpdate of the wrapped component. It's the case when the state depends on a prop and when that specific setProps call does change the state. If that call doesn't change the state, componentDidUpdate is called. I suspect this is a regression of #2007. It worked fine in version 3.8.0
To reproduce
import{shallow}from'enzyme';import*asReactfrom'react';classDummyCompextendsReact.PureComponent{constructor(props){super(props);this.state={stateCounter: 0};}staticgetDerivedStateFromProps(nextProps){// set stateCounter if asked forif(nextProps.propCounter>-1){return{stateCounter: nextProps.propCounter};}returnnull;}componentDidUpdate(){console.log('Counter:',this.state.stateCounter);}render(){return<p>Counter: {this.state.stateCounter}</p>;}}describe('Counter',()=>{it('should call cDU on setProps',()=>{constwrapper=shallow(<DummyComppropCounter={0}/>);constcDUSpy=jest.spyOn(wrapper.instance(),'componentDidUpdate');// does not change the state, so cDU will be calledwrapper.setProps({propCounter: -1});expect(cDUSpy).toBeCalledTimes(1);// does change the state, cDU should be called anywaywrapper.setProps({propCounter: 1});expect(cDUSpy).toBeCalledTimes(2);// this test will fail});});
Your environment
API
shallow
mount
render
Version
library
version
enzyme
3.9.0
react
16.8.2
react-dom
16.8.2
react-test-renderer
16.8.2
adapter (below)
Adapter
enzyme-adapter-react-16
enzyme-adapter-react-16.3
enzyme-adapter-react-16.2
enzyme-adapter-react-16.1
enzyme-adapter-react-15
enzyme-adapter-react-15.4
enzyme-adapter-react-14
enzyme-adapter-react-13
enzyme-adapter-react-helper
others ( )
The text was updated successfully, but these errors were encountered:
Replicated the above test case, which results in the aforementioned regression. With regards to the fix and the previous discussions from 1862, is it possible to apply a fix without implementing batched updates? @ljharb any pointers would be appreciated
Calling
setProps
on a shallow wrapper may result in not callingcomponentDidUpdate
of the wrapped component. It's the case when the state depends on a prop and when that specificsetProps
call does change the state. If that call doesn't change the state,componentDidUpdate
is called. I suspect this is a regression of #2007. It worked fine in version 3.8.0To reproduce
Your environment
API
Version
Adapter
The text was updated successfully, but these errors were encountered: