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 5965423
Show file tree
Hide file tree
Showing 2 changed files with 39 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';

// UNDEF = undefined;
const UNDEF = void 0;

export default function composeStores(stores) {
const finalStores = pick(stores, (val) => typeof val === 'function');
return function Composition(atom = {}, action) {
return mapValues(finalStores, (store, key) =>
store(atom[key], action)
);
return mapValues(finalStores, (store, key) => {
const state = store(atom[key], action);
if (state === UNDEF) {
throw new Error(`Store ${key} returns undefined. By default store should return original state.`);
}
return state;
});
};
}
27 changes: 27 additions & 0 deletions test/composeStores.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import expect from 'expect';
import { composeStores } from '../src';

// UNDEF = undefined;
const UNDEF = void 0;

describe('Utils', () => {
describe('composeStores', () => {
it('should return a store that maps state keys to reducer functions', () => {
Expand All @@ -27,5 +30,29 @@ describe('Utils', () => {

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

0 comments on commit 5965423

Please sign in to comment.