1

反应很新,我正在使用反应进行表单验证。我尝试测试下面定义的 createTask():

renderForm(handleSubmit) {
return (
  <form
    onSubmit={handleSubmit}
  >
    <div className="task-details">
        {(this.createTask()}
    </div>
  </form>);
}

render() {
return (
  <Form
    onSubmit={this.handleApplyClick}
    render={this.renderForm}
    validate={this.validateForm}
  />
 );
}

如何模拟 renderForm 以便我可以测试 createTask 已被调用?jest.fn() 对我不起作用,我做了如下

 beforeEach(() => {
   testPanel = mountWithIntl(<TestPanel/>);
  });
 it('calls the createHeader', () => {
   testPanel.find('panelName').instance().renderForm = jest.fn();
   testPanel.find('panelName').instance().render();
   testPanel.update();
   expect(testPanel.find('panelName').instance().createHeader)
   .toHaveBeenCalled();
});

提前致谢

4

0 回答 0