Helper for testing redux-saga
Switch branches/tags
Clone or download
Latest commit 18c0d49 Jan 16, 2017

readme.md

redux-saga-test Build Status

A little helper to test [redux-saga][rs].

Install

$ npm install --save-dev redux-saga-test

Usage

The saga under test.

function * testSaga () {
  try {
    yield put({type: 'FETCHING'})
    const data = yield call(loadData)
    yield put({type: 'FETCHED', payload: data})
  } catch (e) {
    yield put({type: 'FETCHED', payload: e})
  }

The test looks like this:

const fromGenerator = require('redux-saga-test');

test('saga', (t) => {
  const expect = fromGenerator(t, testSaga()) // <= pass your assert library with a `deepEqual` method.

  expect.next().put({type: 'FETCHING'})
  expect.next().call(loadData)
  expect.next(mockData).put({type: 'FETCHED', payload: mockData})
  expect.next().returns()
})

To test a watcher you can use expect.takeEvery/takeLatest.

The watcher:

function * watchEvery () {
  yield * takeEvery('TEST_ACTION', testSaga)
}

The test:

test('takeEvery', (t) => {
  const expect = fromGenerator(t, watchEvery())
  expect.takeEvery('TEST_ACTION', testSaga)
})

For more examples look at the tests

License

MIT © Stoeffel