如何使用Jest测试Redux传奇?

时间:2018-11-08 05:26:16

标签: testing jestjs redux-saga

我不是前端开发人员,但是尝试为项目编写测试。 另外,我正在使用redux-saga和jest进行测试 顺便说一句,我有传奇故事:

export function* setAuthorizeCode(action) {
  try {
    let response = yield call(Api.token.getToken, action.payload.authorizeCode);

    yield put(appActions.setAccessToken(response.data.access_token));
    yield put(appActions.setIdToken(response.data.id_token));

    const accessToken = yield select((state) => appSelectors.getAccessToken(state.app));
    const idToken = yield select((state) => appSelectors.getIdToken(state.app));

    response = yield call(Api.user.getUser, accessToken, idToken);
    yield put(appActions.setUser(response.data));
  } catch (error) {
    console.log('Error: ', error);
  }
} 

function* setAuhtorizeCodeFlow () {
  yield takeLatest(appActions.SET_AUTHORIZE_CODE, setAuthorizeCode);
}

export default function* authentication() {
  yield all([
    setAuhtorizeCodeFlow()
  ]);
}

为此,我为此编写了测试:

test('TESTING SAGAS', function(t)  {
  const gen = setAuthorizeCode()
  let next = gen.next(appActions.setAuthorizeCode())
  t.deepEqual(next.value, call(Api.token.getToken), 'must yield api.getToken')

  next = gen.next(accessToken)
  t.deepEqual(next.value, put(appActions.setAccessToken(accessToken)), 'must yield action appActions.setAccessToken')

  next = gen.next(idToken)
  t.deepEqual(next.value, put(appActions.setIdToken(idToken)), 'must yield action appActions.setIdToken')

  next = gen.next()
  t.deepEqual(next.value, select(accessToken), 'must select accessToken')

  next = gen.next()
  t.deepEqual(next.value, select(idToken), 'must select idToken')

  next = gen.next(Api.user.getUser())
  t.deepEqual(next.value, call(Api.user.getUser), 'must yield api.getUser')

  next = gen.next(setUser)
  t.deepEqual(next.value, put(appActions.setUser(setUser)), 'must yield appActions.setUser')

  t.end()
})

我真的不知道怎么了。 但这不起作用,请我需要一些帮助,我在做什么错了?

有错误:

operator: deepEqual 
expected: |- { '@@redux-saga/IO': true, CALL: { context: null, fn: 
[Function: getToken], args: [] } } 
actual: |- undefined

其他2个相同。

0 个答案:

没有答案