Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/utils/composeReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export default function composeReducers(reducers) {
const finalReducers = pick(reducers, (val) => typeof val === 'function');

return function Composition(atom = {}, action) {
return mapValues(finalReducers, (store, key) =>
store(atom[key], action)
);
return mapValues(finalReducers, (reducer, key) => {
const state = reducer(atom[key], action);
if (typeof state === 'undefined') {
throw new Error(`Reducer ${key} returns undefined. By default reducer should return original state.`);
}
return state;
});
};
}
23 changes: 23 additions & 0 deletions test/composeReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,28 @@ describe('Utils', () => {
Object.keys(reducer({}, { type: 'push' }))
).toEqual(['stack']);
});
it('should throw an error if undefined return from reducer', () => {
const reducer = composeReducers({
stack: (state = []) => state,
bad: (state = [], action) => {
if (action.type === 'something') {
return state;
}
}
});
expect(() => reducer({}, {type: '@@testType'})).toThrow();
});
it('should throw an error if undefined return not by default', () => {
const reducer = composeReducers({
stack: (state = []) => state,
bad: (state = 1, action) => {
if (action.type !== 'something') {
return state;
}
}
});
expect(reducer({}, {type: '@@testType'})).toEqual({stack: [], bad: 1});
expect(() => reducer({}, {type: 'something'})).toThrow();
});
});
});