From 0405d79678ab4e26d4ec9f1a19b0e7e378744e0a 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 | 14 +++++++++----- test/composeStores.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/utils/composeStores.js b/src/utils/composeStores.js index d8c4420546d..b971ea35726 100644 --- a/src/utils/composeStores.js +++ b/src/utils/composeStores.js @@ -1,11 +1,15 @@ -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'); 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 === undefined) { + throw new Error(`Store ${key} returns undefined. By default should return original state.`); + } + return state; + }); }; } diff --git a/test/composeStores.spec.js b/test/composeStores.spec.js index 551b275e59a..65f61c9d65e 100644 --- a/test/composeStores.spec.js +++ b/test/composeStores.spec.js @@ -27,5 +27,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 undefined; + } + return state; + } + }); + expect(store({}, {type: '@@testType'})).toEqual({stack: [], bad: 1}); + expect(() => store({}, {type: 'something'})).toThrow(); + }); }); });