-
-
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(router-store): selects should return selectors (#2517)
Closes #2516
- Loading branch information
1 parent
52125b7
commit 831e1e4
Showing
4 changed files
with
139 additions
and
8 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ ts_test_library( | |
"//modules/store", | ||
"@npm//@angular/router", | ||
"@npm//rxjs", | ||
"@npm//ts-snippet", | ||
], | ||
) | ||
|
||
|
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,119 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './utils'; | ||
|
||
describe('router selectors', () => { | ||
const expectSnippet = expecter( | ||
code => ` | ||
import * as fromRouter from '@ngrx/router-store'; | ||
import { createSelector, createFeatureSelector } from '@ngrx/store'; | ||
export interface State { | ||
router: fromRouter.RouterReducerState<any>; | ||
} | ||
export const selectRouter = createFeatureSelector< | ||
State, | ||
fromRouter.RouterReducerState<any> | ||
>('router'); | ||
export const { | ||
selectCurrentRoute, | ||
selectQueryParams, | ||
selectQueryParam, | ||
selectRouteParams, | ||
selectRouteParam, | ||
selectRouteData, | ||
selectUrl, | ||
} = fromRouter.getSelectors(selectRouter); | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
it('selectCurrentRoute should return any', () => { | ||
expectSnippet(` | ||
export const selector = createSelector( | ||
selectCurrentRoute, | ||
route => route | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, any, DefaultProjectorFn<any>>' | ||
); | ||
}); | ||
|
||
it('selectQueryParams should return Params', () => { | ||
expectSnippet(` | ||
export const selector = createSelector( | ||
selectQueryParams, | ||
params => params | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, Params, DefaultProjectorFn<Params>>' | ||
); | ||
}); | ||
|
||
it('selectQueryParam should return string', () => { | ||
expectSnippet(` | ||
export const selectIdFromRoute = selectQueryParam('id') | ||
export const selector = createSelector( | ||
selectIdFromRoute, | ||
id => id | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, string, DefaultProjectorFn<string>>' | ||
); | ||
}); | ||
|
||
it('selectRouteParams should return Params', () => { | ||
expectSnippet(` | ||
export const selector = createSelector( | ||
selectRouteParams, | ||
params => params | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, Params, DefaultProjectorFn<Params>>' | ||
); | ||
}); | ||
|
||
it('selectRouteParam should return string', () => { | ||
expectSnippet(` | ||
export const selectIdFromRoute = selectRouteParam('id') | ||
export const selector = createSelector( | ||
selectIdFromRoute, | ||
id => id | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, string, DefaultProjectorFn<string>>' | ||
); | ||
}); | ||
|
||
it('selectRouteData should return Data', () => { | ||
expectSnippet(` | ||
export const selector = createSelector( | ||
selectRouteData, | ||
data => data | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, Data, DefaultProjectorFn<Data>>' | ||
); | ||
}); | ||
|
||
it('selectUrl should return string', () => { | ||
expectSnippet(` | ||
export const selector = createSelector( | ||
selectUrl, | ||
url => url | ||
); | ||
`).toInfer( | ||
'selector', | ||
'MemoizedSelector<State, string, DefaultProjectorFn<string>>' | ||
); | ||
}); | ||
}); |
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,10 @@ | ||
export const compilerOptions = () => ({ | ||
moduleResolution: 'node', | ||
target: 'es2015', | ||
baseUrl: '.', | ||
experimentalDecorators: true, | ||
paths: { | ||
'@ngrx/store': ['./modules/store'], | ||
'@ngrx/router-store': ['./modules/router-store'], | ||
}, | ||
}); |
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,11 +1,12 @@ | ||
import { Data, Params } from '@angular/router'; | ||
import { Params, Data } from '@angular/router'; | ||
import { MemoizedSelector } from '@ngrx/store'; | ||
|
||
export interface RouterStateSelectors<V> { | ||
selectCurrentRoute: (state: V) => any; | ||
selectQueryParams: (state: V) => Params; | ||
selectQueryParam: (param: string) => (state: V) => string | undefined; | ||
selectRouteParams: (state: V) => Params; | ||
selectRouteParam: (param: string) => (state: V) => string | undefined; | ||
selectRouteData: (state: V) => Data; | ||
selectUrl: (state: V) => string; | ||
selectCurrentRoute: MemoizedSelector<V, any>; | ||
selectQueryParams: MemoizedSelector<V, Params>; | ||
selectQueryParam: (param: string) => MemoizedSelector<V, string | undefined>; | ||
selectRouteParams: MemoizedSelector<V, Params>; | ||
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>; | ||
selectRouteData: MemoizedSelector<V, Data>; | ||
selectUrl: MemoizedSelector<V, string>; | ||
} |