diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 2d15672d..47b950ce 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -54,7 +54,12 @@ describe('AppComponent', () => { events: { subscribe: handler => subscribeHandler = handler }, - navigateByUrl: url => navigateByUrlParams = url + navigateByUrl: url => navigateByUrlParams = url, + parseUrl: url => { + return { + fragment: (url === '') ? undefined : 'scroll-here' + }; + } } }, { @@ -103,9 +108,10 @@ describe('AppComponent', () => { }); } - // Reset skyAppConfig + // Reset skyAppConfig and scrollCalled beforeEach(() => { skyAppConfig = defaultSkyAppConfig; + scrollCalled = false; }); it('should create component', async(() => { @@ -125,6 +131,14 @@ describe('AppComponent', () => { }); })); + it('should not call scroll on NavigationEnd when a url fragment is present', async(() => { + setup(skyAppConfig).then(() => { + fixture.detectChanges(); + subscribeHandler(new NavigationEnd(0, '/#scroll-here', '/#scroll-here')); + expect(scrollCalled).toBe(false); + }); + })); + it('should not call BBOmnibar.load if config.skyux.omnibar does not exist', async(() => { let spyOmnibar = spyOn(BBOmnibar, 'load'); setup(skyAppConfig).then(() => { diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 81ff9364..0ac02eb3 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -89,10 +89,14 @@ export class AppComponent implements OnInit { public ngOnInit() { // Without this code, navigating to a new route doesn't cause the window to be - // scrolled to the top like the browser does automatically with non-SPA navigation. + // scrolled to the top like the browser does automatically with non-SPA navigation + // when no route fragment is present. this.router.events.subscribe((event: any) => { if (event instanceof NavigationEnd) { - this.windowRef.nativeWindow.scroll(0, 0); + const urlTree = this.router.parseUrl(event.url); + if (!urlTree.fragment) { + this.windowRef.nativeWindow.scroll(0, 0); + } } });