是否可以模拟延迟?

时间:2017-06-30 09:32:40

标签: reactjs jest redux-observable

我正在进行一次史诗测试。问题?我用延迟。见

export const displayToasterEpic = (action$, store) =>
  action$
    .ofType(SHOW_TOASTER)
    .filter(action => action.toaster.show)
    .delay(getToaster(store.getState()).duration || DELAY_TOASTER)
    .map(() => showToaster(MESSAGE_CLOSE));

我的测试:

  describe('displayToasterEpic', () => {
    it('should display toaster delete account', () => {
      const expectedAction1 = {
        type: SHOW_TOASTER,
        toaster: MESSAGE_DELETE_ACCOUNT,
      };
      const expectedAction2 = {
        type: SHOW_TOASTER,
        toaster: MESSAGE_CLOSE,
      };

      store = mockStore(displayToasterEpic);

      store.dispatch(expectedAction1);

      expect(store.getActions()).toEqual([expectedAction1, expectedAction2]);
    });

问题是这种延迟。 jest.clearAllTimers();

时出现同样的问题

enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我发现自己是模拟延迟的漂亮解决方案。使用方法spyOn:)