forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(docs-infra): scroll to top when navigating to new page via addres…
…s bar (angular#33344) Previously, when navigating to a new page via a link, the scroll position was correctly restored to 0, but navigating to a new page via typing the URL in the browser address bar keeps the old scroll position. This commit ensures that the scroll position is restored to 0 whenever the `ScrollService` is instantiated anew (i.e. new page navigation). The old behavior of retaining the scroll position on reload is kept by storing the old URL when leaving a page and only applying the stored scroll position if the new URL matches the stored one. Fixes angular#33260 PR Close angular#33344
- Loading branch information
1 parent
ed4d96f
commit 43ac02e
Showing
8 changed files
with
133 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { browser } from 'protractor'; | ||
import { SitePage } from './app.po'; | ||
|
||
describe('site auto-scrolling', () => { | ||
let page: SitePage; | ||
|
||
// Helpers | ||
const scrollAndWait = async (y: Parameters<SitePage['scrollTo']>[0] = 'bottom') => { | ||
await page.scrollTo(y); | ||
await browser.sleep(500); // Scroll position is stored every 250ms for performance reasons. | ||
}; | ||
|
||
beforeEach(async () => { | ||
page = new SitePage(); | ||
await page.navigateTo(''); | ||
}); | ||
|
||
it('should be initially scrolled to top', async () => { | ||
expect(await page.getScrollTop()).toBe(0); | ||
}); | ||
|
||
it('should scroll to top when navigating to a different page', async () => { | ||
await scrollAndWait(); | ||
expect(await page.getScrollTop()).not.toBe(0); | ||
|
||
await page.navigateTo('docs'); | ||
expect(await page.getScrollTop()).toBe(0); | ||
}); | ||
|
||
it('should retain the scroll position on reload', async () => { | ||
await scrollAndWait(); | ||
expect(await page.getScrollTop()).not.toBe(0); | ||
|
||
await browser.refresh(); | ||
expect(await page.getScrollTop()).not.toBe(0); | ||
}); | ||
|
||
it('should scroll to top when navigating to a different page via a link', async () => { | ||
await scrollAndWait(); | ||
expect(await page.getScrollTop()).not.toBe(0); | ||
|
||
await page.docsMenuLink.click(); | ||
expect(await page.getScrollTop()).toBe(0); | ||
}); | ||
|
||
it('should scroll to top when navigating to the same page via a link', async () => { | ||
await scrollAndWait(); | ||
expect(await page.getScrollTop()).not.toBe(0); | ||
|
||
await page.homeLink.click(); | ||
expect(await page.getScrollTop()).toBe(0); | ||
}); | ||
|
||
// TODO: Find a way to accurately emulate clicking the browser back/forward button. Apparently, | ||
// both `browser.navigate().back()` and `browser.executeScript('history.back()')` cause a full | ||
// page load, which behaves differently than clicking the browser back button (and triggering a | ||
// `popstate` event instead of a navigation). Same for `forward()`. | ||
xit('should retain the scroll position when navigating back/forward', async () => { | ||
await scrollAndWait(100); | ||
expect(await page.getScrollTop()).toBeCloseTo(100, -1); | ||
|
||
await page.navigateTo('docs'); | ||
await scrollAndWait(50); | ||
expect(await page.getScrollTop()).toBeCloseTo(50, -1); | ||
|
||
await page.navigateTo('features'); | ||
await scrollAndWait(75); | ||
expect(await page.getScrollTop()).toBeCloseTo(75, -1); | ||
|
||
// Go back. | ||
await browser.navigate().back(); | ||
expect(await page.locationPath()).toBe('/docs'); | ||
expect(await page.getScrollTop()).toBeCloseTo(50, -1); | ||
|
||
// Go back. | ||
await browser.navigate().back(); | ||
expect(await page.locationPath()).toBe('/'); | ||
expect(await page.getScrollTop()).toBeCloseTo(100, -1); | ||
|
||
// Go forward. | ||
await browser.navigate().forward(); | ||
expect(await page.locationPath()).toBe('/docs'); | ||
expect(await page.getScrollTop()).toBeCloseTo(50, -1); | ||
|
||
// Go forward. | ||
await browser.navigate().forward(); | ||
expect(await page.locationPath()).toBe('/features'); | ||
expect(await page.getScrollTop()).toBeCloseTo(75, -1); | ||
}); | ||
}); |