Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router-store): return resolved title via selectTitle #3648

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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