Skip to content

Commit

Permalink
feat(router-store): add route fragment selector (ngrx#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
blove authored and BioPhoton committed Jun 5, 2020
1 parent b0cda84 commit 7705158
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const mockData = {
ref: 'ngrx.io',
},
},
fragment: 'test-fragment',
children: [],
},
fragment: null,
Expand Down Expand Up @@ -141,6 +142,14 @@ describe('Router State Selectors', () => {
expect(result).toEqual(undefined);
});

it('should create a selector for selecting the fragment', () => {
const result = selectors.selectFragment(state);

expect(result).toEqual(
state.router.state.root.firstChild.firstChild.fragment
);
});

it('should create a selector for selecting the query params', () => {
const result = selectors.selectQueryParams(state);

Expand Down
1 change: 1 addition & 0 deletions modules/router-store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MemoizedSelector } from '@ngrx/store';

export interface RouterStateSelectors<V> {
selectCurrentRoute: MemoizedSelector<V, any>;
selectFragment: MemoizedSelector<V, string | undefined>;
selectQueryParams: MemoizedSelector<V, Params>;
selectQueryParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectRouteParams: MemoizedSelector<V, Params>;
Expand Down
5 changes: 5 additions & 0 deletions modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function getSelectors<V>(
}
return route;
});
const selectFragment = createSelector(
selectCurrentRoute,
route => route && route.fragment
);
const selectQueryParams = createSelector(
selectCurrentRoute,
route => route && route.queryParams
Expand All @@ -45,6 +49,7 @@ export function getSelectors<V>(

return {
selectCurrentRoute,
selectFragment,
selectQueryParams,
selectQueryParam,
selectRouteParams,
Expand Down
1 change: 1 addition & 0 deletions projects/ngrx.io/content/guide/router-store/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const selectRouter = createFeatureSelector<

export const {
selectCurrentRoute, // select the current route
selectFragment, // select the current route fragment
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
Expand Down

0 comments on commit 7705158

Please sign in to comment.