-
-
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(router-store) Add base router selectors
- create initial spec - tested selectors retrievable from example app
- Loading branch information
1 parent
4ac2170
commit b3af493
Showing
4 changed files
with
127 additions
and
38 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,97 @@ | ||
import * as fromRouter from '@ngrx/router-store'; | ||
import { RouterStateSelectors } from '../src/models'; | ||
import { getSelectors } from '../src/router_selectors'; | ||
|
||
let mockData = { | ||
state: { | ||
root: { | ||
params: {}, | ||
paramMap: { | ||
params: {}, | ||
}, | ||
data: {}, | ||
url: [], | ||
outlet: 'primary', | ||
routeConfig: null, | ||
queryParams: {}, | ||
queryParamMap: { | ||
params: {}, | ||
}, | ||
fragment: null, | ||
firstChild: { | ||
params: {}, | ||
paramMap: { | ||
params: {}, | ||
}, | ||
data: {}, | ||
url: [ | ||
{ | ||
path: 'login', | ||
parameters: {}, | ||
}, | ||
], | ||
outlet: 'primary', | ||
routeConfig: { | ||
path: 'login', | ||
}, | ||
queryParams: {}, | ||
queryParamMap: { | ||
params: {}, | ||
}, | ||
fragment: null, | ||
children: [], | ||
}, | ||
children: [ | ||
{ | ||
params: {}, | ||
paramMap: { | ||
params: {}, | ||
}, | ||
data: {}, | ||
url: [ | ||
{ | ||
path: 'login', | ||
parameters: {}, | ||
}, | ||
], | ||
outlet: 'primary', | ||
routeConfig: { | ||
path: 'login', | ||
}, | ||
queryParams: {}, | ||
queryParamMap: { | ||
params: {}, | ||
}, | ||
fragment: null, | ||
children: [], | ||
}, | ||
], | ||
}, | ||
url: '/login', | ||
}, | ||
navigationId: 1, | ||
}; | ||
describe('Router State Selectors', () => { | ||
describe('Composed Selectors', () => { | ||
interface State { | ||
router: fromRouter.RouterReducerState<any>; | ||
} | ||
|
||
let selectors: RouterStateSelectors<any, State>; | ||
let state: State; | ||
|
||
beforeEach(() => { | ||
state = { | ||
router: mockData, | ||
}; | ||
|
||
selectors = getSelectors((state: State) => state.router); | ||
}); | ||
|
||
it('should create a selector for selecting the url', () => { | ||
const url = selectors.selectUrl(state); | ||
|
||
expect(url).toEqual(state.router.state.url); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,15 +1,6 @@ | ||
import { RouterReducerState } from '@ngrx/router-store'; | ||
|
||
export interface RouterSelectors<T, V> { | ||
export interface RouterStateSelectors<T, V> { | ||
selectQueryParams: (state: V) => string[]; | ||
selectRouteParams: (state: V) => string[] | number[]; | ||
selectRouteData: (state: V) => T[]; | ||
selectUrl: (state: V) => string; | ||
} | ||
|
||
export interface RouterAdapter<T> { | ||
getSelectors(): RouterSelectors<T, RouterReducerState>; | ||
getSelectors<V>( | ||
selectState: (state: V) => RouterReducerState | ||
): RouterSelectors<T, V>; | ||
} |
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,36 +1,37 @@ | ||
import { RouterReducerState } from '@ngrx/router-store'; | ||
import { createSelector } from '@ngrx/store'; | ||
import { RouterSelectors } from './models'; | ||
import { RouterStateSelectors } from './models'; | ||
|
||
export function createSelectorsFactory<T>() { | ||
function getSelectors(): RouterSelectors<T, RouterReducerState>; | ||
function getSelectors<V>( | ||
selectState: (state: V) => RouterReducerState | ||
): RouterSelectors<T, V>; | ||
function getSelectors( | ||
selectState?: (state: any) => RouterReducerState | ||
): RouterSelectors<T, any> { | ||
const selectQueryParams = (state: any) => state.queryParams; | ||
const selectRouteParams = (state: any) => state.routeParams; | ||
const selectRouteData = (state: any) => state.routeData; | ||
const selectUrl = (state: any) => state.url; | ||
|
||
if (!selectState) { | ||
return { | ||
selectQueryParams, | ||
selectRouteParams, | ||
selectRouteData, | ||
selectUrl, | ||
}; | ||
} | ||
export function getSelectors(): RouterStateSelectors< | ||
any, | ||
RouterReducerState<any> | ||
>; | ||
export function getSelectors<V>( | ||
selectState: (state: V) => RouterReducerState<any> | ||
): RouterStateSelectors<any, V>; | ||
export function getSelectors<T>( | ||
selectState?: (state: any) => RouterReducerState | ||
): RouterStateSelectors<T, any> { | ||
const selectQueryParams = (state: any) => | ||
state && state.state.root.queryParams; | ||
const selectRouteParams = (state: any) => | ||
state && state.state.root.routeParams; | ||
const selectRouteData = (state: any) => state && state.state.root.data; | ||
const selectUrl = (state: any) => state && state.state.url; | ||
|
||
if (!selectState) { | ||
return { | ||
selectQueryParams: createSelector(selectState, selectQueryParams), | ||
selectRouteParams: createSelector(selectState, selectRouteParams), | ||
selectRouteData: createSelector(selectState, selectRouteData), | ||
selectUrl: createSelector(selectState, selectUrl), | ||
selectQueryParams, | ||
selectRouteParams, | ||
selectRouteData, | ||
selectUrl, | ||
}; | ||
} | ||
|
||
return { getSelectors }; | ||
return { | ||
selectQueryParams: createSelector(selectState, selectQueryParams), | ||
selectRouteParams: createSelector(selectState, selectRouteParams), | ||
selectRouteData: createSelector(selectState, selectRouteData), | ||
selectUrl: createSelector(selectState, selectUrl), | ||
}; | ||
} |