Skip to content

Commit bdb60d8

Browse files
committed
Initial commit
1 parent 24fc343 commit bdb60d8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/toolkit/src/mapBuilders.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function executeReducerBuilderCallback<S>(
140140
) {
141141
if (process.env.NODE_ENV !== 'production') {
142142
/*
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,
144144
we enforce `addCase` to always be called before calling `addMatcher`
145145
as matching cases take precedence over matchers
146146
*/
@@ -159,6 +159,11 @@ export function executeReducerBuilderCallback<S>(
159159
typeof typeOrActionCreator === 'string'
160160
? typeOrActionCreator
161161
: typeOrActionCreator.type
162+
if (!type) {
163+
throw new Error(
164+
'`builder.addCase` cannot be called with an empty action type'
165+
)
166+
}
162167
if (type in actionsMap) {
163168
throw new Error(
164169
'addCase cannot be called with two reducers for the same action type'

packages/toolkit/src/tests/createReducer.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,24 @@ describe('createReducer', () => {
473473
`"addCase cannot be called with two reducers for the same action type"`
474474
)
475475
})
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"'
492+
)
493+
})
476494
})
477495

478496
describe('builder "addMatcher" method', () => {

0 commit comments

Comments
 (0)