Skip to content

Commit

Permalink
infer action name from type
Browse files Browse the repository at this point in the history
  • Loading branch information
ben.durrant committed May 3, 2023
1 parent 6189cc3 commit fc53a5a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/toolkit/src/actionCreatorInvariantMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export interface ActionCreatorInvariantMiddlewareOptions {
}

export function getMessage(type?: unknown) {
const splitType = type ? `${type}`.split('/') : []
const actionName = splitType[splitType.length - 1] || 'actionCreator'
return `Detected an action creator with type "${
type || 'unknown'
}" being dispatched.
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(actionCreator())\` instead of \`dispatch(actionCreator)\`. This is necessary even if the action has no payload.`
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(${actionName}())\` instead of \`dispatch(${actionName})\`. This is necessary even if the action has no payload.`
}

export function createActionCreatorInvariantMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('createActionCreatorInvariantMiddleware', () => {
consoleSpy.mockRestore()
})

const dummyAction = createAction('anAction')
const dummyAction = createAction('aSlice/anAction')

it('sends the action through the middleware chain', () => {
const next: Dispatch = (action) => ({
Expand Down

0 comments on commit fc53a5a

Please sign in to comment.