Skip to content

Commit

Permalink
Throw error on undefined value from store function
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Jun 30, 2015
1 parent 34fe400 commit b2476c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/utils/composeStores.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import mapValues from '../utils/mapValues';
import pick from '../utils/pick';
import mapValues from './mapValues';
import pick from './pick';

export default function composeStores(stores) {
const finalStores = pick(stores, (val) => typeof val === 'function');

if (process.env.NODE_ENV !== 'production') {
Object.keys(finalStores).forEach(function(key) {
if (finalStores[key](undefined, '@@TEST') === undefined) {
throw new Error(`Store ${key} returns undefined. By default store should return original state.`);
}
});
}

return function Composition(atom = {}, action) {
return mapValues(finalStores, (store, key) =>
store(atom[key], action)
);
return mapValues(finalStores, (store, key) => store(atom[key], action));
};
}
10 changes: 10 additions & 0 deletions test/composeStores.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ describe('Utils', () => {

expect(Object.keys(store({}, {type: 'push'}))).toEqual(['stack']);
});
it('should throw an error if undefined return from store', () => {
expect(() => composeStores({
stack: (state = []) => state,
bad: (state= [], action) => {
if (action === 'something') {
return state;
}
}
})).toThrow();
});
});
});

0 comments on commit b2476c5

Please sign in to comment.