This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(create-async-actions): add createAsyncActions utility function
- Loading branch information
Showing
6 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import createAsyncActions from './create-async-actions'; | ||
|
||
test('creates a triad of identity action creators', () => { | ||
const [start, success, fail] = createAsyncActions<string>('foo'); | ||
expect(start.type).toBe('foo'); | ||
expect(success.type).toBe('foo/SUCCESS'); | ||
expect(fail.type).toBe('foo/ERROR'); | ||
|
||
expect(start('foo')).toEqual({ | ||
type: 'foo', | ||
payload: 'foo', | ||
}); | ||
|
||
expect(success('foo')).toEqual({ | ||
type: 'foo/SUCCESS', | ||
payload: 'foo', | ||
}); | ||
|
||
const err = new Error('foo'); | ||
expect(fail(err)).toEqual({ | ||
type: 'foo/ERROR', | ||
payload: err, | ||
error: true, | ||
}); | ||
}); | ||
|
||
test('creates a triad of action creators with custom payloads', () => { | ||
const [start, success, fail] = createAsyncActions( | ||
'bar', | ||
(str: string) => str, | ||
(length: number) => length, | ||
); | ||
expect(start.type).toBe('bar'); | ||
expect(success.type).toBe('bar/SUCCESS'); | ||
expect(fail.type).toBe('bar/ERROR'); | ||
|
||
expect(start('bar')).toEqual({ | ||
type: 'bar', | ||
payload: 'bar', | ||
}); | ||
|
||
expect(success(3)).toEqual({ | ||
type: 'bar/SUCCESS', | ||
payload: 3, | ||
}); | ||
|
||
const err = new Error('foo'); | ||
expect(fail(err)).toEqual({ | ||
type: 'bar/ERROR', | ||
payload: err, | ||
error: true, | ||
}); | ||
}); | ||
|
||
test('allows for mixed void and any', () => { | ||
const [start, success, fail] = createAsyncActions( | ||
'baz', | ||
() => {}, | ||
(users: { name: string }[]) => users, | ||
); | ||
|
||
expect(start()).toEqual({ | ||
type: 'baz', | ||
}); | ||
expect(success([{ name: 'knpwrs' }])).toEqual({ | ||
type: 'baz/SUCCESS', | ||
payload: [{ name: 'knpwrs' }], | ||
}); | ||
const err = new Error('baz'); | ||
expect(fail(err)).toEqual({ | ||
type: 'baz/ERROR', | ||
payload: err, | ||
error: true, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import createAction, { PayloadCreator, TsActionCreator, identity } from './create-action'; | ||
|
||
export default < | ||
PStart, | ||
AStart extends any[] = [PStart], | ||
PSuc = PStart, | ||
ASuc extends any[] = AStart, | ||
PErr = Error, | ||
AErr extends any[] = [PErr] | ||
>( | ||
name: string, | ||
startPc: PayloadCreator<PStart, AStart> = identity, | ||
sucPc: PayloadCreator<PSuc, ASuc> = identity, | ||
errPc: PayloadCreator<PErr, AErr> = identity, | ||
): [ | ||
TsActionCreator<PStart, AStart>, | ||
TsActionCreator<PSuc, ASuc>, | ||
TsActionCreator<PErr, AErr>, | ||
] => [ | ||
createAction<PStart, AStart>(name, startPc), | ||
createAction<PSuc, ASuc>(`${name}/SUCCESS`, sucPc), | ||
createAction<PErr, AErr>(`${name}/ERROR`, errPc), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters