-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add action creator functions (#1654)
- Loading branch information
1 parent
d559998
commit e7fe28b
Showing
48 changed files
with
536 additions
and
364 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,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 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 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
Oops, something went wrong.