# reduceActions

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

```js
// JavaScript

function reduceActions(reducer, ...actions)
```

```ts
// 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`](/api/getinitialstate.md)) 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:

```js
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](https://jestjs.io/) test:

```js
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

* [Testing](/guides/testing.md) guide


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://redux-preboiled.js.org/api/reduceactions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
