我有以下测试:
describe('Form', () => {
let store;
let wrapper;
beforeEach(() => {
store = mockStore(mockData);
wrapper = mount(
<Provider store={store}>
<Form />
</Provider>
);
});
it('handleForm calls uses validate() for validation', () => {
const instance = wrapper.instance();
const submitFormButton = wrapper.find('.submitFormButton');
submitFormButton.simulate('click');
console.log(instance); // null
});
});
知道我到底做错了什么吗?
我知道 enzyme 有这个东西:
NOTE: With React 16 and above, instance() returns null for stateless functional components.
但是我的功能组件确实有一个状态,我正在使用钩子(Hook)(如果这会改变任何东西),并且应该有某种方法仍然可以访问 instance.componentMethod()
,对吗?
最佳答案
NOTE: With React 16 and above, instance() returns null for stateless functional components.
对于无状态组件
,它们实际上意味着功能组件。 instance()
方法是为基于类的组件保留的。
因此您可以将它用于此组件:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
但不适用于这个:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
关于reactjs - enzyme 实例()返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60561427/