Skip to content

Commit cc04e2f

Browse files
feat(router-store): return resolved title via selectTitle (#3648)
Closes #3622 BREAKING CHANGES: Property `title: string | undefined` is added to the `MinimalActivatedRouteSnapshot` interface. BEFORE: The `MinimalActivatedRouteSnapshot` interface doesn't contain the `title` property. AFTER: The `MinimalActivatedRouteSnapshot` interface contains the required `title` property.
1 parent 0c1050a commit cc04e2f

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

modules/router-store/spec/router_selectors.spec.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const mockData = {
4141
outlet: 'primary',
4242
routeConfig: {
4343
path: 'login',
44-
title: 'Login',
4544
},
4645
queryParams: {
4746
ref: 'ngrx.io',
@@ -234,10 +233,65 @@ describe('Router State Selectors', () => {
234233
expect(result).toEqual(state.router.state.url);
235234
});
236235

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

240-
expect(result).toEqual(state.router.state.routeConfig?.title);
242+
expect(title).toBe(undefined);
243+
});
244+
245+
it('should return undefined when route config is not defined', () => {
246+
const title = selectors.selectTitle({
247+
router: {
248+
state: { root: { routeConfig: null } },
249+
navigationId: 1,
250+
},
251+
});
252+
253+
expect(title).toBe(undefined);
254+
});
255+
256+
it('should return undefined when title is not defined', () => {
257+
const title = selectors.selectTitle({
258+
router: {
259+
state: { root: { routeConfig: {} } },
260+
navigationId: 1,
261+
},
262+
});
263+
264+
expect(title).toBe(undefined);
265+
});
266+
267+
it('should return static title', () => {
268+
const staticTitle = 'Static Title';
269+
const title = selectors.selectTitle({
270+
router: {
271+
state: { root: { routeConfig: { title: staticTitle } } },
272+
navigationId: 1,
273+
},
274+
});
275+
276+
expect(title).toBe(staticTitle);
277+
});
278+
279+
it('should return resolved title', () => {
280+
const resolvedTitle = 'Resolved Title';
281+
const title = selectors.selectTitle({
282+
router: {
283+
state: {
284+
root: {
285+
routeConfig: { title: class TitleResolver {} },
286+
title: resolvedTitle,
287+
},
288+
},
289+
navigationId: 1,
290+
},
291+
});
292+
293+
expect(title).toBe(resolvedTitle);
294+
});
241295
});
242296
});
243297
});

modules/router-store/src/router_selectors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ export function getSelectors<V extends Record<string, any>>(
5858
selectRouterState,
5959
(routerState) => routerState && routerState.url
6060
);
61-
const selectTitle = createSelector(
62-
selectCurrentRoute,
63-
(route) => route && route.routeConfig?.title
64-
);
61+
const selectTitle = createSelector(selectCurrentRoute, (route) => {
62+
if (!route?.routeConfig) {
63+
return undefined;
64+
}
65+
return typeof route.routeConfig.title === 'string'
66+
? route.routeConfig.title // static title
67+
: route.title; // resolved title
68+
});
6569

6670
return {
6771
selectCurrentRoute,

modules/router-store/src/serializers/minimal_serializer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface MinimalActivatedRouteSnapshot {
99
fragment: ActivatedRouteSnapshot['fragment'];
1010
data: ActivatedRouteSnapshot['data'];
1111
outlet: ActivatedRouteSnapshot['outlet'];
12+
title: ActivatedRouteSnapshot['title'];
1213
firstChild?: MinimalActivatedRouteSnapshot;
1314
children: MinimalActivatedRouteSnapshot[];
1415
}
@@ -37,6 +38,7 @@ export class MinimalRouterStateSerializer
3738
data: route.data,
3839
url: route.url,
3940
outlet: route.outlet,
41+
title: route.title,
4042
routeConfig: route.routeConfig
4143
? {
4244
path: route.routeConfig.path,

0 commit comments

Comments
 (0)