-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export getType to public API
- Loading branch information
Mohammad Hasani
committed
Jan 17, 2019
1 parent
c521828
commit f8e4dc5
Showing
5 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getType getType(viaToString) (type) should match snapshot: getType getType(viaToString) 1`] = `"\\"[Todo] truncate\\""`; | ||
|
||
exports[`getType getType(viaType) (type) should match snapshot: getType getType(viaType) 1`] = `"\\"[Todo] truncate\\""`; |
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,19 @@ | ||
import { getType } from '../get-type' | ||
|
||
// @dts-jest:group getType | ||
|
||
const type = '[Todo] truncate' | ||
type Type = typeof type | ||
|
||
const viaType = { type: <Type>type } | ||
const viaToString = { | ||
toString(): Type { | ||
return type | ||
}, | ||
} | ||
|
||
// @dts-jest:pass:snap | ||
getType(viaType) | ||
|
||
// @dts-jest:pass:snap | ||
getType(viaToString) |
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,28 @@ | ||
import { getType } from '../get-type' | ||
|
||
describe('getType', () => { | ||
it('should throw error when action creator does not provide either .type nor toString()', () => { | ||
const type = '[Todo] truncate' | ||
const actionCreator = () => ({ type }) | ||
|
||
expect(() => getType(actionCreator)).toThrow() | ||
}) | ||
|
||
it('should get action type of action creator via .type', () => { | ||
const type = '[Todo] truncate' | ||
const actionCreator = Object.assign(() => ({ type }), { type }) | ||
|
||
expect(getType(actionCreator)).toBe(type) | ||
}) | ||
|
||
it('should get action type of action creator via toString()', () => { | ||
const type = '[Todo] truncate' | ||
const actionCreator = Object.assign(() => ({ type }), { | ||
toString() { | ||
return type | ||
}, | ||
}) | ||
|
||
expect(getType(actionCreator)).toBe(type) | ||
}) | ||
}) |
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,29 @@ | ||
/** | ||
* Map action creator to it's contained action type | ||
* @description it gets an object with at least a type property or overridden toString method and returns it. | ||
* @example | ||
* const increment = createAction('[Counter] increment') | ||
* getType(increment) //=> '[Counter] increment' | ||
* @example | ||
* getType({ type: 'TEST' }) //=> 'TEST' | ||
* @example | ||
* getType({ | ||
* toString() { return 'TEST' } | ||
* }) //=> 'TEST' | ||
*/ | ||
export function getType< | ||
AC extends { type?: string; toString(): string } | { type: string }, | ||
Type extends string = AC extends { type: infer T } | ||
? T | ||
: AC extends { toString(): infer U } | ||
? U | ||
: never | ||
>(actionCreator: AC) { | ||
if (!actionCreator.type && !actionCreator.hasOwnProperty('toString')) { | ||
throw new Error( | ||
`Action creator that has been passed to getType() does not provide any API to expose action type. You can use createAction() to create an action creator without any unsense errors.` | ||
) | ||
} | ||
|
||
return <Type>(actionCreator.type || actionCreator.toString()) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './action' | ||
export { createAction, ActionCreator } from './create-action' | ||
export { getType } from './get-type' |