Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';标题';未定义的_Javascript_Reactjs_Jestjs - Fatal编程技术网

Javascript TypeError:无法读取属性';标题';未定义的

Javascript TypeError:无法读取属性';标题';未定义的,javascript,reactjs,jestjs,Javascript,Reactjs,Jestjs,尝试测试是否正确调用了以下函数 以下是使用Jest和Ezyme进行React Js的函数: changeCurrentColumn = (e) => { { if(e.target.value !== 'default') { const id = e.target.value const name = this.props.defaultData[e.target.value].caption this.props.setCurrentColu

尝试测试是否正确调用了以下函数

以下是使用Jest和Ezyme进行React Js的函数:

changeCurrentColumn = (e) => {
 {
   if(e.target.value !== 'default')
   {
     const id = e.target.value
     const name = this.props.defaultData[e.target.value].caption
     this.props.setCurrentColumn({ name, id })
   }
 }
}
以下是测试:

it('changeCurrentColumn function test', () => {
 wrapper.setProps({
   defaultData:[{
      caption:"test"
   },],
 }),
 wrapper.update();
错误指向
({target:'test'}))

expect(wrapper.instance().changeCurrentColumn({target:'test'})).toBeDefined(); 
})
我可以在“test”之后添加什么来读取“caption”的属性


谢谢。

您可以将defaultData的类型更改为这样的对象

it('changeCurrentColumn function test', () => {
    const event = {
        target: {
            value: "caption"
        }
    }
    wrapper.setProps({
      defaultData:{
         caption:"test"
      },
    }),
    wrapper.update();
    expect(wrapper.instance().changeCurrentColumn(event)).toBeDefined()
});
或者将值传递为0,以从现有的defaultData获取rest

it('changeCurrentColumn function test', () => {
    const event = {
        target: {
            value: 0
        }
    }
    wrapper.setProps({
      defaultData:[{
         caption:"test"
      }],
    }),
    wrapper.update();
    expect(wrapper.instance().changeCurrentColumn(event)).toBeDefined()
});

为什么要在
}
之后添加coma我的意思是
wrapper.setProps({defaultData:[{caption:“test”},//这个
不应该是
changeCurrentColumn({target:{value:'test'}})
?另外,你的props.defaultData是一个数组,所以props.defaultData[e.target.value]仅当e.target.value是一个数字时才有效。因为您将它与“默认值”进行比较,所以它似乎不是一个数字…@iLiA拼写错误,忘了删除it@Vinicius正确地说,您正在使用props.defaultData[e.target.value],这将仅适用于对象(括号表示法)\