File tree Expand file tree Collapse file tree 2 files changed +33
-5
lines changed Expand file tree Collapse file tree 2 files changed +33
-5
lines changed Original file line number Diff line number Diff line change 1- import mapValues from '../utils /mapValues' ;
2- import pick from '../utils /pick' ;
1+ import mapValues from './mapValues' ;
2+ import pick from './pick' ;
33
44export 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 should return original state.` ) ;
11+ }
12+ return state ;
13+ } ) ;
1014 } ;
1115}
Original file line number Diff line number Diff 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 ;
47+ }
48+ return state ;
49+ }
50+ } ) ;
51+ expect ( store ( { } , { type : '@@testType' } ) ) . toEqual ( { stack : [ ] , bad : 1 } ) ;
52+ expect ( ( ) => store ( { } , { type : 'something' } ) ) . toThrow ( ) ;
53+ } ) ;
3054 } ) ;
3155} ) ;
You can’t perform that action at this time.
0 commit comments