Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when type is empty in builder.addCase #3572

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/toolkit/src/mapBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function executeReducerBuilderCallback<S>(
) {
if (process.env.NODE_ENV !== 'production') {
/*
to keep the definition by the user in line with actual behavior,
to keep the definition by the user in line with actual behavior,
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
we enforce `addCase` to always be called before calling `addMatcher`
as matching cases take precedence over matchers
*/
Expand All @@ -159,6 +159,11 @@ export function executeReducerBuilderCallback<S>(
typeof typeOrActionCreator === 'string'
? typeOrActionCreator
: typeOrActionCreator.type
if (!type) {
throw new Error(
'`builder.addCase` cannot be called with an empty action type'
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
)
}
if (type in actionsMap) {
throw new Error(
'addCase cannot be called with two reducers for the same action type'
Expand Down
18 changes: 18 additions & 0 deletions packages/toolkit/src/tests/createReducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ describe('createReducer', () => {
`"addCase cannot be called with two reducers for the same action type"`
)
})

test('will throw if an empty type is used', () => {
const customActionCreator = (payload: number) => ({
type: 'custom_action',
payload,
})
customActionCreator.type = ""
expect(() =>
createReducer(0, (builder) =>
builder.addCase(
customActionCreator,
(state, action) => state + action.payload
)
)
).toThrowErrorMatchingInlineSnapshot(
'"`builder.addCase` cannot be called with an empty action type"'
)
})
})

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