测试包含在 withRouter 中的 React 组件(最好使用 jest/enzyme)
Testing react component enclosed in withRouter (preferably using jest/enzyme)
我有一个 React 组件,它包含在高阶组件 withRouter 中,如下所示:
module.exports = withRouter(ManageProfilePage);
我的路线如下:
<Route path="/" component={AdrApp}>
<IndexRoute component={Login}/>
<Route component={CheckLoginStatus}>
<Route path="manage-profiles/:profileId" component=
{ManageProfilesPage}/>
</Route>
<Route path="*" component={notFoundPage}/>
</Route>
我需要使用一次 Router 生命周期方法,这就是为什么我需要 withRouter:
class ManageProfilePage extends React.Component {
componentDidMount() {
this.props.router.setRouteLeaveHook(this.props.route, () => {
...
})
render(){
...
}
}
我需要使用 Jest/Enzyme 测试这个组件,我编写了如下测试用例:
describe('manage profile page test suite', () => {
it('snapshot test', () => {
const setRouteLeaveHook =jest.fn();
let wrapper = shallow(
<ManageProfilePage params={{id : 25, router:
setRouteLeaveHook}}/>
);
expect(wrapper).toMatchSnapshot();
})
})
问题是它没有渲染一层深度。我正在粘贴下面的快照:
exports[`manage drug term page test suites snapshot test 1`] = `
<ManageProfilePage
params={
Object {
"id": 25,
"router": [Function],
}
}
/>
`;
是否有任何不同的方法可以编写我的测试用例,以便我能够呈现 ManageProfilePage 至少 1 级深度?它无法呈现,因为它包含在 WithRouter 中?我们如何测试这些类型的组件?
浅渲染只会渲染一层,这是它的规范的一部分。
您可以使用 Mount 渲染整棵树,但我认为您不能限制它渲染的层数。
在任何情况下,当使用高阶组件时,我通常也只导出基本组件(在包装它之前),这样我就可以在没有包装器的情况下进行所有测试,并简单地为所需的提供者传递模拟。
与带有 redux 的 Connect
组件相同,您导出常规组件并在其上测试不同的 props,而不是连接的 props。
另请注意,某些 with...
包装器不会公开内部实例(有些会,但有些不会),因此在您自己的组件而不是包装器上进行测试也有帮助。
通常情况下,如果我们尝试测试此类组件,我们将无法呈现它,因为它被包装在 WithRouter 中(WithRouter 是组件的包装器,它提供
路由器道具,如匹配、路由和历史记录,可直接在组件中使用)。
module.exports = withRouter(ManageProfilePage);
要呈现此类组件,我们必须明确告诉它使用 WrappedComponent 关键字呈现包装的组件。
对于例如。我们将使用以下代码进行快照测试:
describe('manage profile page test suite', () => {
it('snapshot test', () => {
const setRouteLeaveHook =jest.fn();
let wrapper = shallow(
<ManageProfilePage.WrappedComponent params={{id : 25, router:
setRouteLeaveHook}}/>
);
expect(wrapper).toMatchSnapshot();
})
})
这将告诉 enzyme 对 ManageProfilePage 进行浅渲染(浅渲染只渲染特定组件并跳过子组件),ManageProfilePage 是 WithRouter 中的包装组件。
我做了什么解决了这个问题:
在"this.props.match.params.id"中使用了post组件
在测试用例中
const miniProps = {
otherProps,
match:{params:{id:'112'}}
};
const wrapper = shallow();
我想你应该试试这个
describe('manage profile page test suite', () => {
it('snapshot test', () => {
let wrapper = shallow(
<ManageProfilePage.WrappedComponent/>
);
expect(wrapper).toMatchSnapshot();
})
})
我有一个 React 组件,它包含在高阶组件 withRouter 中,如下所示:
module.exports = withRouter(ManageProfilePage);
我的路线如下:
<Route path="/" component={AdrApp}>
<IndexRoute component={Login}/>
<Route component={CheckLoginStatus}>
<Route path="manage-profiles/:profileId" component=
{ManageProfilesPage}/>
</Route>
<Route path="*" component={notFoundPage}/>
</Route>
我需要使用一次 Router 生命周期方法,这就是为什么我需要 withRouter:
class ManageProfilePage extends React.Component {
componentDidMount() {
this.props.router.setRouteLeaveHook(this.props.route, () => {
...
})
render(){
...
}
}
我需要使用 Jest/Enzyme 测试这个组件,我编写了如下测试用例:
describe('manage profile page test suite', () => {
it('snapshot test', () => {
const setRouteLeaveHook =jest.fn();
let wrapper = shallow(
<ManageProfilePage params={{id : 25, router:
setRouteLeaveHook}}/>
);
expect(wrapper).toMatchSnapshot();
})
})
问题是它没有渲染一层深度。我正在粘贴下面的快照:
exports[`manage drug term page test suites snapshot test 1`] = `
<ManageProfilePage
params={
Object {
"id": 25,
"router": [Function],
}
}
/>
`;
是否有任何不同的方法可以编写我的测试用例,以便我能够呈现 ManageProfilePage 至少 1 级深度?它无法呈现,因为它包含在 WithRouter 中?我们如何测试这些类型的组件?
浅渲染只会渲染一层,这是它的规范的一部分。
您可以使用 Mount 渲染整棵树,但我认为您不能限制它渲染的层数。
在任何情况下,当使用高阶组件时,我通常也只导出基本组件(在包装它之前),这样我就可以在没有包装器的情况下进行所有测试,并简单地为所需的提供者传递模拟。
与带有 redux 的 Connect
组件相同,您导出常规组件并在其上测试不同的 props,而不是连接的 props。
另请注意,某些 with...
包装器不会公开内部实例(有些会,但有些不会),因此在您自己的组件而不是包装器上进行测试也有帮助。
通常情况下,如果我们尝试测试此类组件,我们将无法呈现它,因为它被包装在 WithRouter 中(WithRouter 是组件的包装器,它提供 路由器道具,如匹配、路由和历史记录,可直接在组件中使用)。 module.exports = withRouter(ManageProfilePage);
要呈现此类组件,我们必须明确告诉它使用 WrappedComponent 关键字呈现包装的组件。 对于例如。我们将使用以下代码进行快照测试:
describe('manage profile page test suite', () => {
it('snapshot test', () => {
const setRouteLeaveHook =jest.fn();
let wrapper = shallow(
<ManageProfilePage.WrappedComponent params={{id : 25, router:
setRouteLeaveHook}}/>
);
expect(wrapper).toMatchSnapshot();
})
})
这将告诉 enzyme 对 ManageProfilePage 进行浅渲染(浅渲染只渲染特定组件并跳过子组件),ManageProfilePage 是 WithRouter 中的包装组件。
我做了什么解决了这个问题:
在"this.props.match.params.id"中使用了post组件
在测试用例中
const miniProps = { otherProps, match:{params:{id:'112'}} }; const wrapper = shallow();
我想你应该试试这个
describe('manage profile page test suite', () => {
it('snapshot test', () => {
let wrapper = shallow(
<ManageProfilePage.WrappedComponent/>
);
expect(wrapper).toMatchSnapshot();
})
})