Testing
Because your domain is described a series of pure functions, testing becomes ultra-simple: given some input, expect a certain output. No mocking calls to external services or a database.
Actions
Let's look at the basic example from the Domain page:
const actions = {
addTodo: (state, payload) => {
if (!payload.title) throw new Error('titleMissing')
return [{
type: 'TodoAdded',
title: payload.title,
at: Date.now(),
}]
}
}
module.exports = actionsNow let's write a test for this.
// stub Date.now
Date.now = () => 123
const test = require('tape')
const actions = require('../actions')
test('addTodo', assert => {
assert.deepEquals(
actions.addTodo({}, { title: 'foobar' }),
[{
type: 'TodoAdded',
title: 'foobar',
at: 123
}],
'generates a TodoAdded event'
)
assert.throws(
() => actions.addTodo({}, { foo: 'bar' }),
/titleMissing/,
'throws if title is missing'
)
})And we can even execute it:
Reducer
Last updated
Was this helpful?