Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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

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 如何测试未传递到包装组件中的React HOC函数?_Javascript_Reactjs_React Hoc - Fatal编程技术网

Javascript 如何测试未传递到包装组件中的React HOC函数?

Javascript 如何测试未传递到包装组件中的React HOC函数?,javascript,reactjs,react-hoc,Javascript,Reactjs,React Hoc,我有一个类似这样的HOC,我想测试getStuff2函数: function withHoc(aComponent) { class hocClass extends React.Component { getStuff1 = () => { } getStuff2 = () => { } render() { return <aComponent {...this.props} getStuff1={this.

我有一个类似这样的HOC,我想测试
getStuff2
函数:

function withHoc(aComponent) {
  class hocClass extends React.Component {

    getStuff1 = () => {

    }

    getStuff2 = () => {

    }

    render() {
      return <aComponent {...this.props} getStuff1={this.getStuff1} />;
    }
}
}
我不想将
getStuff2
传递到包装的组件中,因为那里不需要它。它只需要作为
getStuff1
的助手方法,那么有没有一种方法可以访问该方法而不将其传递到包装的组件中?

(我假设您正在使用,因为您使用的是
shallow()

您可以使用
包装器.instance()

请参见

您是否正在使用?
class MockApp extends React.PureComponent {
  render() {
    return <p>Hello from your Mock App</p>;
  }
}

const MockWithHOC = withHoc(MockApp);

const wrapper = shallow(<MockWithHOC />);

it('Test getStuff2', () => {
    const result = wrapper.getStuff2();
});
<MockApp getStuff1={[Function]} />
const result = wrapper.instance().getStuff2();