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
Reactjs 反应+;Redux可观测超时测试_Reactjs_Rxjs_Redux Observable - Fatal编程技术网

Reactjs 反应+;Redux可观测超时测试

Reactjs 反应+;Redux可观测超时测试,reactjs,rxjs,redux-observable,Reactjs,Rxjs,Redux Observable,我正在使用React和Redux观察值创建一个web应用程序,我想为我的一个epics的超时场景创建一个单元测试 以下是史诗: export const loginUserEpic = (action$: ActionsObservable<any>, store, { ajax, scheduler }): Observable<Action> => action$.pipe( ofType<LoginAction>(LoginAction

我正在使用React和Redux观察值创建一个web应用程序,我想为我的一个epics的超时场景创建一个单元测试

以下是史诗:

export const loginUserEpic = (action$: ActionsObservable<any>, store, { ajax, scheduler }): Observable<Action> =>
  action$.pipe(
    ofType<LoginAction>(LoginActionTypes.LOGIN_ACTION),
    switchMap((action: LoginAction) =>
      ajax({
        url,
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: { email: action.payload.username, password: action.payload.password },
      }).pipe(
        timeout(timeoutValue, scheduler),
        map((response: AjaxResponse) => loginSuccess(response.response.token)),
        catchError((error: Error) => of(loginFailed(error))),
      ),
    ),
  );
export const loginUserEpic=(action$:ActionsObservable,store,{ajax,scheduler}):可观察=>
动作$.pipe(
ofType(LoginActionTypes.LOGIN\u操作),
开关映射((操作:登录操作)=>
阿贾克斯({
网址,
方法:“POST”,
标题:{'Content-Type':'application/json'},
正文:{电子邮件:action.payload.username,密码:action.payload.password},
}).烟斗(
超时(超时值,调度程序),
map((response:AjaxResponse)=>loginsAccess(response.response.token)),
catchError((错误:错误)=>of(登录失败(错误)),
),
),
);
这是我的测试:

  it('should handle a timeout error', () => {
    // My current timeout value is 20 millseconds
    const inputMarble = '------a';
    const inputValues = {
      a: login('fake-user', 'fake-password'),
    };

    const outputMarble = '--b';
    const outputValues = {
      b: loginFailed(new Error('timeout')),
    };

    const ajaxMock = jest.fn().mockReturnValue(of({ response: { token: 'fake-token' } }));
    const action$ = new ActionsObservable<Action>(ts.createHotObservable(inputMarble, inputValues));
    const outputAction = loginUserEpic(action$, undefined, { ajax: ajaxMock, scheduler: ts });

    // I am not sure what error to expect here...
    ts.expectObservable(outputAction).toBe(outputMarble, outputValues);
    ts.flush();
    expect(ajaxMock).toHaveBeenCalled();
  });
it('应该处理超时错误',()=>{
//我当前的超时值是20毫秒
常量inputMarble='----a';
常量输入值={
a:登录(“假用户”、“假密码”),
};
常量输出大理石='--b';
常量输出值={
b:登录失败(新错误(‘超时’),
};
const ajaxMock=jest.fn().mockReturnValue(of({response:{token:'fake token'}));
const action$=新的ActionsObservable(ts.createHotObservable(inputMarble,inputValues));
const outputAction=loginUserEpic(action$,未定义,{ajax:ajaxMock,scheduler:ts});
//我不确定这里会发生什么错误。。。
ts.expectObservable(outputAction).toBe(outputMarble,outputValues);
t.flush();
expect(ajaxMock).tohavebeincall();
});
我所期望的是Epic会抛出一个超时错误,因为我的超时值是20ms,而观测者在发出一个值之前会延迟60ms。然后,我将接受这个错误,并在最后进行比较,以使测试通过


很遗憾,没有引发超时错误。我做错了吗?

调用
ajax()
后,您在链中使用了
timeout
,它只返回({…})的
ajaxMock
),因此
超时
不会被触发,因为
会立即发出

如果要测试
超时
运算符,您需要将
延迟
添加到
ajaxMock

const ajaxMock = () => of({ response: { token: 'fake-token' } }).pipe(delay(30, ts));
这是您的演示:

如果登录请求从
60
开始,并且您添加了
30
延迟,那么在
80
您将得到一个TimeoutError

[0: Object
  frame: 80
  notification: Notification
  error: undefined
  hasValue: true
  kind: "N"
  value: Object
    error: Error
      message: "Timeout has occurred"
      name: "TimeoutError"
]