Skip to content

Commit

Permalink
Merge pull request #3877 from reduxjs/tostring-restore
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Nov 15, 2023
2 parents 0dc7fdd + 16f5d62 commit 815a705
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/usage/usage-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ const reducer = createReducer({}, (builder) => {

This means you don't have to write or use a separate action type variable, or repeat the name and value of an action type like `const SOME_ACTION_TYPE = "SOME_ACTION_TYPE"`.

If you want to use one of these action creators in a switch statement, you need to call `actionCreator.type` yourself:
If you want to use one of these action creators in a switch statement, you need to reference `actionCreator.type` yourself:

```js
const actionCreator = createAction('SOME_ACTION_TYPE')
Expand Down
8 changes: 6 additions & 2 deletions packages/toolkit/src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ export type PayloadActionCreator<
/**
* A utility function to create an action creator for the given action type
* string. The action creator accepts a single argument, which will be included
* in the action object as a field called payload.
* in the action object as a field called payload. The action creator function
* will also have its toString() overridden so that it returns the action type.
*
* @param type The action type to use for created actions.
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
Expand All @@ -239,7 +240,8 @@ export function createAction<P = void, T extends string = string>(
/**
* A utility function to create an action creator for the given action type
* string. The action creator accepts a single argument, which will be included
* in the action object as a field called payload.
* in the action object as a field called payload. The action creator function
* will also have its toString() overridden so that it returns the action type.
*
* @param type The action type to use for created actions.
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
Expand Down Expand Up @@ -273,6 +275,8 @@ export function createAction(type: string, prepareAction?: Function): any {
return { type, payload: args[0] }
}

actionCreator.toString = () => `${type}`

actionCreator.type = type

actionCreator.match = (action: Action<string>): action is PayloadAction =>
Expand Down
7 changes: 7 additions & 0 deletions packages/toolkit/src/tests/createAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ describe('createAction', () => {
})
})

describe('when stringifying action', () => {
it('should return the action type', () => {
const actionCreator = createAction('A_TYPE')
expect(`${actionCreator}`).toEqual('A_TYPE')
})
})

describe('when passing a prepareAction method only returning a payload', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
Expand Down

0 comments on commit 815a705

Please sign in to comment.