Skip to content

Commit 10f7c59

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

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/utils/composeStores.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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');
66
return function Composition(atom = {}, action) {
7-
return mapValues(finalStores, (store, key) =>
8-
store(atom[key], action)
9-
);
7+
return mapValues(finalStores, (store, key) => {
8+
const state = store(atom[key], action);
9+
if (state === undefined) {
10+
throw new Error(`Store ${key} returns undefined. By default store should return original state.`);
11+
}
12+
return state;
13+
});
1014
};
1115
}

test/composeStores.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,29 @@ 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+
const store = composeStores({
32+
stack: (state = []) => state,
33+
bad: (state = [], action) => {
34+
if (action.type === 'something') {
35+
return state;
36+
}
37+
}
38+
});
39+
expect(() => store({}, {type: '@@testType'})).toThrow();
40+
});
41+
it('should throw an error if undefined return not by default', () => {
42+
const store = composeStores({
43+
stack: (state = []) => state,
44+
bad: (state = 1, action) => {
45+
if (action.type === 'something') {
46+
return undefined;
47+
}
48+
return state;
49+
}
50+
});
51+
expect(store({}, {type: '@@testType'})).toEqual({stack: [], bad: 1});
52+
expect(() => store({}, {type: 'something'})).toThrow();
53+
});
3054
});
3155
});

0 commit comments

Comments
 (0)