getInitialState
Returns the initial state of a reducer.
1
// JavaScript
2
3
function getInitialState(reducer)
Copied!
1
// TypeScript
2
3
function getInitialState<S>(reducer: Reducer<S>): S
Copied!

Details

getInitialState asks the passed reducer for its initial state by calling it with an undefined state and a dummy action, just as a Redux store would.
This helper is mainly intended for tests, where it can help improve readabilty. For example, when making assertions about a reducer's initial state, getInitialState(reducer) is more expressive than something like reducer(undefined, { type: '' }).

Examples

Basic usage:
1
import { getInitialState } from 'redux-preboiled'
2
3
const reducer = (state = 0, action) =>
4
action.type === 'increment' ? state + 1 : state
5
6
7
getInitialState(reducer)
8
// => 0
Copied!
Usage in Jest:
1
import { getInitialState } from 'redux-preboiled'
2
3
const reducer = (state = 0, action) =>
4
action.type === 'increment' ? state + 1 : state
5
6
test('initial state is 0', () => {
7
expect(getInitialState(reducer)).toBe(0);
8
})
Copied!

See Also

Copy link