试图测试以下onChange事件:React JS的JEST / ENZYME
it(" Render method : test 1st onChange event :", () => {
baseProps.onChange.mockClear();
wrapper.setState({
localState:{}
});
wrapper.find('#individual-series').simulate('change',{target:{value:""}})
wrapper.update()
wrapper.find('#individual-series').simulate('change', event)
expect(wrapper.state().localState).toEqual('test');
expect(baseProps.addNewSeries).toHaveBeenCalled();
});
我认为这是StackOverflow的答案,我试图将其应用于我的测试
const event = Object.assign(jest.fn(), {preventDefault: () => {}})
wrapper.find('#individual-series').simulate('change',event)
但是一旦完成,就会弹出一个新错误:它将不会“读取属性值”分配给该方法
这是方法
handleChange = (e) => {
if(this.props.currentChartValues.series.map(function(e) { return e.id; }).indexOf(e.target.value) > -1)
{
let options = {
type: toast.TYPE.ERROR,
position: toast.POSITION.TOP_LEFT
}
toast( <div><p>Column already selected.</p><i>Error Code: 001</i></div>, options )
}
else {
e.preventDefault()
let localState = {}
localState[e.target.name] = e.target.value
localState['id'] = this.props.id
if (this.props.defaultData) {
localState = Object.assign({}, this.props.defaultData, localState)
} else {
localState = Object.assign({}, this.state, localState)
}
if (localState.analyzedColumn != null) {
if(localState.axis == null){
localState['axis'] = '0';
}
if(localState.charttype == null){
localState['charttype'] = this.props.currentChartValues.type;
}
很抱歉,代码行太多,但目标是让此onCHange事件通过并了解如何测试方法内部是否存在prevetDefault()。
谢谢