|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {BrowserViewportScroller, ViewportScroller} from '../src/viewport_scroller'; |
10 | | -import {isNode} from '@angular/private/testing'; |
| 10 | +import {isNode, waitFor} from '@angular/private/testing'; |
11 | 11 |
|
12 | 12 | describe('BrowserViewportScroller', () => { |
13 | 13 | describe('setHistoryScrollRestoration', () => { |
@@ -112,6 +112,30 @@ describe('BrowserViewportScroller', () => { |
112 | 112 | cleanup(); |
113 | 113 | }); |
114 | 114 |
|
| 115 | + it('should honor the scroll offset when smooth scrolling', async () => { |
| 116 | + // Ensure the scroll behavior is smooth for this test, as the bug only occurred with smooth scrolling. |
| 117 | + document.documentElement.style.scrollBehavior = 'smooth'; |
| 118 | + |
| 119 | + const {anchorNode, cleanup} = createTallElement(); |
| 120 | + anchorNode.id = anchor; |
| 121 | + |
| 122 | + // Padding ensures the page is tall enough that the offset-adjusted target |
| 123 | + // is reachable and not clamped to the maximum scroll position. |
| 124 | + document.body.style.paddingBottom = '5000px'; |
| 125 | + // Header offset |
| 126 | + scroller.setOffset([0, 80]); |
| 127 | + |
| 128 | + scroller.scrollToAnchor(anchor); |
| 129 | + |
| 130 | + await waitFor(() => throwUnless(anchorNode.getBoundingClientRect().top).toBe(80), { |
| 131 | + timeout: 1_000, |
| 132 | + }); |
| 133 | + |
| 134 | + document.documentElement.style.scrollBehavior = ''; |
| 135 | + document.body.style.paddingBottom = ''; |
| 136 | + cleanup(); |
| 137 | + }); |
| 138 | + |
115 | 139 | function createTallElement() { |
116 | 140 | const tallItem = document.createElement('div'); |
117 | 141 | tallItem.style.height = '3000px'; |
|
0 commit comments