簡體   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