Skip to content

Commit

Permalink
feat(router-store) Add base router selectors
Browse files Browse the repository at this point in the history
- create initial spec
- tested selectors retrievable from example app
  • Loading branch information
jasonhodges authored and Jason Hodges committed May 18, 2019
1 parent 4ac2170 commit b3af493
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 38 deletions.
97 changes: 97 additions & 0 deletions modules/router-store/spec/router_selectors.spec.ts
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);
});
});
});
2 changes: 1 addition & 1 deletion modules/router-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export {
SerializedRouterStateSnapshot,
BaseRouterStoreState,
} from './serializer';
export { RouterAdapter } from './models';
export * from './router_selectors';
11 changes: 1 addition & 10 deletions modules/router-store/src/models.ts
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>;
}
55 changes: 28 additions & 27 deletions modules/router-store/src/router_selectors.ts
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),
};
}

0 comments on commit b3af493

Please sign in to comment.