reduceActions

Testing helper. Calculates the state after dispatching an sequence of actions to a specific reducer, starting from the initial state.

// JavaScript

function reduceActions(reducer, ...actions)
// TypeScript

function reduceActions<S, A extends Action, AS extends A[]>(
  reducer: Reducer<S, A>,
  ...actions: AS
): S

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:

import { reduceActions } from 'redux-preboiled'

const reducer = (state = 0, action) => {
  switch (action.type) {
    case 'increment':
      return state + 1
    case 'multiply':
      return state * action.payload
    default:
      return state
  }
}

reduceActions(reducer, { type: 'increment' })
// => 1

reduceActions(
  reducer,
  { type: 'increment' },
  { type: 'increment' },
  { type: 'multiply', payload: 2 }
)
// => 4

reduceActions(reducer)
// => 0

Usage in a Jest test:

import { reduceActions } from 'redux-preboiled'
import counterReducer, {
  increment,
  multiply
} from './counter.redux'

test('increment', () => {
  const state = reduceActions(counterReducer, increment())
  expect(state).toEqual(1)
})

test('increment and multiply', () => {
  const state = reduceActions(
    counterReducer,
    increment(),
    increment(),
    multiply(4)
  )
  expect(state).toEqual(8)
})

See Also

Last updated