|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { renderHook } from '@testing-library/react'; |
| 19 | +import { useVirtualization } from '../useVirtualization'; |
| 20 | + |
| 21 | +describe('useVirtualization', () => { |
| 22 | + it('should return empty values when items array is empty', () => { |
| 23 | + const { result } = renderHook(() => |
| 24 | + useVirtualization({ items: [], scrollTop: 0 }) |
| 25 | + ); |
| 26 | + |
| 27 | + expect(result.current).toEqual({ |
| 28 | + visibleItems: [], |
| 29 | + paddingTop: 0, |
| 30 | + paddingBottom: 0, |
| 31 | + startIndex: 0, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should calculate visible items and padding correctly for initial state', () => { |
| 36 | + const items = Array.from({ length: 100 }, (_, i) => i); |
| 37 | + const { result } = renderHook(() => |
| 38 | + useVirtualization({ items, scrollTop: 0, itemHeight: 37, overscan: 10, visibleCount: 40 }) |
| 39 | + ); |
| 40 | + |
| 41 | + // Initial state (scrollTop = 0) |
| 42 | + // startIndex = max(0, 0 - 10) = 0 |
| 43 | + // endIndex = min(99, 0 + 40 + 10) = 50 |
| 44 | + // visibleItems = items.slice(0, 51) |
| 45 | + |
| 46 | + expect(result.current.startIndex).toBe(0); |
| 47 | + expect(result.current.visibleItems.length).toBe(51); |
| 48 | + expect(result.current.paddingTop).toBe(0); |
| 49 | + |
| 50 | + // paddingBottom = (100 - 1 - 50) * 37 = 49 * 37 = 1813 |
| 51 | + expect(result.current.paddingBottom).toBe(1813); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should calculate visible items correctly when scrolled down', () => { |
| 55 | + const items = Array.from({ length: 100 }, (_, i) => i); |
| 56 | + // Scrolled 20 items down: 20 * 37 = 740 |
| 57 | + const { result } = renderHook(() => |
| 58 | + useVirtualization({ items, scrollTop: 740, itemHeight: 37, overscan: 10, visibleCount: 40 }) |
| 59 | + ); |
| 60 | + |
| 61 | + // startIndex = max(0, 20 - 10) = 10 |
| 62 | + // endIndex = min(99, 20 + 40 + 10) = 70 |
| 63 | + |
| 64 | + expect(result.current.startIndex).toBe(10); |
| 65 | + expect(result.current.visibleItems.length).toBe(61); // 70 - 10 + 1 |
| 66 | + |
| 67 | + // paddingTop = 10 * 37 = 370 |
| 68 | + expect(result.current.paddingTop).toBe(370); |
| 69 | + |
| 70 | + // paddingBottom = (100 - 1 - 70) * 37 = 29 * 37 = 1073 |
| 71 | + expect(result.current.paddingBottom).toBe(1073); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should cap end index at total items length', () => { |
| 75 | + const items = Array.from({ length: 50 }, (_, i) => i); |
| 76 | + // Scrolled way past the bottom |
| 77 | + const { result } = renderHook(() => |
| 78 | + useVirtualization({ items, scrollTop: 5000, itemHeight: 37, overscan: 10, visibleCount: 40 }) |
| 79 | + ); |
| 80 | + |
| 81 | + // startIndex = max(0, 135 - 10) = 125 |
| 82 | + // endIndex = min(49, 135 + 40 + 10) = 49 |
| 83 | + // Wait, if startIndex > endIndex, slice will return empty array |
| 84 | + |
| 85 | + expect(result.current.startIndex).toBe(125); |
| 86 | + expect(result.current.visibleItems.length).toBe(0); |
| 87 | + expect(result.current.paddingBottom).toBe(0); |
| 88 | + }); |
| 89 | +}); |
0 commit comments