File tree 2 files changed +27
-4
lines changed
2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -140,7 +140,7 @@ export function executeReducerBuilderCallback<S>(
140
140
) {
141
141
if ( process . env . NODE_ENV !== 'production' ) {
142
142
/*
143
- to keep the definition by the user in line with actual behavior,
143
+ to keep the definition by the user in line with actual behavior,
144
144
we enforce `addCase` to always be called before calling `addMatcher`
145
145
as matching cases take precedence over matchers
146
146
*/
@@ -159,9 +159,14 @@ export function executeReducerBuilderCallback<S>(
159
159
typeof typeOrActionCreator === 'string'
160
160
? typeOrActionCreator
161
161
: typeOrActionCreator . type
162
+ if ( ! type ) {
163
+ throw new Error (
164
+ '`builder.addCase` cannot be called with an empty action type'
165
+ )
166
+ }
162
167
if ( type in actionsMap ) {
163
168
throw new Error (
164
- 'addCase cannot be called with two reducers for the same action type'
169
+ '`builder. addCase` cannot be called with two reducers for the same action type'
165
170
)
166
171
}
167
172
actionsMap [ type ] = reducer
Original file line number Diff line number Diff line change @@ -460,7 +460,7 @@ describe('createReducer', () => {
460
460
. addCase ( decrement , ( state , action ) => state - action . payload )
461
461
)
462
462
) . toThrowErrorMatchingInlineSnapshot (
463
- `" addCase cannot be called with two reducers for the same action type"`
463
+ '"`builder. addCase` cannot be called with two reducers for the same action type"'
464
464
)
465
465
expect ( ( ) =>
466
466
createReducer ( 0 , ( builder ) =>
@@ -470,7 +470,25 @@ describe('createReducer', () => {
470
470
. addCase ( decrement , ( state , action ) => state - action . payload )
471
471
)
472
472
) . toThrowErrorMatchingInlineSnapshot (
473
- `"addCase cannot be called with two reducers for the same action type"`
473
+ '"`builder.addCase` cannot be called with two reducers for the same action type"'
474
+ )
475
+ } )
476
+
477
+ test ( 'will throw if an empty type is used' , ( ) => {
478
+ const customActionCreator = ( payload : number ) => ( {
479
+ type : 'custom_action' ,
480
+ payload,
481
+ } )
482
+ customActionCreator . type = ""
483
+ expect ( ( ) =>
484
+ createReducer ( 0 , ( builder ) =>
485
+ builder . addCase (
486
+ customActionCreator ,
487
+ ( state , action ) => state + action . payload
488
+ )
489
+ )
490
+ ) . toThrowErrorMatchingInlineSnapshot (
491
+ '"`builder.addCase` cannot be called with an empty action type"'
474
492
)
475
493
} )
476
494
} )
You can’t perform that action at this time.
0 commit comments