Skip to content

Commit 18a16d4

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(router-store): serialize routeConfig inside the default serializer (#1384)
BREAKING CHANGE: The default router serializer now returns a `null` value for `routeConfig` when `routeConfig` doesn't exist on the `ActivatedRouteSnapshot` instead of an empty object. BEFORE: ```json { "routeConfig": {} } ``` AFTER: ```json { "routeConfig": null } ```
1 parent 0e38673 commit 18a16d4

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { RouterStateSnapshot } from '@angular/router';
2+
import { DefaultRouterStateSerializer } from '../src';
3+
4+
describe('serializer', () => {
5+
it('should serialize all properties', () => {
6+
const serializer = new DefaultRouterStateSerializer();
7+
const snapshot = createSnapshot();
8+
const routerState = {
9+
url: 'url',
10+
root: snapshot,
11+
} as RouterStateSnapshot;
12+
13+
const actual = serializer.serialize(routerState);
14+
const expected = {
15+
url: 'url',
16+
root: createExpectedSnapshot(),
17+
};
18+
expect(actual).toEqual(expected);
19+
});
20+
21+
it('should serialize with an empty routeConfig', () => {
22+
const serializer = new DefaultRouterStateSerializer();
23+
const snapshot = { ...createSnapshot(), routeConfig: null };
24+
const routerState = {
25+
url: 'url',
26+
root: snapshot,
27+
} as RouterStateSnapshot;
28+
29+
const actual = serializer.serialize(routerState);
30+
const expected = {
31+
url: 'url',
32+
root: {
33+
...createExpectedSnapshot(),
34+
routeConfig: null,
35+
component: undefined,
36+
},
37+
};
38+
expect(actual).toEqual(expected);
39+
});
40+
41+
it('should serialize children', () => {
42+
const serializer = new DefaultRouterStateSerializer();
43+
const snapshot = {
44+
...createSnapshot(),
45+
children: [createSnapshot('child')],
46+
};
47+
const routerState = {
48+
url: 'url',
49+
root: snapshot,
50+
} as RouterStateSnapshot;
51+
52+
const actual = serializer.serialize(routerState);
53+
54+
const expected = {
55+
url: 'url',
56+
root: {
57+
...createExpectedSnapshot(),
58+
firstChild: createExpectedSnapshot('child'),
59+
children: [createExpectedSnapshot('child')],
60+
},
61+
};
62+
63+
expect(actual).toEqual(expected);
64+
});
65+
66+
function createSnapshot(prefix = 'root'): any {
67+
return {
68+
params: `${prefix}-route.params`,
69+
paramMap: `${prefix}-route.paramMap`,
70+
data: `${prefix}-route.data`,
71+
url: `${prefix}-route.url`,
72+
outlet: `${prefix}-route.outlet`,
73+
routeConfig: {
74+
component: `${prefix}-route.routeConfig.component`,
75+
path: `${prefix}-route.routeConfig.path`,
76+
pathMatch: `${prefix}-route.routeConfig.pathMatch`,
77+
redirectTo: `${prefix}-route.routeConfig.redirectTo`,
78+
outlet: `${prefix}-route.routeConfig.outlet`,
79+
},
80+
queryParams: `${prefix}-route.queryParams`,
81+
queryParamMap: `${prefix}-route.queryParamMap`,
82+
fragment: `${prefix}-route.fragment`,
83+
root: `${prefix}-route.root`,
84+
parent: `${prefix}-route.parent`,
85+
pathFromRoot: `${prefix}-route.params`,
86+
firstChild: null,
87+
children: [],
88+
};
89+
}
90+
91+
function createExpectedSnapshot(prefix = 'root') {
92+
return {
93+
...createSnapshot(prefix),
94+
component: `${prefix}-route.routeConfig.component`,
95+
root: undefined,
96+
parent: undefined,
97+
firstChild: undefined,
98+
pathFromRoot: undefined,
99+
};
100+
}
101+
});

modules/router-store/src/serializer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ export class DefaultRouterStateSerializer
3939
data: route.data,
4040
url: route.url,
4141
outlet: route.outlet,
42-
routeConfig: {
43-
component: route.routeConfig ? route.routeConfig.component : undefined,
44-
},
42+
routeConfig: route.routeConfig
43+
? {
44+
component: route.routeConfig.component,
45+
path: route.routeConfig.path,
46+
pathMatch: route.routeConfig.pathMatch,
47+
redirectTo: route.routeConfig.redirectTo,
48+
outlet: route.routeConfig.outlet,
49+
}
50+
: null,
4551
queryParams: route.queryParams,
4652
queryParamMap: route.queryParamMap,
4753
fragment: route.fragment,

projects/ngrx.io/content/guide/migration/v7.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ AFTER:
9494
StoreRouterConnectingModule.forRoot(),
9595
```
9696

97+
### ActivatedRouteSnapshot.RouteConfig
98+
99+
The default router serializer now returns a `null` value for `routeConfig` when `routeConfig` doesn't exist on the `ActivatedRouteSnapshot` instead of an empty object.
100+
101+
BEFORE:
102+
103+
```json
104+
{
105+
"routeConfig": {}
106+
}
107+
```
108+
109+
AFTER:
110+
111+
```json
112+
{
113+
"routeConfig": null
114+
}
115+
```
116+
97117
## @ngrx/store-devtools
98118

99119
The devtools is using the new `@ngrx/store-devtools/recompute` action to recompute its state instead of the `@ngrx/store/update-reducers` action.

0 commit comments

Comments
 (0)