繁体   English   中英

开玩笑 onChange 单元测试 - React

[英]jest onChange Unit test - React

我有这个函数 onChange 我写了一个单元测试。

<Checkbox
          onChange={ () => {
                if (this.props.showTab) {
                      this.showBox();
                   }
                  this.props.handleChange();
        }}/>

showBox() {
        this.setState({ showBox: true });
    }

我试过了

it('onChange', () => {
        const component = mount(<View
            showTab={true}
            handleChange= {handleChange}
            />);
        const instance = component.instance();
        const mockSetStateFn = jest.fn();
        instance.setState = mockSetStateFn;
        component.find('input').at(0).simulate('change');
        component.instance().showBox();
        expect(instance.setState).toHaveBeenCalledWith({ showBox: true });
    });

它给出了错误“模拟”意味着要在 1 个节点上运行。 找到了 0。”我如何测试它?

在这个单元测试中,如果你渲染复选框,你不应该找到输入。 你应该这样:

component.find(Checkbox).invoke('onChange')()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM