Javascript 如何测试未传递到包装组件中的React HOC函数?
我有一个类似这样的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.
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();