1

我正在尝试测试“redux observable epic”,但测试失败,因为并非所有操作都在 store.getActions() 中,奇怪的是 store.dispatch 函数运行。

史诗和动作

export const VERIFY_SESION = 'auth/VERIFY_SESION';
export const SET_POLICIES_ACCEPTED = 'auth/SET_POLICIES_ACCEPTED';
export const AUTHENTICATE = 'auth/AUTHENTICATE';
export function setPoliciesAccepted(wereAccepted: boolean) {
  return {
    wereAccepted,
    type: SET_POLICIES_ACCEPTED,
  };
}

export function verifySesion() {
  return {
    type: VERIFY_SESION,
  };
}

export function authenticate(token) {
  return {
    token,
    type: AUTHENTICATE,
  };
}

export function verifySesionEpic(action$, store) {
  return action$
    .ofType(VERIFY_SESION)
    .switchMap(async () => {
      try {
        store.dispatch(setBlockLoading(true));
        const token = await AsyncStorage.getItem('token');
        if (token !== null) {
          store.dispatch(setBlockLoading(false));
          return authenticate(token);
        }
        const policiesWereAccepted = await AsyncStorage.getItem('policiesWereAccepted');
        store.dispatch(setBlockLoading(false));
        return setPoliciesAccepted(policiesWereAccepted);
      } catch (error) {
        return setMessage(error.message);
      }
    });
}

测试

describe('actions/auth', () => {
  let store;
  const asyncStorageGetStub = stub(AsyncStorage, 'getItem');

  beforeEach(() => {
    store = mockStore();
  });

  afterEach(() => {
    asyncStorageGetStub.restore();
  });

  it('Should call authenticate if token', () => {
    const token = 'mitoken';
    asyncStorageGetStub.withArgs('token').returns(Promise.resolve(token));
    store.dispatch(verifySesion());
    expect(store.getActions()).toContain({ type: AUTHENTICATE, token });
  });
});

测试结果

1)“actions/auth 应该为 verifySesion 调用史诗:错误:预期 [{ type:'auth/VERIFY_SESION'}] 包括 { token:'mitoken', type:'auth/AUTHENTICATE'}”

笔记

我确定条件令牌!== null pass

4

1 回答 1

0

我要在 getAction 之前添加超时,因为在之后添加了“AUTHENTICATE”操作。

it('Should call authenticate if token', (done) => {
    const token = 'mitoken';
    asyncStorageGetStub.withArgs('token').returns(Promise.resolve(token));
    store.dispatch(verifySesion());
    setTimeout(() => {
      expect(store.getActions()).toContain({ type: AUTHENTICATE, token });
      done();
    }, 1000);
  });
于 2017-01-06T14:33:51.797 回答