Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何编写HOC-in-jest的测试&;酶_Reactjs_Testing_Jestjs_Enzyme_React Router V4 - Fatal编程技术网

Reactjs 如何编写HOC-in-jest的测试&;酶

Reactjs 如何编写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

我们的应用程序目前使用react路由器和HOCs在或系统中创建身份验证。我已经习惯于为没有额外身份验证层的组件编写测试,但对于如何处理我编写的代码感到困惑。我一直在寻找专门关于react路由器身份验证的文档,我被卡住了。处理react路由器中的HOC的最佳方法是什么

index.js

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);