Skip to content

Commit

Permalink
feat: Add identityPayloadCreator helper (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikew authored Dec 12, 2023
1 parent 9e027e1 commit 89d2bdd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default createActions('example', {

// An action with an argument that also returns a payload.
setIncrementBy: (n: number) => n,
// Which could also be written as this.
// setIncrementBy: identityPayloadCreator<number>(),

// An action that returns meta, payload, and even overrides the action type.
// Note that when you override the action type, the `.actionType` property
Expand Down
7 changes: 7 additions & 0 deletions src/createReducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ describe('createReducer', () => {
},
}),
)

state = testReducer(state, testActions.identityPayload('hello world'))
expect(state).toEqual(
expect.objectContaining({
identityPayload: 'hello world',
}),
)
})
})
11 changes: 11 additions & 0 deletions src/identityPayloadCreator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import identityPayloadCreator from './identityPayloadCreator'

describe('identityPayloadCreator', () => {
it('has one argument which is typed and returned', () => {
const payloadCreator = identityPayloadCreator<number>()
const payload = payloadCreator(12)

expect(typeof payload).toBe('number')
expect(payload).toBe(12)
})
})
5 changes: 5 additions & 0 deletions src/identityPayloadCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function identityPayloadCreator<T>() {
return (payload: T) => payload
}

export default identityPayloadCreator
2 changes: 2 additions & 0 deletions src/test/testActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import createActions from '../createActions'
import identityPayloadCreator from '../identityPayloadCreator'

const testActions = createActions('test', {
noPayload: () => undefined,
simplePayload: () => 'simple',
identityPayload: identityPayloadCreator<string>(),
complexPayload: (arg1: number, arg2: string) => ({ arg1, arg2 }),
specialProperties: (arg1: number, arg2: string) => ({
payload: { arg1, arg2 },
Expand Down
5 changes: 5 additions & 0 deletions src/test/testReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const initialState = {
arg1: -1,
arg2: '',
},
identityPayload: '',

start: '',
success: '',
Expand All @@ -38,6 +39,10 @@ const testReducer = createReducer(initialState, (builder) => {
arg2: action.payload.arg2,
},
}))
.addHandler(testActions.identityPayload, (state, action) => ({
...state,
identityPayload: action.payload,
}))
.addStartHandler(testActions.promisePayload, (state) => ({
...state,
start: 'started',
Expand Down

0 comments on commit 89d2bdd

Please sign in to comment.