Reactjs 如何编写HOC-in-jest的测试&;酶
我们的应用程序目前使用react路由器和HOCs在或系统中创建身份验证。我已经习惯于为没有额外身份验证层的组件编写测试,但对于如何处理我编写的代码感到困惑。我一直在寻找专门关于react路由器身份验证的文档,我被卡住了。处理react路由器中的HOC的最佳方法是什么 index.jsReactjs 如何编写HOC-in-jest的测试&;酶,reactjs,testing,jestjs,enzyme,react-router-v4,Reactjs,Testing,Jestjs,Enzyme,React Router V4,我们的应用程序目前使用react路由器和HOCs在或系统中创建身份验证。我已经习惯于为没有额外身份验证层的组件编写测试,但对于如何处理我编写的代码感到困惑。我一直在寻找专门关于react路由器身份验证的文档,我被卡住了。处理react路由器中的HOC的最佳方法是什么 index.js const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore); const store = createStoreWithMi
const createStoreWithMiddleware = applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);
const token = localStorage.getItem('token');
if (token) {
store.dispatch({ type: AUTH_USER });
}
ReactDOM.render(
<Provider store={store}>
<App id={token} />
</Provider>,
document.querySelector('.app')
);
const createStoreWithMiddleware=applyMiddleware(reduxThunk)(createStore);
const store=createStoreWithMiddleware(reducers);
const token=localStorage.getItem('token');
如果(令牌){
dispatch({type:AUTH_USER});
}
ReactDOM.render(
,
document.querySelector(“.app”)
);
App.js
function PrivateRoute({ component: Component, auth, ...rest }) {
return (
<Route
{...rest}
render={props =>
auth ? (<div><Component {...props} /></div>) : (
<Redirect to={{ pathname: '/login', state: { from: props.location } }} />
)
}
/>
);
}
function PublicRoute({ component: Component, auth, ...rest }) {
return <Route {...rest} render={props => (!auth ? <Component {...props} /> : <Redirect to="/dashboard" />)} />;
}
class App extends Component {
static contextTypes = {
router: PropTypes.object,
};
render() {
return (
<Router history={history} >
<Switch>
<PublicRoute auth={this.props.auth} path="/login" exact component={Login} />
<PrivateRoute auth={this.props.auth} path="/dashboard" exact component={Dashboard} />
</Switch>
</Router>
);
}
}
function mapStateToProps(state) {
return {
auth: state.auth.authenticated,
};
}
export default connect(
mapStateToProps,
null
)(App);
函数privaterout({component:component,auth,…rest}){
返回(
作者?():(
)
}
/>
);
}
函数PublicRoute({component:component,auth,…rest}){
返回(!auth?:)}/>;
}
类应用程序扩展组件{
静态上下文类型={
路由器:PropTypes.object,
};
render(){
返回(
);
}
}
函数MapStateTops(状态){
返回{
auth:state.auth.authenticated,
};
}
导出默认连接(
MapStateTops,
无效的
)(App);