Open History to see the last commit
1import { beforeEach, test, expect, vi } from 'vitest'
2import * as React from 'react'
3import { render, screen, fireEvent, renderHook } from '@testing-library/react'
4β
5import { useVirtualizer, Range } from '../src/index'
6β
7beforeEach(() => {
8 Object.defineProperties(HTMLElement.prototype, {
9 scrollHeight: {
10 configurable: true,
11 get: () => Number.MAX_SAFE_INTEGER,
12 },
13 scrollWidth: {
14 configurable: true,
15 get: () => Number.MAX_SAFE_INTEGER,
16 },
17 })
18})
19β
20let renderer: vi.Mock<undefined, []>
21β
22interface ListProps {
23 count?: number
24 overscan?: number
25 height?: number
26 width?: number
27 itemSize?: number
28 rangeExtractor?: (range: Range) => number[]
29 dynamic?: boolean
30}
31β
32function List({
33 count = 200,
34 overscan,
35 height = 200,
36 width = 200,
37 itemSize,
38 rangeExtractor,
39 dynamic,
40}: ListProps) {
41 renderer()
42β
43 const parentRef = React.useRef<HTMLDivElement>(null)
44β
45 const elementRectCallbackRef = React.useRef<
46 ((rect: { height: number; width: number }) => void) | null
47 >(null)
48β
49 const rowVirtualizer = useVirtualizer({
50 count,
51 getScrollElement: () => parentRef.current,
52 estimateSize: () => 50,
53 overscan,
54 observeElementRect: (_, cb) => {
55 cb({ height, width })
56 elementRectCallbackRef.current = cb
57 },
58 measureElement: () => itemSize ?? 0,
59 rangeExtractor,
60 })
61β
62 React.useEffect(() => {
63 elementRectCallbackRef.current?.({ height, width })
64 }, [height, width])
65β
66 const measureElement = dynamic ? rowVirtualizer.measureElement : undefined
67β
68 const items = rowVirtualizer.getVirtualItems()
69β
70 return (
71 <div
72 ref={parentRef}
73 style={{ height, width, overflow: 'auto' }}
74 data-testid="scroller"
75 >
76 <div
77 style={{
78 height: rowVirtualizer.getTotalSize(),
79 width: '100%',
80 position: 'relative',
81 }}
82 >
83 {items.map((virtualRow) => (
84 <div
85 data-testid={`item-${virtualRow.key}`}
86 key={virtualRow.key}
87 data-index={virtualRow.index}
88 ref={measureElement}
89 style={{
90 position: 'absolute',
91 top: 0,
92 left: 0,
93 width: '100%',
94 transform: `translateY(${virtualRow.start}px)`,
95 height: itemSize,
96 }}
97 >
98 Row {virtualRow.index}
99 </div>
100 ))}
101 </div>
102 </div>
103 )
104}
105β
106beforeEach(() => {
107 renderer = vi.fn(() => undefined)
108})
109β
110test('should render', () => {
111 render(<List />)
112β
113 expect(screen.queryByText('Row 0')).toBeInTheDocument()
114 expect(screen.queryByText('Row 4')).toBeInTheDocument()
115 expect(screen.queryByText('Row 5')).not.toBeInTheDocument()
116β
117 expect(renderer).toHaveBeenCalledTimes(2)
118})
119β
120test('should render with overscan', () => {
121 render(<List overscan={0} />)
122β
123 expect(screen.queryByText('Row 0')).toBeInTheDocument()
124 expect(screen.queryByText('Row 3')).toBeInTheDocument()
125 expect(screen.queryByText('Row 4')).not.toBeInTheDocument()
126β
127 expect(renderer).toHaveBeenCalledTimes(2)
128})
129β
130test('should render given dynamic size', async () => {
131 render(<List itemSize={100} dynamic />)
132β
133 expect(screen.queryByText('Row 0')).toBeInTheDocument()
134 expect(screen.queryByText('Row 1')).toBeInTheDocument()
135 expect(screen.queryByText('Row 2')).toBeInTheDocument()
136 expect(screen.queryByText('Row 3')).not.toBeInTheDocument()
137β
138 expect(renderer).toHaveBeenCalledTimes(3)
139})
140β
141test('should render given dynamic size after scroll', () => {
142 render(<List itemSize={100} dynamic />)
143β
144 expect(screen.queryByText('Row 0')).toBeInTheDocument()
145 expect(screen.queryByText('Row 1')).toBeInTheDocument()
146 expect(screen.queryByText('Row 2')).toBeInTheDocument()
147 expect(screen.queryByText('Row 3')).not.toBeInTheDocument()
148β
149 expect(renderer).toHaveBeenCalledTimes(3)
150 renderer.mockReset()
151β
152 fireEvent.scroll(screen.getByTestId('scroller'), {
153 target: { scrollTop: 400 },
154 })
155β
156 expect(screen.queryByText('Row 2')).not.toBeInTheDocument()
157 expect(screen.queryByText('Row 3')).toBeInTheDocument()
158 expect(screen.queryByText('Row 6')).toBeInTheDocument()
159 expect(screen.queryByText('Row 7')).not.toBeInTheDocument()
160β
161 expect(renderer).toHaveBeenCalledTimes(2)
162})
163β
164test('should use rangeExtractor', () => {
165 render(<List rangeExtractor={() => [0, 1]} />)
166β
167 expect(screen.queryByText('Row 0')).toBeInTheDocument()
168 expect(screen.queryByText('Row 1')).toBeInTheDocument()
169 expect(screen.queryByText('Row 2')).not.toBeInTheDocument()
170})
171β
172test('should handle count change', () => {
173 const { rerender } = render(<List count={2} />)
174β
175 expect(screen.queryByText('Row 0')).toBeInTheDocument()
176 expect(screen.queryByText('Row 1')).toBeInTheDocument()
177 expect(screen.queryByText('Row 2')).not.toBeInTheDocument()
178β
179 rerender(<List count={10} />)
180β
181 expect(screen.queryByText('Row 2')).toBeInTheDocument()
182 expect(screen.queryByText('Row 4')).toBeInTheDocument()
183 expect(screen.queryByText('Row 5')).not.toBeInTheDocument()
184})
185β
186test('should handle handle height change', () => {
187 const { rerender } = render(<List count={0} height={0} />)
188β
189 expect(screen.queryByText('Row 0')).not.toBeInTheDocument()
190 rerender(<List count={1} height={200} />)
191 expect(screen.queryByText('Row 0')).toBeInTheDocument()
192})
193β
194test('should refresh the virtualizer instance when the items are changing (React Compiler)', () => {
195 const scrollElement = document.createElement('div')
196 const { rerender, result } = renderHook(
197 (props) =>
198 useVirtualizer({
199 count: props.count,
200 getScrollElement: () => scrollElement,
201 estimateSize: () => 50,
202 observeElementRect: (_, cb) => {
203 cb({ height: 100, width: 100 })
204 },
205 measureElement: () => 50,
206 }),
207 {
208 initialProps: { count: 0 },
209 },
210 )
211β
212 const initialVirtualizer = result.current
213β
214 rerender({ count: 10 })
215β
216 expect(result.current).not.toBe(initialVirtualizer)
217})