尝试使用Jest测试textinput组件上的模拟更改

时间:2020-04-09 15:36:27

标签: reactjs testing jestjs components enzyme

我正在尝试模拟组件(ListPage)上的更改事件

 <TextInput
        className="search-s"
        id="textM"
        width="m"
        type="search"
        handleChange={this.props.updateS}
        placeholder="Search for a scenario"
      />
    </div>

handleChange属性调用一个名为updateS的prop函数,该函数看起来像

 updateS(e) {
     this.setState({
      name: e.target.value,
    }); 
  }

以及我目前拥有的测试功能

 it("should call handleChange on change with the correct params", () => {
   const wrapper = shallow(<ListPage />);
   const spy = jest.spyOn(wrapper.instance(), "handleChange");
   wrapper.instance().forceUpdate();

   const p = wrapper.find(".search-s");
   p.simulate("change");
   expect(spy).toHaveBeenCalled();
 });

由于某种原因,当我尝试模拟更改并检查是否调用了updateSearch函数时,我的测试功能不起作用。所有在线指南均包含组件内测试功能的示例,但没有传递的道具,我认为这是导致问题的原因。任何见识都很棒

0 个答案:

没有答案
相关问题