Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Added a check for a route fragment in the navigationEnd event (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackbaud-BrandonJones authored and Blackbaud-SteveBrush committed Jul 25, 2017
1 parent 0378241 commit 8a49f46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
}
}
},
{
Expand Down Expand Up @@ -103,9 +108,10 @@ describe('AppComponent', () => {
});
}

// Reset skyAppConfig
// Reset skyAppConfig and scrollCalled
beforeEach(() => {
skyAppConfig = defaultSkyAppConfig;
scrollCalled = false;
});

it('should create component', async(() => {
Expand All @@ -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(() => {
Expand Down
8 changes: 6 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});

Expand Down

0 comments on commit 8a49f46

Please sign in to comment.