e.preventDefault is not a function - Jest React

此生再无相见时 提交于 2019-12-13 04:33:45

问题


Trying to test the following onChange event: JEST / ENZYME for React JS

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

I saw this as an answer on StackOverflow and I tried to apply towards my test

const event = Object.assign(jest.fn(), {preventDefault: () => {}})
wrapper.find('#individual-series').simulate('change',event)

But once that is done, a new error pops up which : it wont "read property value " assigned to the method

Here is the method

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

Sorry for too many codelines but goal is to get this onCHange event to pass and understand how to test when theres a prevetDefault() inside of a method.

Thanks

来源:https://stackoverflow.com/questions/55185000/e-preventdefault-is-not-a-function-jest-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!