我的组件看起来像这样
const FooBar = ({ onClose, }) => { return ( <div className='foo'> <Icon name='close' onClick={onClose} /> </div> ) }
我的测试结果如下
const mockFn = jest.fn(); const title = 'Title'; it('calls the onClose function', () => { const component = mount( <FooBar title={title} onClose={mockFn} /> ); component.instance().onClose(); expect(mockFn).toBeCalled(); component.unmount(); });
那回报
类型错误:无法读取空的属性“onClose”
我怎么打电话给 onClose 功能?
onClose
尝试
component.find(Icon).first().props().onClick();
另外,看看 react-testing-library 它使这种测试更容易。
react-testing-library