-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(store, example): add action creator #1654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
import { createAction, props, union } from '@ngrx/store'; | ||
import { expecter } from 'ts-snippet'; | ||
|
||
describe('Action Creators', () => { | ||
let originalTimeout: number; | ||
|
||
beforeEach(() => { | ||
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000; | ||
}); | ||
|
||
afterEach(() => { | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; | ||
}); | ||
|
||
const expectSnippet = expecter( | ||
code => ` | ||
// path goes from root | ||
import {createAction, props, union} from './modules/store/src/action_creator'; | ||
${code}`, | ||
{ | ||
moduleResolution: 'node', | ||
target: 'es2015', | ||
} | ||
); | ||
|
||
describe('createAction', () => { | ||
it('should create an action', () => { | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const fooAction = foo(42); | ||
|
||
expect(fooAction).toEqual({ type: 'FOO', foo: 42 }); | ||
}); | ||
|
||
it('should narrow the action', () => { | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const bar = createAction('BAR', (bar: number) => ({ bar })); | ||
const both = union({ foo, bar }); | ||
const narrow = (action: typeof both) => { | ||
if (action.type === foo.type) { | ||
expect(action.foo).toEqual(42); | ||
} else { | ||
throw new Error('Should not get here.'); | ||
} | ||
}; | ||
|
||
narrow(foo(42)); | ||
}); | ||
|
||
it('should be serializable', () => { | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const fooAction = foo(42); | ||
const text = JSON.stringify(fooAction); | ||
|
||
expect(JSON.parse(text)).toEqual({ type: 'FOO', foo: 42 }); | ||
}); | ||
|
||
it('should enforce ctor parameters', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const fooAction = foo('42'); | ||
`).toFail(/not assignable to parameter of type 'number'/); | ||
}); | ||
|
||
it('should enforce action property types', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const fooAction = foo(42); | ||
const value: string = fooAction.foo; | ||
`).toFail(/'number' is not assignable to type 'string'/); | ||
}); | ||
|
||
it('should enforce action property names', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', (foo: number) => ({ foo })); | ||
const fooAction = foo(42); | ||
const value = fooAction.bar; | ||
`).toFail(/'bar' does not exist on type/); | ||
}); | ||
}); | ||
|
||
describe('empty', () => { | ||
it('should allow empty action', () => { | ||
const foo = createAction('FOO'); | ||
const fooAction = foo(); | ||
|
||
expect(fooAction).toEqual({ type: 'FOO' }); | ||
}); | ||
}); | ||
|
||
describe('props', () => { | ||
it('should create an action', () => { | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const fooAction = foo({ foo: 42 }); | ||
|
||
expect(fooAction).toEqual({ type: 'FOO', foo: 42 }); | ||
}); | ||
|
||
it('should narrow the action', () => { | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const bar = createAction('BAR', props<{ bar: number }>()); | ||
const both = union({ foo, bar }); | ||
const narrow = (action: typeof both) => { | ||
if (action.type === foo.type) { | ||
expect(action.foo).toEqual(42); | ||
} else { | ||
throw new Error('Should not get here.'); | ||
} | ||
}; | ||
|
||
narrow(foo({ foo: 42 })); | ||
}); | ||
|
||
it('should be serializable', () => { | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const fooAction = foo({ foo: 42 }); | ||
const text = JSON.stringify(fooAction); | ||
|
||
expect(JSON.parse(text)).toEqual({ foo: 42, type: 'FOO' }); | ||
}); | ||
|
||
it('should enforce ctor parameters', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const fooAction = foo({ foo: '42' }); | ||
`).toFail(/'string' is not assignable to type 'number'/); | ||
}); | ||
|
||
it('should enforce action property types', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const fooAction = foo({ foo: 42 }); | ||
const value: string = fooAction.foo; | ||
`).toFail(/'number' is not assignable to type 'string'/); | ||
}); | ||
|
||
it('should enforce action property names', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
const fooAction = foo({ foo: 42 }); | ||
const value = fooAction.bar; | ||
`).toFail(/'bar' does not exist on type/); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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,67 @@ | ||
import { | ||
Creator, | ||
ActionCreator, | ||
TypedAction, | ||
FunctionWithParametersType, | ||
ParametersType, | ||
} from './models'; | ||
|
||
/** | ||
* Action creators taken from ts-action library and modified a bit to better | ||
* fit current NgRx usage. Thank you Nicholas Jamieson (@cartant). | ||
*/ | ||
export function createAction<T extends string>( | ||
type: T | ||
): ActionCreator<T, () => TypedAction<T>>; | ||
export function createAction<T extends string, P extends object>( | ||
type: T, | ||
config: { _as: 'props'; _p: P } | ||
): ActionCreator<T, (props: P) => P & TypedAction<T>>; | ||
export function createAction<T extends string, C extends Creator>( | ||
type: T, | ||
creator: C | ||
): FunctionWithParametersType< | ||
ParametersType<C>, | ||
ReturnType<C> & TypedAction<T> | ||
> & | ||
TypedAction<T>; | ||
export function createAction<T extends string>( | ||
type: T, | ||
config?: { _as: 'props' } | Creator | ||
): Creator { | ||
if (typeof config === 'function') { | ||
return defineType(type, (...args: unknown[]) => ({ | ||
...config(...args), | ||
type, | ||
})); | ||
} | ||
const as = config ? config._as : 'empty'; | ||
switch (as) { | ||
case 'empty': | ||
return defineType(type, () => ({ type })); | ||
case 'props': | ||
return defineType(type, (props: unknown) => ({ | ||
...(props as object), | ||
type, | ||
})); | ||
default: | ||
throw new Error('Unexpected config.'); | ||
} | ||
} | ||
|
||
export function props<P>(): { _as: 'props'; _p: P } { | ||
return { _as: 'props', _p: undefined! }; | ||
} | ||
|
||
export function union< | ||
C extends { [key: string]: ActionCreator<string, Creator> } | ||
>(creators: C): ReturnType<C[keyof C]> { | ||
return undefined!; | ||
} | ||
|
||
function defineType(type: string, creator: Creator): Creator { | ||
return Object.defineProperty(creator, 'type', { | ||
value: type, | ||
writable: false, | ||
}); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.