Skip to content

Commit b11ebde

Browse files
committed
create useWindowVirtualizer initialOffset test
1 parent 995c9bc commit b11ebde

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

packages/react-virtual/tests/index.test.tsx

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { beforeEach, test, expect, vi } from 'vitest'
22
import * as React from 'react'
33
import { render, screen, fireEvent } from '@testing-library/react'
44

5-
import { useVirtualizer, Range } from '../src/index'
5+
import { useVirtualizer, useWindowVirtualizer, Range } from '../src/index'
66

77
beforeEach(() => {
88
Object.defineProperties(HTMLElement.prototype, {
@@ -190,3 +190,111 @@ test('should handle handle height change', () => {
190190
rerender(<List count={1} height={200} />)
191191
expect(screen.queryByText('Row 0')).toBeInTheDocument()
192192
})
193+
194+
test('useWindowVirtualizer should not set initialOffset from window.scrollY by default', () => {
195+
// Reset window.scrollY to 0 initially
196+
Object.defineProperty(window, 'scrollY', {
197+
configurable: true,
198+
value: 0,
199+
})
200+
201+
// Mock window.scrollTo to avoid jsdom error
202+
window.scrollTo = vi.fn()
203+
204+
let virtualizerInstance: any = null
205+
206+
function WindowList() {
207+
const virtualizer = useWindowVirtualizer({
208+
count: 100,
209+
estimateSize: () => 50,
210+
overscan: 5,
211+
})
212+
213+
virtualizerInstance = virtualizer
214+
215+
return (
216+
<div
217+
style={{
218+
height: virtualizer.getTotalSize(),
219+
position: 'relative',
220+
}}
221+
>
222+
{virtualizer.getVirtualItems().map((item) => (
223+
<div
224+
key={item.key}
225+
style={{
226+
position: 'absolute',
227+
top: 0,
228+
left: 0,
229+
width: '100%',
230+
transform: `translateY(${item.start}px)`,
231+
}}
232+
>
233+
Item {item.index}
234+
</div>
235+
))}
236+
</div>
237+
)
238+
}
239+
240+
render(<WindowList />)
241+
242+
// Verify that initialOffset option is not set (should be default 0)
243+
expect(virtualizerInstance.options.initialOffset).toBe(0)
244+
245+
// The scrollOffset will match window.scrollY (0 in this case) due to the observer
246+
expect(virtualizerInstance.scrollOffset).toBe(0)
247+
})
248+
249+
test('useWindowVirtualizer should allow explicit initialOffset', () => {
250+
// Mock window.scrollY
251+
Object.defineProperty(window, 'scrollY', {
252+
configurable: true,
253+
value: 500,
254+
})
255+
256+
// Mock window.scrollTo to avoid jsdom error
257+
window.scrollTo = vi.fn()
258+
259+
let virtualizerInstance: any = null
260+
261+
function WindowListWithOffset() {
262+
const virtualizer = useWindowVirtualizer({
263+
count: 100,
264+
estimateSize: () => 50,
265+
overscan: 5,
266+
initialOffset: () => window.scrollY, // Explicitly set to preserve scroll
267+
})
268+
269+
virtualizerInstance = virtualizer
270+
271+
return (
272+
<div
273+
style={{
274+
height: virtualizer.getTotalSize(),
275+
position: 'relative',
276+
}}
277+
>
278+
{virtualizer.getVirtualItems().map((item) => (
279+
<div
280+
key={item.key}
281+
style={{
282+
position: 'absolute',
283+
top: 0,
284+
left: 0,
285+
width: '100%',
286+
transform: `translateY(${item.start}px)`,
287+
}}
288+
>
289+
Item {item.index}
290+
</div>
291+
))}
292+
</div>
293+
)
294+
}
295+
296+
render(<WindowListWithOffset />)
297+
298+
// The virtualizer should use the provided initialOffset
299+
expect(virtualizerInstance.scrollOffset ?? 500).toBe(500)
300+
})

0 commit comments

Comments
 (0)