-
-
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.
fix(store): improve types for string selectors (#2174)
- Loading branch information
1 parent
1b4ba1a
commit 46a8467
Showing
9 changed files
with
333 additions
and
136 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
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,95 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './utils'; | ||
|
||
describe('createAction()', () => { | ||
const expectSnippet = expecter( | ||
code => ` | ||
import {createAction, props, union} from '@ngrx/store'; | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
describe('with props', () => { | ||
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/); | ||
}); | ||
|
||
it('should not allow type property', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ type: number }>()); | ||
`).toFail( | ||
/Argument of type '"type property is not allowed in action creators"' is not assignable to parameter of type/ | ||
); | ||
}); | ||
|
||
it('should not allow ararys', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<[]>()); | ||
`).toFail( | ||
/Argument of type '"arrays are not allowed in action creators"' is not assignable to parameter of type/ | ||
); | ||
}); | ||
}); | ||
|
||
describe('with function', () => { | ||
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/); | ||
}); | ||
|
||
it('should not allow type property', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', (type: string) => ({type})); | ||
`).toFail( | ||
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"'/ | ||
); | ||
}); | ||
|
||
it('should not allow arrays', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', () => [ ]); | ||
`).toFail( | ||
/Type 'any\[]' is not assignable to type '"arrays are not allowed in action creators"'/ | ||
); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './utils'; | ||
|
||
describe('createReducer()', () => { | ||
const expectSnippet = expecter( | ||
code => ` | ||
import {createAction, props, on} from '@ngrx/store'; | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
describe('on()', () => { | ||
it('should enforce action property types', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
on(foo, (state, action) => { const foo: string = action.foo; return state; }); | ||
`).toFail(/'number' is not assignable to type 'string'/); | ||
}); | ||
|
||
it('should enforce action property names', () => { | ||
expectSnippet(` | ||
const foo = createAction('FOO', props<{ foo: number }>()); | ||
on(foo, (state, action) => { const bar: string = action.bar; return state; }); | ||
`).toFail(/'bar' does not exist on type/); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.