diff --git a/projects/common/src/navigation/navigation.service.test.ts b/projects/common/src/navigation/navigation.service.test.ts index e781ff12a..6d7a55baa 100644 --- a/projects/common/src/navigation/navigation.service.test.ts +++ b/projects/common/src/navigation/navigation.service.test.ts @@ -141,8 +141,6 @@ describe('Navigation Service', () => { expect(router.navigate).toHaveBeenCalledWith( ['root', 'child'], expect.objectContaining({ - // tslint:disable-next-line: no-null-keyword - queryParams: { time: null }, relativeTo: undefined }) ); @@ -155,8 +153,6 @@ describe('Navigation Service', () => { expect(router.navigate).toHaveBeenLastCalledWith( ['child'], expect.objectContaining({ - // tslint:disable-next-line: no-null-keyword - queryParams: { time: null }, relativeTo: spectator.service.getCurrentActivatedRoute() }) ); @@ -199,8 +195,6 @@ describe('Navigation Service', () => { expect(spectator.service.buildNavigationParams('/services')).toEqual({ path: '/services', extras: expect.objectContaining({ - // tslint:disable-next-line: no-null-keyword - queryParams: { time: null }, relativeTo: undefined }) }); @@ -220,4 +214,24 @@ describe('Navigation Service', () => { }) ); }); + + test('propagates global query params', () => { + spectator.service.navigate({ + navType: NavigationParamsType.InApp, + path: 'root', + queryParams: { + global: 'foo', + other: 'bar' + } + }); + + spectator.service.registerGlobalQueryParamKey('global'); + + spectator.service.navigate('root/child'); + + expect(spectator.service.getCurrentActivatedRoute().snapshot.url).toEqual([ + expect.objectContaining({ path: 'child' }) + ]); + expect(spectator.service.getCurrentActivatedRoute().snapshot.queryParams).toEqual({ global: 'foo' }); + }); }); diff --git a/projects/common/src/navigation/navigation.service.ts b/projects/common/src/navigation/navigation.service.ts index dac33fe6b..837164a8e 100644 --- a/projects/common/src/navigation/navigation.service.ts +++ b/projects/common/src/navigation/navigation.service.ts @@ -27,6 +27,7 @@ export class NavigationService { ); private isFirstNavigation: boolean = true; + private readonly globalQueryParams: Set = new Set(); public constructor( private readonly router: Router, @@ -57,6 +58,13 @@ export class NavigationService { }); } + /** + * Global query params will be preserved by default on navigation + */ + public registerGlobalQueryParamKey(queryParamKey: string): void { + this.globalQueryParams.add(queryParamKey); + } + public getQueryParameter(parameterName: string, defaultValue: string): string { return this.currentParamMap.has(parameterName) ? this.currentParamMap.get(parameterName)! : defaultValue; } @@ -116,7 +124,7 @@ export class NavigationService { } private buildQueryParam(preserveParameters: string[] = []): QueryParamObject { - return ['time', ...preserveParameters].reduce( + return [...this.globalQueryParams, ...preserveParameters].reduce( (paramObj, param) => ({ ...paramObj, [param]: this.currentParamMap.get(param) diff --git a/projects/common/src/time/time-range.service.ts b/projects/common/src/time/time-range.service.ts index c4398e4c4..e7b67da6c 100644 --- a/projects/common/src/time/time-range.service.ts +++ b/projects/common/src/time/time-range.service.ts @@ -26,6 +26,7 @@ export class TimeRangeService { private readonly timeDurationService: TimeDurationService ) { this.initializeTimeRange(); + this.navigationService.registerGlobalQueryParamKey(TimeRangeService.TIME_RANGE_QUERY_PARAM); } public getShareableCurrentUrl(): string {