这是我试图使用Jest和Enzyme测试的代码。 我想知道是否可以在不使用spyON的情况下测试其是否正常工作
toggleIsChecked = (e) => {
if (this.state.key && this.state.value && this.state.isChecked === false){
let localState = {}
localState['isChecked'] = true
let keyValuePairObj = Object.assign({}, this.state, localState)
keyValuePairObj['type'] = this.props.type
keyValuePairObj['id'] = this.props.id
if (this.props.subType) {
keyValuePairObj['subType'] = this.props.subType
}
this.props.addNewKeyValue(keyValuePairObj)
this.setState({
isChecked: !this.state.isChecked
})
} else {
this.setState({
isChecked: !this.state.isChecked
})
}
我上网查看了一个示例,得到的答案如下:
it("should call sort ids", () => {
const wrapper = shallow(<KeyValuePair />);
wrapper.instance().toggleIsChecked = jest.fn();
wrapper.update(); // Force re-rendering
wrapper.instance().componentDidMount();
expect(wrapper.instance().toggleIsChecked).toBeCalled();
});
此刻失败了,我知道上面的答案不正确。我该如何从先前的答案中改进并测试上面的代码以通过它
谢谢