Add default value option to combineReducers

This commit is contained in:
Zhigang Fang
2016-08-25 15:12:13 +08:00
parent 14834591ea
commit cda290ef72
2 changed files with 9 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ function assertReducerSanity(reducers) {
* @returns {Function} A reducer function that invokes every reducer inside the
* passed object, and builds a state object with the same shape.
*/
export default function combineReducers(reducers) {
export default function combineReducers(reducers, defaultValue = {}) {
var reducerKeys = Object.keys(reducers)
var finalReducers = {}
for (var i = 0; i < reducerKeys.length; i++) {
@@ -126,7 +126,7 @@ export default function combineReducers(reducers) {
sanityError = e
}
return function combination(state = {}, action) {
return function combination(state = defaultValue, action) {
if (sanityError) {
throw sanityError
}

View File

@@ -49,6 +49,13 @@ describe('Utils', () => {
spy.restore()
})
it('accepts default value as secound params', () => {
const reducer = combineReducers({
fake: (i='') => i
}, { fake: 'hello' })
expect(reducer(undefined, {}).fake).toEqual('hello')
})
it('throws an error if a reducer returns undefined handling an action', () => {
const reducer = combineReducers({
counter(state = 0, action) {