Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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
Javascript Jest模拟服务变量.asObservable返回_Javascript_Reactjs_Unit Testing_Mocking_Jestjs - Fatal编程技术网

Javascript Jest模拟服务变量.asObservable返回

Javascript Jest模拟服务变量.asObservable返回,javascript,reactjs,unit-testing,mocking,jestjs,Javascript,Reactjs,Unit Testing,Mocking,Jestjs,我是一名初级开发人员,使用Jest作为单元测试框架开发React应用程序 我必须测试我的privateRoute文件: export const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => { const currentUser = authenticationService.currentUser;

我是一名初级开发人员,使用Jest作为单元测试框架开发React应用程序

我必须测试我的privateRoute文件:

export const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={props => {
      const currentUser = authenticationService.currentUser;
      if (!currentUser) {
        // not logged in so redirect to login page with the return url
        return (
          <Redirect to={{ pathname: "/", state: { from: props.location } }} />
        );
      }

      // authorized so return component
      return <Component {...props} />;
    }}
  />
);

使用
模块的
PrivateRoute
组件的单元测试解决方案

privaterout.tsx

从“React”导入React;
从“react router”导入{Route,Redirect};
从“/authenticationService”导入{authenticationService};
导出常量PrivateRoute=({component:component,…rest})=>(
{
const currentUser=authenticationService.currentUser;
如果(!currentUser){
返回;
}
返回;
}}
/>
);
authenticationService.ts

export const authenticationService={
当前用户:{},
};
privateRoute.test.ts

从“React”导入React;
从“/PrivateRoute”导入{PrivateRoute};
从“酶”导入{mount,shallow};
从“react Router”导入{MemoryRouter,Redirect,Router};
从“/authenticationService”导入{authenticationService};
描述('59825407',()=>{
它('如果当前用户存在,则应呈现组件',()=>{
const-mProps={component:jest.fn().mockReturnValueOnce(null)};
常量包装器=装入(
,
);
expect(wrapper.find(mProps.component.props()).toEqual(
expect.objectContaining({
历史记录:expect.any(对象),
位置:任何(对象)除外,
匹配:expect.any(对象),
}),
);
});
它('如果当前用户不存在,则应重定向',()=>{
authenticationService.currentUser=未定义为任何;
const-mProps={component:jest.fn().mockReturnValueOnce(null),路径:'/user'};
常量包装器=装入(
,
);
const history=wrapper.find('Router').prop('history'),如有;
expect(history.location.state.from.pathname).toBe('/user');
expect(history.location.pathname).toBe('/');
});
});
100%覆盖率的单元测试结果:

通过src/stackoverflow/59825407/privaterout.test.tsx(16.491s)
59825407
✓ 如果当前用户存在,则应呈现组件(74ms)
✓ 如果当前用户不存在,则应重定向(12毫秒)
--------------------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
--------------------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
authenticationService.ts | 100 | 100 | 100 | 100 ||
privateRoute.tsx | 100 | 100 | 100 | 100 ||
--------------------------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:2次通过,共2次
快照:共0个
时间:18.683s
源代码:

const currentUserSubject = new BehaviorSubject(
  JSON.parse(localStorage.getItem("currentUser"))
);

export const authenticationService = {
  // ...
  currentUser: currentUserSubject.asObservable(),
  // ...
};