Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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 模拟酶中函数的返回_Javascript_Reactjs_Enzyme - Fatal编程技术网

Javascript 模拟酶中函数的返回

Javascript 模拟酶中函数的返回,javascript,reactjs,enzyme,Javascript,Reactjs,Enzyme,我有这个功能: addTheItem = item => { return this.itemAlreadyAdded(item) ? this.setState({errorMessageShown: true}) : this.props.addItem(item) } 如何模拟此函数(位于我的react组件中,即不是道具)以返回true或false,这样我就可以测试三元函数了?类似的内容可以让您测试此函数: import Component from './Comp

我有这个功能:

addTheItem = item => {
     return this.itemAlreadyAdded(item) ? this.setState({errorMessageShown: true}) : this.props.addItem(item)
   }

如何模拟此函数(位于我的react组件中,即不是道具)以返回true或false,这样我就可以测试三元函数了?

类似的内容可以让您测试此函数:

import Component from './Component'
....
const wrapper = Shallow(Component);
expect(wrapper.instance().addTheItem()).toBe(...) // Jest (or whatever you choose)
您可以这样检查状态:

const wrapper = Shallow(Component);
expect(wrapper.state('errorMessageShown').toBeTruthy() // When this.itemAlreadyAdded(item) returns true.
  • 对嵌套组件使用酶装入