|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import type { LightningElement } from '@plextv/react-lightning'; |
| 4 | + |
| 5 | +import { createTestRenderer } from './createTestRenderer'; |
| 6 | +import type { LayoutManager } from './LayoutManager'; |
| 7 | +import { type UseScrollHandlerResult, useScrollHandler } from './useScrollHandler'; |
| 8 | + |
| 9 | +const ROW = 100; |
| 10 | +const COUNT = 20; |
| 11 | +const VIEWPORT = 300; |
| 12 | + |
| 13 | +function setup() { |
| 14 | + const { render } = createTestRenderer(); |
| 15 | + const sizes = Array.from({ length: COUNT }, () => ROW); |
| 16 | + const offsetOf = (index: number) => sizes.slice(0, index).reduce((sum, size) => sum + size, 0); |
| 17 | + const totalSize = () => offsetOf(COUNT); |
| 18 | + const layoutManager = { |
| 19 | + getLayout: (index: number) => ({ |
| 20 | + offset: offsetOf(index), |
| 21 | + size: sizes[index], |
| 22 | + column: 0, |
| 23 | + crossOffset: 0, |
| 24 | + crossSize: 500, |
| 25 | + }), |
| 26 | + get totalSize() { |
| 27 | + return totalSize(); |
| 28 | + }, |
| 29 | + } as unknown as LayoutManager<unknown>; |
| 30 | + let current: UseScrollHandlerResult | undefined; |
| 31 | + |
| 32 | + function Probe({ total }: { total: number }) { |
| 33 | + current = useScrollHandler({ |
| 34 | + layoutManager, |
| 35 | + horizontal: false, |
| 36 | + viewportSize: VIEWPORT, |
| 37 | + itemAreaOffset: 0, |
| 38 | + totalContentSize: total, |
| 39 | + viewportCrossSize: 500, |
| 40 | + totalCrossSize: 500, |
| 41 | + animationDuration: 300, |
| 42 | + snapToAlignment: 'start', |
| 43 | + onEndReachedThreshold: null, |
| 44 | + paddingStart: 0, |
| 45 | + paddingEnd: 0, |
| 46 | + itemCount: COUNT, |
| 47 | + keepFocusedItemSnapped: true, |
| 48 | + }); |
| 49 | + |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + const rerender = () => render(<Probe total={totalSize()} />); |
| 54 | + |
| 55 | + rerender(); |
| 56 | + |
| 57 | + // oxlint-disable-next-line typescript/no-non-null-assertion -- set by the first render |
| 58 | + const handler = () => current!; |
| 59 | + |
| 60 | + handler().contentRef.current = { node: { x: 0, y: 0 } } as unknown as LightningElement; |
| 61 | + |
| 62 | + const focus = (index: number) => { |
| 63 | + const child = { |
| 64 | + getRelativePosition: () => ({ x: 0, y: offsetOf(index) }), |
| 65 | + node: { w: 500, h: sizes[index] }, |
| 66 | + props: {}, |
| 67 | + children: [], |
| 68 | + } as unknown as LightningElement; |
| 69 | + |
| 70 | + handler().handleChildFocused(child, index); |
| 71 | + rerender(); |
| 72 | + }; |
| 73 | + |
| 74 | + const resize = (index: number, size: number) => { |
| 75 | + sizes[index] = size; |
| 76 | + rerender(); |
| 77 | + }; |
| 78 | + |
| 79 | + return { handler, focus, resize, rerender }; |
| 80 | +} |
| 81 | + |
| 82 | +describe('useScrollHandler', () => { |
| 83 | + beforeEach(() => { |
| 84 | + vi.stubGlobal('requestAnimationFrame', () => 1); |
| 85 | + vi.stubGlobal('cancelAnimationFrame', () => {}); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + vi.unstubAllGlobals(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('keeps the focused row snapped when a scrollToIndex echoes the focus scroll', () => { |
| 93 | + const { handler, focus, resize } = setup(); |
| 94 | + |
| 95 | + focus(10); |
| 96 | + handler().scrollToIndex(10, true); |
| 97 | + resize(5, 76); |
| 98 | + |
| 99 | + expect(handler().scrollOffsetRef.current).toBe(976); |
| 100 | + }); |
| 101 | + |
| 102 | + it('lets a scroll to another row release the focused row', () => { |
| 103 | + const { handler, focus, resize } = setup(); |
| 104 | + |
| 105 | + focus(10); |
| 106 | + handler().scrollToIndex(12, true); |
| 107 | + resize(5, 76); |
| 108 | + |
| 109 | + expect(handler().scrollOffsetRef.current).toBe(1200); |
| 110 | + }); |
| 111 | +}); |
0 commit comments