Reactjs 在react路由器dom中测试privaterout
react路由器dom文档中私有路由的实现Reactjs 在react路由器dom中测试privaterout,reactjs,react-router,react-router-v4,react-router-dom,react-router-redux,Reactjs,React Router,React Router V4,React Router Dom,React Router Redux,react路由器dom文档中私有路由的实现 function PrivateRoute({ authenticated, ownProps }) { let {component:Component, ...rest} = ownProps //PrivateRoute, If not authenicated ie false, redirect return ( <Route // JSX Spread sttributes
function PrivateRoute({ authenticated, ownProps }) {
let {component:Component, ...rest} = ownProps
//PrivateRoute, If not authenicated ie false, redirect
return (
<Route
// JSX Spread sttributes to get path for Route
{...rest}
render={() => authenticated ? (
<Component />
) :
<Redirect
to={{pathname: "/" }}
/>
}
/>
);
}
export default PrivateRoute
函数PrivateRoute({authenticated,ownProps}){
让{component:component,…rest}=ownProps
//PrivateRoute,如果未授权,即为false,则重定向
返回(
认证(
) :
}
/>
);
}
导出默认PrivateRoute
PrivateRoute是从Redux存储获取身份验证状态的已连接组件
我正在尝试使用redux模拟存储和mount from Ezyme测试连接的组件
import configureStore from 'redux-mock-store'
const mockStore = configureStore()
const authStateTrue = {auth: {AUTHENTICATED: true}};
test('Private path renders a component when auntentication is true', () => {
const store = mockStore(authStateTrue)
const AComponent = () => <div>AComponent</div>
const props = {path:"/aprivatepath" ,component:<AComponent/>};
let enzymeWrapper = mount(<Provider store={store}>
<BrowserRouter>
<PrivateRoute path="/aprivatepath" component={AComponent}/>
</BrowserRouter>
</Provider>);
expect(enzymeWrapper.exists(AComponent)).toBe(true)
});
从“redux模拟存储”导入配置存储
const mockStore=configureStore()
const authStateTrue={auth:{AUTHENTICATED:true};
test('auntentication为true时私有路径呈现组件',()=>{
const store=mockStore(authStateTrue)
常量AComponent=()=>AComponent
const props={path:“/aprivatepath”,组件:
似乎传递给PrivateRoute的组件不存在,即使身份验证状态为true
如何测试PrivateRoute中呈现或重定向的组件。以下是单元测试解决方案:
privaterout.tsx
从“React”导入React;
从“react router”导入{Route,Redirect};
函数PrivateRoute({authenticated,ownProps}){
const{component:component,…rest}=ownProps;
返回(已验证?:)}/>;
}
导出默认私有路由;
privaterote.test.tsx
:
从“/PrivateRoute”导入PrivateRoute;
从“React”导入React;
从“酶”导入{mount};
从“react router”导入{MemoryRouter,Redirect};
描述('56730186',()=>{
它('如果用户已通过身份验证,则应呈现组件',()=>{
常量AComponent=()=>AComponent;
const props={path:'/aprivatepath',component:AComponent};
常数enzymeWrapper=mount(
,
);
expect(enzymeWrapper.exists(AComponent)).toBe(true);
});
它('如果用户未经身份验证,则应重定向',()=>{
常量AComponent=()=>AComponent;
const props={path:'/aprivatepath',component:AComponent};
常数enzymeWrapper=mount(
,
);
const history:any=enzymewapper.find('Router').prop('history');
expect(history.location.pathname).toBe('/');
});
});
100%覆盖率的单元测试结果:
通过src/stackoverflow/56730186/privaterout.test.tsx(15.063s)
56730186
✓ 如果用户已通过身份验证,则应呈现组件(96ms)
✓ 如果用户未通过身份验证,则应重定向(23ms)
------------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
------------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
privateRoute.tsx | 100 | 100 | 100 | 100 ||
------------------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:共0个
时间:17:053s
源代码: