Skip to content

Commit

Permalink
fix(custom-reducers): bypass combine reducer when state is empty and …
Browse files Browse the repository at this point in the history
…no custom reducers

Only calls `combineReducers` if there are reducers to call it with.
  • Loading branch information
rbrewington authored and kwelch committed Aug 31, 2017
1 parent 0d0bc38 commit 33c8a5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ const resolveAction = (reducers = {}, state, action) => {
[curr]: noOpReducer,
};
}, {});
return combineReducers(Object.assign({}, baseReducers, reducers))(
state,
action
);
const mergedReducers = Object.assign({}, baseReducers, reducers);
if (Object.keys(mergedReducers).length) {
return combineReducers(Object.assign({}, baseReducers, reducers))(
state,
action
);
}
return state;
};

export default (
Expand Down
11 changes: 11 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,15 @@ describe('entitiesReducer', () => {
console.error = error;
/* eslint-enable no-console */
});

it('should handle no custom reducers and empty initial state', () => {
/* eslint-disable no-console */
const error = console.error;
console.error = jest.fn();
const result = entitiesReducer({})({}, {});
expect(console.error).not.toHaveBeenCalled();
expect(result).toEqual({});
console.error = error;
/* eslint-enable no-console */
});
});

0 comments on commit 33c8a5d

Please sign in to comment.