Skip to content

Commit

Permalink
feat(router-store): return resolved title via selectTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic committed Nov 5, 2022
1 parent 0c1050a commit a85915b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
62 changes: 58 additions & 4 deletions modules/router-store/spec/router_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const mockData = {
outlet: 'primary',
routeConfig: {
path: 'login',
title: 'Login',
},
queryParams: {
ref: 'ngrx.io',
Expand Down Expand Up @@ -234,10 +233,65 @@ describe('Router State Selectors', () => {
expect(result).toEqual(state.router.state.url);
});

it('should create a selector for getting the title', () => {
const result = selectors.selectTitle(state);
describe('selectTitle', () => {
it('should return undefined when route is not defined', () => {
const title = selectors.selectTitle({
router: { state: { root: null }, navigationId: 1 },
});

expect(result).toEqual(state.router.state.routeConfig?.title);
expect(title).toBe(undefined);
});

it('should return undefined when route config is not defined', () => {
const title = selectors.selectTitle({
router: {
state: { root: { routeConfig: null } },
navigationId: 1,
},
});

expect(title).toBe(undefined);
});

it('should return undefined when title is not defined', () => {
const title = selectors.selectTitle({
router: {
state: { root: { routeConfig: {} } },
navigationId: 1,
},
});

expect(title).toBe(undefined);
});

it('should return static title', () => {
const staticTitle = 'Static Title';
const title = selectors.selectTitle({
router: {
state: { root: { routeConfig: { title: staticTitle } } },
navigationId: 1,
},
});

expect(title).toBe(staticTitle);
});

it('should return resolved title', () => {
const resolvedTitle = 'Resolved Title';
const title = selectors.selectTitle({
router: {
state: {
root: {
routeConfig: { title: class TitleResolver {} },
title: resolvedTitle,
},
},
navigationId: 1,
},
});

expect(title).toBe(resolvedTitle);
});
});
});
});
12 changes: 8 additions & 4 deletions modules/router-store/src/router_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ export function getSelectors<V extends Record<string, any>>(
selectRouterState,
(routerState) => routerState && routerState.url
);
const selectTitle = createSelector(
selectCurrentRoute,
(route) => route && route.routeConfig?.title
);
const selectTitle = createSelector(selectCurrentRoute, (route) => {
if (!route?.routeConfig) {
return undefined;
}
return typeof route.routeConfig.title === 'string'
? route.routeConfig.title // static title
: route.title; // resolved title
});

return {
selectCurrentRoute,
Expand Down
2 changes: 2 additions & 0 deletions modules/router-store/src/serializers/minimal_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface MinimalActivatedRouteSnapshot {
fragment: ActivatedRouteSnapshot['fragment'];
data: ActivatedRouteSnapshot['data'];
outlet: ActivatedRouteSnapshot['outlet'];
title: ActivatedRouteSnapshot['title'];
firstChild?: MinimalActivatedRouteSnapshot;
children: MinimalActivatedRouteSnapshot[];
}
Expand Down Expand Up @@ -37,6 +38,7 @@ export class MinimalRouterStateSerializer
data: route.data,
url: route.url,
outlet: route.outlet,
title: route.title,
routeConfig: route.routeConfig
? {
path: route.routeConfig.path,
Expand Down

0 comments on commit a85915b

Please sign in to comment.