简体   繁体   English

开玩笑 onChange 单元测试 - React

[英]jest onChange Unit test - React

I have this function onChange for which i write to a unit test.我有这个函数 onChange 我写了一个单元测试。

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

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

I have tried with我试过了

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 });
    });

It gives error “simulate” is meant to be run on 1 node.它给出了错误“模拟”意味着要在 1 个节点上运行。 0 found instead." How do i test it?找到了 0。”我如何测试它?

In this unit test you shouldn't find the input if you render the Checkbox.在这个单元测试中,如果你渲染复选框,你不应该找到输入。 You should make it like this:你应该这样:

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

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

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