Skip to content

Instantly share code, notes, and snippets.

@granmoe
Last active August 6, 2016 21:50
Show Gist options
  • Save granmoe/c9d2fd0b1a93876f8c563b11b757ae5c to your computer and use it in GitHub Desktop.
Save granmoe/c9d2fd0b1a93876f8c563b11b757ae5c to your computer and use it in GitHub Desktop.
Example of an async saga and test of non-trivial complexity.
import test from 'tape';
import { put, call } from 'redux-saga/effects'
import { changeLOS } from './sagas'
import api from 'nowhere'
test('changeLOS Saga', assert => {
const gen = changeLOS()
assert.deepEqual(
gen.next().value,
put({ type: 'SET_SHOW_SPINNER', data: true }),
'changeLOS Saga must dispatch SET_SHOW_SPINNER action with data = true'
)
assert.deepEqual(
gen.next().value,
call(api.changeLOS),
'changeLOS Saga must yield the effect call(api.changeLOS)'
)
assert.deepEqual(
gen.next().value,
call(orderUpdateActions, order), // import this I guess? need to export it from other file
'changeLOS Saga must yield the effect call(orderUpdateActions, order)'
)
// prob would break updateFromOrder into its own test
assert.deepEqual(
gen.next().value,
put({ type: 'SET_SHOW_SPINNER', data: false }),
'changeLOS Saga must dispatch SET_SHOW_SPINNER action with data = false'
)
assert.deepEqual(
gen.next().value,
put({ type: 'UPDATE_ERRORS', data: [] }), // how to check only that the arguments are correct type?
'changeLOS Saga must dispatch UPDATE_ERRORS with data = an array'
)
assert.deepEqual(
gen.next().value,
put({ type: 'UPDATE_ORDER', data: {} }),
'changeLOS Saga must dispatch UPDATE_ORDER with data = an object'
)
assert.deepEqual(
gen.next(),
{ done: true, value: undefined },
'changeLOS Saga must be done'
)
assert.end()
})
import { delay } from 'redux-saga'
import { put, call, take } from 'redux-saga/effects'
import api from 'nowhere'
function* updateFromOrder (order) { // simplified, obviously
yield put({ type: 'SET_SHOW_SPINNER', data: false })
yield put({ type: 'UPDATE_ERRORS', data: errors })
yield put({ type: 'UPDATE_ORDER', data: order })
}
export function* changeLOS () {
while (yield take('changeLOS', LOS)) {
yield put({ type: 'SET_SHOW_SPINNER', data: true })
const order = yield call(api.changeLOS, LOS)
yield* call(orderUpdateActions, order)
}
}
// need to export all generators within one generator so redux-saga can start them all at once
export default function* rootSaga () {
// import arrays of generators from various ducks, then yield gens.map(call) like below
const generators = [incrementAsync]
yield generators.map(call)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment