diff --git a/modules/router-store/spec/router_selectors.spec.ts b/modules/router-store/spec/router_selectors.spec.ts index 57671ddf29..e170f467d1 100644 --- a/modules/router-store/spec/router_selectors.spec.ts +++ b/modules/router-store/spec/router_selectors.spec.ts @@ -105,7 +105,7 @@ describe('Router State Selectors', () => { router: RouterReducerState; } - let selectors: RouterStateSelectors; + let selectors: RouterStateSelectors; let state: State; beforeEach(() => { @@ -115,6 +115,25 @@ describe('Router State Selectors', () => { selectors = getSelectors((state: State) => state.router); }); + it('should create selectCurrentRoute selector for selecting the current route', () => { + const result = selectors.selectCurrentRoute(state); + + expect(result).toEqual(state.router.state.root.firstChild.firstChild); + }); + it('should return undefined from selectCurrentRoute if routerState does not exist', () => { + interface State { + router: any; + } + let state: State; + state = { + router: undefined, + }; + selectors = getSelectors((state: State) => state.router); + + const result = selectors.selectCurrentRoute(state); + + expect(result).toEqual(undefined); + }); it('should create a selector for selecting the query params', () => { const result = selectors.selectQueryParams(state); diff --git a/modules/router-store/src/models.ts b/modules/router-store/src/models.ts index a768b8c9f0..4ccd1d7e3b 100644 --- a/modules/router-store/src/models.ts +++ b/modules/router-store/src/models.ts @@ -1,6 +1,6 @@ import { Data, Params } from '@angular/router'; -export interface RouterStateSelectors { +export interface RouterStateSelectors { selectCurrentRoute: (state: V) => any; selectQueryParams: (state: V) => Params; selectRouteParams: (state: V) => Params; diff --git a/modules/router-store/src/router_selectors.ts b/modules/router-store/src/router_selectors.ts index 089ccc702a..ad012ccf43 100644 --- a/modules/router-store/src/router_selectors.ts +++ b/modules/router-store/src/router_selectors.ts @@ -2,23 +2,22 @@ import { RouterReducerState } from '@ngrx/router-store'; import { createSelector } from '@ngrx/store'; import { RouterStateSelectors } from './models'; -export function getSelectors(): RouterStateSelectors< - any, - RouterReducerState ->; +// export function getSelectors(): RouterStateSelectors< +// RouterReducerState +// >; export function getSelectors( selectState: (state: V) => RouterReducerState -): RouterStateSelectors; -export function getSelectors( - selectState = (state: any) => state.state -): RouterStateSelectors { +): RouterStateSelectors; +export function getSelectors( + selectState: (state: V) => RouterReducerState +): RouterStateSelectors { const selectRouterState = createSelector( selectState, router => router && router.state ); const selectCurrentRoute = createSelector(selectRouterState, routerState => { if (!routerState) { - return false; + return undefined; } let route = routerState.root; while (route.firstChild) { diff --git a/projects/ngrx.io/content/guide/router-store/selectors.md b/projects/ngrx.io/content/guide/router-store/selectors.md index 9bff096e5f..0117dc679f 100644 --- a/projects/ngrx.io/content/guide/router-store/selectors.md +++ b/projects/ngrx.io/content/guide/router-store/selectors.md @@ -5,7 +5,7 @@ The `getSelectors` method supplied within `@ngrx/router-store` provides function The `getSelectors` method takes a selector function as its only argument to select the piece of state where the router state is being stored. The example below shows how to provide a selector for the top level `router` key in your state object. -**Note:** The `getSelectors` method works with the `routerReducer` provided by `@ngrx/router-store`. If you use a [custom serializer](guide/router-store/configuration#custom-router-state-serializer), you'll need to provide your own selectors if they do not adhere to `RouterReducerState`. +**Note:** The `getSelectors` method works with the `routerReducer` provided by `@ngrx/router-store`. If you use a [custom serializer](guide/router-store/configuration#custom-router-state-serializer), you'll need to provide your own selectors. Usage: