我有一個陣營部件被封裝高階元器件withRouter內如下:測試反應括在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>
我需要的路由器生命週期方法一次使用,這就是爲什麼我需要withRouter:
class ManageProfilePage extends React.Component {
componentDidMount() {
this.props.router.setRouteLeaveHook(this.props.route,() => {
...
})
render(){
...
}
}
我需要測試用玩笑/酶此組件,我寫了下面的測試案例:
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 ATLEAST 1級深,我可以寫我的測試案例?它無法渲染,因爲它被封閉在WithRouter中?我們如何測試這些類型的組件?
問題是淺渲染未呈現,甚至沒有1級深在我的情況 –
我覺得是,你不知道這是怎麼回事在withRouter究竟裏面 - 這是'被渲染IS',它只是沒有想到你我還是建議你測試自己的組件,而不是包裝 – Patrick
我的意思是不是在組件級渲染 - 當然它會在withRouter水平呈現什麼。 –