reduceActions
Testing helper. Calculates the state after dispatching an sequence of actions to a specific reducer, starting from the initial state.
1
// JavaScript
2
3
function reduceActions(reducer, ...actions)
Copied!
1
// TypeScript
2
3
function reduceActions<S, A extends Action, AS extends A[]>(
4
reducer: Reducer<S, A>,
5
...actions: AS
6
): S
Copied!

Details

reduceActions calls reducer with each of the passed actions - as if these had been dispatched in the specified order - and returns the resulting state. For the first reducer call, the reducer's initial state (as determined by getInitialState) is used; each following call receives the state returned by the previous one.
If called without any actions, reduceActions simply returns the reducer's initial state - that is, reduceActions(reducer) is equivalent to getInitialState(reducer).
This helper is meant for use in reducer tests.

Examples

Basic usage:
1
import { reduceActions } from 'redux-preboiled'
2
3
const reducer = (state = 0, action) => {
4
switch (action.type) {
5
case 'increment':
6
return state + 1
7
case 'multiply':
8
return state * action.payload
9
default:
10
return state
11
}
12
}
13
14
reduceActions(reducer, { type: 'increment' })
15
// => 1
16
17
reduceActions(
18
reducer,
19
{ type: 'increment' },
20
{ type: 'increment' },
21
{ type: 'multiply', payload: 2 }
22
)
23
// => 4
24
25
reduceActions(reducer)
26
// => 0
Copied!
Usage in a Jest test:
1
import { reduceActions } from 'redux-preboiled'
2
import counterReducer, {
3
increment,
4
multiply
5
} from './counter.redux'
6
7
test('increment', () => {
8
const state = reduceActions(counterReducer, increment())
9
expect(state).toEqual(1)
10
})
11
12
test('increment and multiply', () => {
13
const state = reduceActions(
14
counterReducer,
15
increment(),
16
increment(),
17
multiply(4)
18
)
19
expect(state).toEqual(8)
20
})
Copied!

See Also

Copy link