From b2476c5c7ecc5e3480c8cba3013a1966d51a4861 Mon Sep 17 00:00:00 2001 From: taylorhakes Date: Mon, 29 Jun 2015 22:41:14 -0400 Subject: [PATCH] Throw error on undefined value from store function --- src/utils/composeStores.js | 17 ++++++++++++----- test/composeStores.spec.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/utils/composeStores.js b/src/utils/composeStores.js index d8c4420546d..0e084f88bc0 100644 --- a/src/utils/composeStores.js +++ b/src/utils/composeStores.js @@ -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)); }; } diff --git a/test/composeStores.spec.js b/test/composeStores.spec.js index 551b275e59a..17795421d71 100644 --- a/test/composeStores.spec.js +++ b/test/composeStores.spec.js @@ -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(); + }); }); });