diff --git a/modules/router-store/spec/BUILD b/modules/router-store/spec/BUILD index fca35ecd7e..7a8668d98e 100644 --- a/modules/router-store/spec/BUILD +++ b/modules/router-store/spec/BUILD @@ -12,6 +12,7 @@ ts_test_library( "//modules/store", "@npm//@angular/router", "@npm//rxjs", + "@npm//ts-snippet", ], ) diff --git a/modules/router-store/spec/types/selectors.spec.ts b/modules/router-store/spec/types/selectors.spec.ts new file mode 100644 index 0000000000..3f5a4e9159 --- /dev/null +++ b/modules/router-store/spec/types/selectors.spec.ts @@ -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; + } + + export const selectRouter = createFeatureSelector< + State, + fromRouter.RouterReducerState + >('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>' + ); + }); + + it('selectQueryParams should return Params', () => { + expectSnippet(` + export const selector = createSelector( + selectQueryParams, + params => params + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); + + it('selectQueryParam should return string', () => { + expectSnippet(` + export const selectIdFromRoute = selectQueryParam('id') + export const selector = createSelector( + selectIdFromRoute, + id => id + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); + + it('selectRouteParams should return Params', () => { + expectSnippet(` + export const selector = createSelector( + selectRouteParams, + params => params + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); + + it('selectRouteParam should return string', () => { + expectSnippet(` + export const selectIdFromRoute = selectRouteParam('id') + export const selector = createSelector( + selectIdFromRoute, + id => id + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); + + it('selectRouteData should return Data', () => { + expectSnippet(` + export const selector = createSelector( + selectRouteData, + data => data + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); + + it('selectUrl should return string', () => { + expectSnippet(` + export const selector = createSelector( + selectUrl, + url => url + ); + `).toInfer( + 'selector', + 'MemoizedSelector>' + ); + }); +}); diff --git a/modules/router-store/spec/types/utils.ts b/modules/router-store/spec/types/utils.ts new file mode 100644 index 0000000000..407985e92d --- /dev/null +++ b/modules/router-store/spec/types/utils.ts @@ -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'], + }, +}); diff --git a/modules/router-store/src/models.ts b/modules/router-store/src/models.ts index dbbaf54bc2..c147f918ad 100644 --- a/modules/router-store/src/models.ts +++ b/modules/router-store/src/models.ts @@ -1,11 +1,12 @@ -import { Data, Params } from '@angular/router'; +import { Params, Data } from '@angular/router'; +import { MemoizedSelector } from '@ngrx/store'; export interface RouterStateSelectors { - 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; + selectQueryParams: MemoizedSelector; + selectQueryParam: (param: string) => MemoizedSelector; + selectRouteParams: MemoizedSelector; + selectRouteParam: (param: string) => MemoizedSelector; + selectRouteData: MemoizedSelector; + selectUrl: MemoizedSelector; }