From bdb60d8136220ad168d910d88ae87400b802a9b4 Mon Sep 17 00:00:00 2001 From: Conor Hawes Date: Fri, 30 Jun 2023 16:55:35 -0400 Subject: [PATCH 1/2] Initial commit --- packages/toolkit/src/mapBuilders.ts | 7 ++++++- .../toolkit/src/tests/createReducer.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/mapBuilders.ts b/packages/toolkit/src/mapBuilders.ts index 559e234d03..151ad6c545 100644 --- a/packages/toolkit/src/mapBuilders.ts +++ b/packages/toolkit/src/mapBuilders.ts @@ -140,7 +140,7 @@ export function executeReducerBuilderCallback( ) { 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, we enforce `addCase` to always be called before calling `addMatcher` as matching cases take precedence over matchers */ @@ -159,6 +159,11 @@ export function executeReducerBuilderCallback( typeof typeOrActionCreator === 'string' ? typeOrActionCreator : typeOrActionCreator.type + if (!type) { + throw new Error( + '`builder.addCase` cannot be called with an empty action type' + ) + } if (type in actionsMap) { throw new Error( 'addCase cannot be called with two reducers for the same action type' diff --git a/packages/toolkit/src/tests/createReducer.test.ts b/packages/toolkit/src/tests/createReducer.test.ts index 5c3a181168..9a4652936e 100644 --- a/packages/toolkit/src/tests/createReducer.test.ts +++ b/packages/toolkit/src/tests/createReducer.test.ts @@ -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', () => { From 5c82a28eddaed7a69f4d47dfc9c70edf307e0788 Mon Sep 17 00:00:00 2001 From: Conor Hawes Date: Sun, 2 Jul 2023 17:26:59 -0400 Subject: [PATCH 2/2] Update error message syntax --- packages/toolkit/src/mapBuilders.ts | 2 +- packages/toolkit/src/tests/createReducer.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/toolkit/src/mapBuilders.ts b/packages/toolkit/src/mapBuilders.ts index 151ad6c545..90bbe7d50e 100644 --- a/packages/toolkit/src/mapBuilders.ts +++ b/packages/toolkit/src/mapBuilders.ts @@ -166,7 +166,7 @@ export function executeReducerBuilderCallback( } if (type in actionsMap) { throw new Error( - 'addCase cannot be called with two reducers for the same action type' + '`builder.addCase` cannot be called with two reducers for the same action type' ) } actionsMap[type] = reducer diff --git a/packages/toolkit/src/tests/createReducer.test.ts b/packages/toolkit/src/tests/createReducer.test.ts index 9a4652936e..4fb840f696 100644 --- a/packages/toolkit/src/tests/createReducer.test.ts +++ b/packages/toolkit/src/tests/createReducer.test.ts @@ -460,7 +460,7 @@ describe('createReducer', () => { .addCase(decrement, (state, action) => state - action.payload) ) ).toThrowErrorMatchingInlineSnapshot( - `"addCase cannot be called with two reducers for the same action type"` + '"`builder.addCase` cannot be called with two reducers for the same action type"' ) expect(() => createReducer(0, (builder) => @@ -470,7 +470,7 @@ describe('createReducer', () => { .addCase(decrement, (state, action) => state - action.payload) ) ).toThrowErrorMatchingInlineSnapshot( - `"addCase cannot be called with two reducers for the same action type"` + '"`builder.addCase` cannot be called with two reducers for the same action type"' ) })