有没有办法检查React组件中的路由数量?

问题描述 投票:0回答:1

假设你在React中有这样的路由器设置,有没有办法在使用Jest和Enzyme编写测试时检查/获取该组件中的路由数量?在这个例子中,它应该返回3,因为PrivateRoute在引擎盖下使用Route。我不是在考虑使用外部插件

<Router history={history}>
    <Switch>
        <PrivateRoute exact path='/' component={Component1} />
        <Route path='/register' component={Component2} />
        <Route path='/login' component={Component3} />
    </Switch>
</Router>
javascript reactjs
1个回答
0
投票

也许你可以尝试这样的事情:

import React from 'react';
import { shallow } from 'enzyme';
import { Route } from 'react-router-dom';
import MyCompo from '../MyCompo';
import PrivateRoute fom '../PrivateRoute';

describe('<MyCompo />', () => {
  it('should have 3 routes', () => {
    const renderedCompo = shallow(<MyCompo />);

    expect(renderedCompo.find(Route)).toHaveLength(2);
    expect(renderedCompo.find(PrivateRoute)).toHaveLength(1);
  })
})
© www.soinside.com 2019 - 2024. All rights reserved.