File tree 2 files changed +33
-5
lines changed 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' ;
3
3
4
4
export default function composeStores ( stores ) {
5
5
const finalStores = pick ( stores , ( val ) => typeof val === 'function' ) ;
6
6
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
+ } ) ;
10
14
} ;
11
15
}
Original file line number Diff line number Diff line change @@ -27,5 +27,29 @@ describe('Utils', () => {
27
27
28
28
expect ( Object . keys ( store ( { } , { type : 'push' } ) ) ) . toEqual ( [ 'stack' ] ) ;
29
29
} ) ;
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
+ } ) ;
30
54
} ) ;
31
55
} ) ;
You can’t perform that action at this time.
0 commit comments