测试传奇终于递归了

时间:2017-11-08 14:10:04

标签: unit-testing jest redux-saga

我有下一个传奇功能

function* fetchData() {
  try {
    const response = yield call(() => api.get('/names'));
    yield put(actions.succeeded(response.data));
  } catch (e) {
    yield put(actions.failed(e.message));
  } finally {
    yield delay(3000);
    yield put(actions.fetchGraph());
  }
}

如何在finally块中测试yield put(actions.fetchGraph());

1 个答案:

答案 0 :(得分:1)

我没有对其进行测试,但在iterator().next调用iterator.throw块的结果后不应该是finally

const iterator = fetchData()

assert.deepEqual(
  iterator.next().value,
  call(api.get, '/names'),
  "fetchData should yield an Effect call(api.get, '/names')",
)

const error = { message: 'Fake Error' }

// Throw error and test `catch`
assert.deepEqual(
  iterator.throw(error).value,
  put(actions.failed(error.message)),
  "fetchData should yield an Effect put(actions.failed(error.message))",
)

// After `catch` block, `next()` should continue in `finally`
assert.deepEqual(
  iterator.next().value,
  delay(3000),
  "fetchData should yield an Effect delay(3000)",
)