Skip to content

Commit

Permalink
feat(router-store) Add base router selectors
Browse files Browse the repository at this point in the history
- remove unnecessary type from model
- add additional unit tests
- correct documentation wording
  • Loading branch information
jasonhodges committed May 29, 2019
1 parent 3abcc7d commit 3acc6dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
21 changes: 20 additions & 1 deletion modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Router State Selectors', () => {
router: RouterReducerState<any>;
}

let selectors: RouterStateSelectors<any, State>;
let selectors: RouterStateSelectors<State>;
let state: State;

beforeEach(() => {
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion modules/router-store/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Data, Params } from '@angular/router';

export interface RouterStateSelectors<T, V> {
export interface RouterStateSelectors<V> {
selectCurrentRoute: (state: V) => any;
selectQueryParams: (state: V) => Params;
selectRouteParams: (state: V) => Params;
Expand Down
17 changes: 8 additions & 9 deletions modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
>;
// export function getSelectors(): RouterStateSelectors<
// RouterReducerState<any>
// >;
export function getSelectors<V>(
selectState: (state: V) => RouterReducerState<any>
): RouterStateSelectors<any, V>;
export function getSelectors<T>(
selectState = (state: any) => state.state
): RouterStateSelectors<T, any> {
): RouterStateSelectors<V>;
export function getSelectors<V>(
selectState: (state: V) => RouterReducerState<any>
): RouterStateSelectors<V> {
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) {
Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx.io/content/guide/router-store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 3acc6dd

Please sign in to comment.