Skip to content

Commit

Permalink
fix(router-store): selects should return selectors (#2517)
Browse files Browse the repository at this point in the history
Closes #2516
  • Loading branch information
timdeschryver authored May 8, 2020
1 parent 52125b7 commit 831e1e4
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions modules/router-store/spec/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ts_test_library(
"//modules/store",
"@npm//@angular/router",
"@npm//rxjs",
"@npm//ts-snippet",
],
)

Expand Down
119 changes: 119 additions & 0 deletions modules/router-store/spec/types/selectors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './utils';

describe('router selectors', () => {
const expectSnippet = expecter(
code => `
import * as fromRouter from '@ngrx/router-store';
import { createSelector, createFeatureSelector } from '@ngrx/store';
export interface State {
router: fromRouter.RouterReducerState<any>;
}
export const selectRouter = createFeatureSelector<
State,
fromRouter.RouterReducerState<any>
>('router');
export const {
selectCurrentRoute,
selectQueryParams,
selectQueryParam,
selectRouteParams,
selectRouteParam,
selectRouteData,
selectUrl,
} = fromRouter.getSelectors(selectRouter);
${code}
`,
compilerOptions()
);

it('selectCurrentRoute should return any', () => {
expectSnippet(`
export const selector = createSelector(
selectCurrentRoute,
route => route
);
`).toInfer(
'selector',
'MemoizedSelector<State, any, DefaultProjectorFn<any>>'
);
});

it('selectQueryParams should return Params', () => {
expectSnippet(`
export const selector = createSelector(
selectQueryParams,
params => params
);
`).toInfer(
'selector',
'MemoizedSelector<State, Params, DefaultProjectorFn<Params>>'
);
});

it('selectQueryParam should return string', () => {
expectSnippet(`
export const selectIdFromRoute = selectQueryParam('id')
export const selector = createSelector(
selectIdFromRoute,
id => id
);
`).toInfer(
'selector',
'MemoizedSelector<State, string, DefaultProjectorFn<string>>'
);
});

it('selectRouteParams should return Params', () => {
expectSnippet(`
export const selector = createSelector(
selectRouteParams,
params => params
);
`).toInfer(
'selector',
'MemoizedSelector<State, Params, DefaultProjectorFn<Params>>'
);
});

it('selectRouteParam should return string', () => {
expectSnippet(`
export const selectIdFromRoute = selectRouteParam('id')
export const selector = createSelector(
selectIdFromRoute,
id => id
);
`).toInfer(
'selector',
'MemoizedSelector<State, string, DefaultProjectorFn<string>>'
);
});

it('selectRouteData should return Data', () => {
expectSnippet(`
export const selector = createSelector(
selectRouteData,
data => data
);
`).toInfer(
'selector',
'MemoizedSelector<State, Data, DefaultProjectorFn<Data>>'
);
});

it('selectUrl should return string', () => {
expectSnippet(`
export const selector = createSelector(
selectUrl,
url => url
);
`).toInfer(
'selector',
'MemoizedSelector<State, string, DefaultProjectorFn<string>>'
);
});
});
10 changes: 10 additions & 0 deletions modules/router-store/spec/types/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const compilerOptions = () => ({
moduleResolution: 'node',
target: 'es2015',
baseUrl: '.',
experimentalDecorators: true,
paths: {
'@ngrx/store': ['./modules/store'],
'@ngrx/router-store': ['./modules/router-store'],
},
});
17 changes: 9 additions & 8 deletions modules/router-store/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Data, Params } from '@angular/router';
import { Params, Data } from '@angular/router';
import { MemoizedSelector } from '@ngrx/store';

export interface RouterStateSelectors<V> {
selectCurrentRoute: (state: V) => any;
selectQueryParams: (state: V) => Params;
selectQueryParam: (param: string) => (state: V) => string | undefined;
selectRouteParams: (state: V) => Params;
selectRouteParam: (param: string) => (state: V) => string | undefined;
selectRouteData: (state: V) => Data;
selectUrl: (state: V) => string;
selectCurrentRoute: MemoizedSelector<V, any>;
selectQueryParams: MemoizedSelector<V, Params>;
selectQueryParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectRouteParams: MemoizedSelector<V, Params>;
selectRouteParam: (param: string) => MemoizedSelector<V, string | undefined>;
selectRouteData: MemoizedSelector<V, Data>;
selectUrl: MemoizedSelector<V, string>;
}

0 comments on commit 831e1e4

Please sign in to comment.