11import expect from 'expect' ;
22import { combineReducers } from '../../src' ;
3+ import { ActionTypes } from '../../src/Store' ;
34
45describe ( 'Utils' , ( ) => {
56 describe ( 'combineReducers' , ( ) => {
@@ -30,43 +31,44 @@ describe('Utils', () => {
3031 ) . toEqual ( [ 'stack' ] ) ;
3132 } ) ;
3233
33- it ( 'should throw an error if a reducer returns undefined' , ( ) => {
34+ it ( 'should throw an error if a reducer returns undefined handling an action ' , ( ) => {
3435 const reducer = combineReducers ( {
35- undefinedByDefault ( state = 0 , action ) {
36+ counter ( state = 0 , action ) {
3637 switch ( action && action . type ) {
3738 case 'increment' :
3839 return state + 1 ;
3940 case 'decrement' :
4041 return state - 1 ;
41- case '@@INIT ' :
42- return state ;
43- default :
42+ case 'whatever ' :
43+ case null :
44+ case undefined :
4445 return undefined ;
46+ default :
47+ return state ;
4548 }
4649 }
4750 } ) ;
4851
49- const initialState = reducer ( undefined , { type : '@@INIT' } ) ;
5052 expect (
51- ( ) => reducer ( initialState , { type : 'whatever' } )
53+ ( ) => reducer ( { counter : 0 } , { type : 'whatever' } )
5254 ) . toThrow (
53- / " u n d e f i n e d B y D e f a u l t " .* " w h a t e v e r " /
55+ / " c o u n t e r " .* " w h a t e v e r " /
5456 ) ;
5557 expect (
56- ( ) => reducer ( initialState , null )
58+ ( ) => reducer ( { counter : 0 } , null )
5759 ) . toThrow (
58- / " u n d e f i n e d B y D e f a u l t " .* a n a c t i o n /
60+ / " c o u n t e r " .* a n a c t i o n /
5961 ) ;
6062 expect (
61- ( ) => reducer ( initialState , { } )
63+ ( ) => reducer ( { counter : 0 } , { } )
6264 ) . toThrow (
63- / " u n d e f i n e d B y D e f a u l t " .* a n a c t i o n /
65+ / " c o u n t e r " .* a n a c t i o n /
6466 ) ;
6567 } ) ;
6668
6769 it ( 'should throw an error if a reducer returns undefined initializing' , ( ) => {
68- const reducer = combineReducers ( {
69- undefinedInitially ( state , action ) {
70+ expect ( ( ) => combineReducers ( {
71+ counter ( state , action ) {
7072 switch ( action . type ) {
7173 case 'increment' :
7274 return state + 1 ;
@@ -76,12 +78,28 @@ describe('Utils', () => {
7678 return state ;
7779 }
7880 }
79- } ) ;
81+ } ) ) . toThrow (
82+ / " c o u n t e r " .* i n i t i a l i z a t i o n /
83+ ) ;
84+ } ) ;
8085
81- expect (
82- ( ) => reducer ( undefined , { type : '@@INIT' } )
83- ) . toThrow (
84- / " u n d e f i n e d I n i t i a l l y " .* i n i t i a l i z a t i o n /
86+ it ( 'should throw an error if a reducer attempts to handle a private action' , ( ) => {
87+ expect ( ( ) => combineReducers ( {
88+ counter ( state , action ) {
89+ switch ( action . type ) {
90+ case 'increment' :
91+ return state + 1 ;
92+ case 'decrement' :
93+ return state - 1 ;
94+ // Never do this in your code:
95+ case ActionTypes . INIT :
96+ return 0 ;
97+ default :
98+ return undefined ;
99+ }
100+ }
101+ } ) ) . toThrow (
102+ / " c o u n t e r " .* p r i v a t e /
85103 ) ;
86104 } ) ;
87105 } ) ;
0 commit comments