Skip to content

Commit dbfcf24

Browse files
refactor: move knowledge of global params out of nav service (#538)
* refactor: move knowledge of global params out of nav service * test: improve test
1 parent bd4d0b3 commit dbfcf24

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

projects/common/src/navigation/navigation.service.test.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ describe('Navigation Service', () => {
141141
expect(router.navigate).toHaveBeenCalledWith(
142142
['root', 'child'],
143143
expect.objectContaining({
144-
// tslint:disable-next-line: no-null-keyword
145-
queryParams: { time: null },
146144
relativeTo: undefined
147145
})
148146
);
@@ -155,8 +153,6 @@ describe('Navigation Service', () => {
155153
expect(router.navigate).toHaveBeenLastCalledWith(
156154
['child'],
157155
expect.objectContaining({
158-
// tslint:disable-next-line: no-null-keyword
159-
queryParams: { time: null },
160156
relativeTo: spectator.service.getCurrentActivatedRoute()
161157
})
162158
);
@@ -199,8 +195,6 @@ describe('Navigation Service', () => {
199195
expect(spectator.service.buildNavigationParams('/services')).toEqual({
200196
path: '/services',
201197
extras: expect.objectContaining({
202-
// tslint:disable-next-line: no-null-keyword
203-
queryParams: { time: null },
204198
relativeTo: undefined
205199
})
206200
});
@@ -220,4 +214,24 @@ describe('Navigation Service', () => {
220214
})
221215
);
222216
});
217+
218+
test('propagates global query params', () => {
219+
spectator.service.navigate({
220+
navType: NavigationParamsType.InApp,
221+
path: 'root',
222+
queryParams: {
223+
global: 'foo',
224+
other: 'bar'
225+
}
226+
});
227+
228+
spectator.service.registerGlobalQueryParamKey('global');
229+
230+
spectator.service.navigate('root/child');
231+
232+
expect(spectator.service.getCurrentActivatedRoute().snapshot.url).toEqual([
233+
expect.objectContaining({ path: 'child' })
234+
]);
235+
expect(spectator.service.getCurrentActivatedRoute().snapshot.queryParams).toEqual({ global: 'foo' });
236+
});
223237
});

projects/common/src/navigation/navigation.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class NavigationService {
2727
);
2828

2929
private isFirstNavigation: boolean = true;
30+
private readonly globalQueryParams: Set<string> = new Set();
3031

3132
public constructor(
3233
private readonly router: Router,
@@ -57,6 +58,13 @@ export class NavigationService {
5758
});
5859
}
5960

61+
/**
62+
* Global query params will be preserved by default on navigation
63+
*/
64+
public registerGlobalQueryParamKey(queryParamKey: string): void {
65+
this.globalQueryParams.add(queryParamKey);
66+
}
67+
6068
public getQueryParameter(parameterName: string, defaultValue: string): string {
6169
return this.currentParamMap.has(parameterName) ? this.currentParamMap.get(parameterName)! : defaultValue;
6270
}
@@ -116,7 +124,7 @@ export class NavigationService {
116124
}
117125

118126
private buildQueryParam(preserveParameters: string[] = []): QueryParamObject {
119-
return ['time', ...preserveParameters].reduce<Params>(
127+
return [...this.globalQueryParams, ...preserveParameters].reduce<Params>(
120128
(paramObj, param) => ({
121129
...paramObj,
122130
[param]: this.currentParamMap.get(param)

projects/common/src/time/time-range.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class TimeRangeService {
2626
private readonly timeDurationService: TimeDurationService
2727
) {
2828
this.initializeTimeRange();
29+
this.navigationService.registerGlobalQueryParamKey(TimeRangeService.TIME_RANGE_QUERY_PARAM);
2930
}
3031

3132
public getShareableCurrentUrl(): string {

0 commit comments

Comments
 (0)