Skip to content

Commit b2476c5

Browse files
author
taylorhakes
committed
Throw error on undefined value from store function
1 parent 34fe400 commit b2476c5

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/utils/composeStores.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import mapValues from '../utils/mapValues';
2-
import pick from '../utils/pick';
1+
import mapValues from './mapValues';
2+
import pick from './pick';
33

44
export default function composeStores(stores) {
55
const finalStores = pick(stores, (val) => typeof val === 'function');
6+
7+
if (process.env.NODE_ENV !== 'production') {
8+
Object.keys(finalStores).forEach(function(key) {
9+
if (finalStores[key](undefined, '@@TEST') === undefined) {
10+
throw new Error(`Store ${key} returns undefined. By default store should return original state.`);
11+
}
12+
});
13+
}
14+
615
return function Composition(atom = {}, action) {
7-
return mapValues(finalStores, (store, key) =>
8-
store(atom[key], action)
9-
);
16+
return mapValues(finalStores, (store, key) => store(atom[key], action));
1017
};
1118
}

test/composeStores.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,15 @@ describe('Utils', () => {
2727

2828
expect(Object.keys(store({}, {type: 'push'}))).toEqual(['stack']);
2929
});
30+
it('should throw an error if undefined return from store', () => {
31+
expect(() => composeStores({
32+
stack: (state = []) => state,
33+
bad: (state= [], action) => {
34+
if (action === 'something') {
35+
return state;
36+
}
37+
}
38+
})).toThrow();
39+
});
3040
});
3141
});

0 commit comments

Comments
 (0)