使用 Enzyme.mount 时获取对 return null 的 `render` 调用
Getting `render` call to return null when using Enzyme.mount
问题
有谁知道在使用 Enzyme.mount
时将安装的组件安装到 return null
(或类似的东西)的可接受方法?
用例
我有一个包含在 n 高阶组件中的组件。我实际上想 mount
组件而不是 shallow
因为我想测试 componentDidMount
是否按预期调用了某些东西。
我的坐骑function/test是这样的:
const component = <ApplicationsShowRoute />;
const { store } = mountSetup(component);
expect(store.getActions()).toEqual([ { type: 'API_REQUEST' } ]);
当我安装它时,我宁愿不必担心正在渲染的内容(子组件中的错误等)。
代码
export class ApplicationsShowRouteRaw extends Component {
componentDidMount() {
const { uuid } = this.props.match.params;
this.props.fetchShowRoute(uuid);
}
props: Props
render() {
const { application, match } = this.props;
return isEmpty(application) || application.Uuid !== match.params.uuid ? (
<FullPageLoader />
) : (
<ApplicationsShow
application={application}
/>
);
}
}
export const mapStateToProps = (state: Object) => ({
application: getShowRoute(state),
});
export const mapDispatchToProps = (dispatch: Function) => ({
fetchShowRoute: (uuid: string) => dispatch(fetchShowRoute(uuid)),
});
export default connect(
mapStateToProps,
mapDispatchToProps,
)(ApplicationsShowRouteRaw);
如果您想要使用所有生命周期方法进行浅渲染,您可以使用 shallow
并将 lifecycleExperimental 选项设置为 true
。
问题
有谁知道在使用 Enzyme.mount
时将安装的组件安装到 return null
(或类似的东西)的可接受方法?
用例
我有一个包含在 n 高阶组件中的组件。我实际上想 mount
组件而不是 shallow
因为我想测试 componentDidMount
是否按预期调用了某些东西。
我的坐骑function/test是这样的:
const component = <ApplicationsShowRoute />;
const { store } = mountSetup(component);
expect(store.getActions()).toEqual([ { type: 'API_REQUEST' } ]);
当我安装它时,我宁愿不必担心正在渲染的内容(子组件中的错误等)。
代码
export class ApplicationsShowRouteRaw extends Component {
componentDidMount() {
const { uuid } = this.props.match.params;
this.props.fetchShowRoute(uuid);
}
props: Props
render() {
const { application, match } = this.props;
return isEmpty(application) || application.Uuid !== match.params.uuid ? (
<FullPageLoader />
) : (
<ApplicationsShow
application={application}
/>
);
}
}
export const mapStateToProps = (state: Object) => ({
application: getShowRoute(state),
});
export const mapDispatchToProps = (dispatch: Function) => ({
fetchShowRoute: (uuid: string) => dispatch(fetchShowRoute(uuid)),
});
export default connect(
mapStateToProps,
mapDispatchToProps,
)(ApplicationsShowRouteRaw);
如果您想要使用所有生命周期方法进行浅渲染,您可以使用 shallow
并将 lifecycleExperimental 选项设置为 true
。