我有这个使用getDerivedStateFromProps
新生命周期的简单代码:
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
if (nextProps.value !== prevState.value) {
console.log('hello');
return {
value: nextProps.value
};
}
return null;
}
这是测试:
it('should call getDerivedStateFromProps', () => {
const instance = shallow(mockComponent());
instance.setProps({ value: 'test2' });
expect(instance.state.value).toEqual('test2');
});
但我有这个错误,但我知道这是因为 console.log() 调用。
Expected value to equal:
"test2"
Received:
undefined
我如何正确测试getDerivedStateFromProps
?
我正在使用:
react: 16.4
react-Dom: 16.4
enzyme-adapter-react-16: 1.1.1
react-test-renderer: 16.4.1