Javascript TypeError:wrapper.props(…).onClick不是函数

Javascript TypeError:wrapper.props(…).onClick不是函数,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,Foo.jsx 从“React”导入React; 导出默认值(道具)=>{ 常量{func}=props; 返回( func()}>单击 ); }; Foo.test.js 从“React”导入React; 输入酶,{shall}来自“酶”; 从'enzyme-Adapter-react-16'导入适配器; 从“/Foo”导入Foo; configure({adapter:newadapter()}); 描述('foo测试',()=>{ 常量道具={ func:jest.fn(), }; 测试

Foo.jsx

从“React”导入React;
导出默认值(道具)=>{
常量{func}=props;
返回(
func()}>单击
);
};
Foo.test.js

从“React”导入React;
输入酶,{shall}来自“酶”;
从'enzyme-Adapter-react-16'导入适配器;
从“/Foo”导入Foo;
configure({adapter:newadapter()});
描述('foo测试',()=>{
常量道具={
func:jest.fn(),
};
测试('testfoo',()=>{
常量包装器=浅()
expect(wrapper.props().onClick()).tohavebeencall();
});
});
当我运行测试覆盖率时,它显示
TypeError:wrapper.props(…).onClick不是一个函数
。根据文档
jest.fn()
返回一个函数。我的要求是需要在Foo.jsx的onClick处理程序上调用
func

任何帮助都将不胜感激。

要测试此场景,您需要模拟按钮的单击事件,并将道具基金附加到该按钮。因为它应该只在按钮上触发click事件时调用

test('test foo', () => {
    const wrapper = shallow(<Foo {...props} />);
    /** here the second argument to `simulate` is the event object, which is not required for your use-case. Still I kept it for future reference, if needed.
**/
    wrapper.find('button').simulate('click', {}); 
    expect(props.func).toHaveBeenCalled();
  });
test('testfoo',()=>{
常量包装器=浅();
/**这里'simulate'的第二个参数是事件对象,它不是您的用例所必需的。不过,如果需要的话,我保留它以备将来参考。
**/
find('button').simulate('click',{});
expect(props.func).tohavebeencall();
});

这将调用组件道具中接收到的
func
,并可通过上述预期进行验证。

要测试此场景,您需要模拟按钮的点击事件,将道具资金附加到该按钮上。因为它应该只在按钮上触发click事件时调用

test('test foo', () => {
    const wrapper = shallow(<Foo {...props} />);
    /** here the second argument to `simulate` is the event object, which is not required for your use-case. Still I kept it for future reference, if needed.
**/
    wrapper.find('button').simulate('click', {}); 
    expect(props.func).toHaveBeenCalled();
  });
test('testfoo',()=>{
常量包装器=浅();
/**这里'simulate'的第二个参数是事件对象,它不是您的用例所必需的。不过,如果需要的话,我保留它以备将来参考。
**/
find('button').simulate('click',{});
expect(props.func).tohavebeencall();
});
这将调用组件道具中接收的
func
,并可通过上述预期进行验证

test('test foo', () => {
    const wrapper = shallow(<Foo {...props} />);
    /** here the second argument to `simulate` is the event object, which is not required for your use-case. Still I kept it for future reference, if needed.
**/
    wrapper.find('button').simulate('click', {}); 
    expect(props.func).toHaveBeenCalled();
  });