|
| 1 | +/** |
| 2 | + * Characterization suite for `useScrollLifecycle`'s debounced scroll command. |
| 3 | + * |
| 4 | + * `scrollToBottomSmooth` is a leading-edge debounce (10 ms window) that is |
| 5 | + * `.clear()`ed on unmount. This file pins that observable behaviour so the |
| 6 | + * underlying debounce implementation can be swapped without drifting. |
| 7 | + * |
| 8 | + * Phase 1 of the swap ran this file against the `debounce` package; phase 2 |
| 9 | + * runs it against the local `leadingEdgeDebounce` helper. Only the hook's |
| 10 | + * import changed; every assertion and timing here is unchanged. |
| 11 | + */ |
| 12 | +import { act, renderHook } from "@testing-library/react" |
| 13 | +import type { VirtuosoHandle } from "react-virtuoso" |
| 14 | + |
| 15 | +import { useScrollLifecycle } from "../useScrollLifecycle" |
| 16 | + |
| 17 | +const SMOOTH_WINDOW_MS = 10 |
| 18 | + |
| 19 | +type ScrollCall = { index: "LAST"; align: "end"; behavior: "auto" | "smooth" } |
| 20 | + |
| 21 | +function makeVirtuosoRef() { |
| 22 | + const scrollToIndex = vi.fn<(options: ScrollCall) => void>() |
| 23 | + const ref = { current: { scrollToIndex } as unknown as VirtuosoHandle } |
| 24 | + return { ref, scrollToIndex } |
| 25 | +} |
| 26 | + |
| 27 | +function makeContainerRef() { |
| 28 | + const element = document.createElement("div") |
| 29 | + document.body.appendChild(element) |
| 30 | + return { current: element } |
| 31 | +} |
| 32 | + |
| 33 | +function renderScrollLifecycle( |
| 34 | + overrides: Partial<Parameters<typeof useScrollLifecycle>[0]> = {}, |
| 35 | + virtuosoRef = makeVirtuosoRef(), |
| 36 | +) { |
| 37 | + const hook = renderHook(() => |
| 38 | + useScrollLifecycle({ |
| 39 | + virtuosoRef: virtuosoRef.ref, |
| 40 | + scrollContainerRef: makeContainerRef(), |
| 41 | + taskTs: undefined, |
| 42 | + isStreaming: false, |
| 43 | + isHidden: false, |
| 44 | + hasTask: true, |
| 45 | + ...overrides, |
| 46 | + }), |
| 47 | + ) |
| 48 | + return { ...hook, scrollToIndex: virtuosoRef.scrollToIndex } |
| 49 | +} |
| 50 | + |
| 51 | +/** Drives the hook into ANCHORED_FOLLOWING with `isAtBottomRef` set. */ |
| 52 | +function enterAnchoredFollowing(result: { current: ReturnType<typeof useScrollLifecycle> }) { |
| 53 | + act(() => { |
| 54 | + result.current.atBottomStateChangeCallback(true) |
| 55 | + }) |
| 56 | + expect(result.current.scrollPhaseRef.current).toBe("ANCHORED_FOLLOWING") |
| 57 | + expect(result.current.isAtBottomRef.current).toBe(true) |
| 58 | +} |
| 59 | + |
| 60 | +const smoothCalls = (scrollToIndex: ReturnType<typeof makeVirtuosoRef>["scrollToIndex"]) => |
| 61 | + scrollToIndex.mock.calls.filter(([options]) => options.behavior === "smooth") |
| 62 | + |
| 63 | +describe("useScrollLifecycle debounced scroll command", () => { |
| 64 | + beforeEach(() => { |
| 65 | + vi.useFakeTimers() |
| 66 | + }) |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + vi.useRealTimers() |
| 70 | + document.body.innerHTML = "" |
| 71 | + }) |
| 72 | + |
| 73 | + test("a taller row in anchored follow scrolls smoothly on the leading edge", () => { |
| 74 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 75 | + enterAnchoredFollowing(result) |
| 76 | + |
| 77 | + act(() => { |
| 78 | + result.current.handleRowHeightChange(true) |
| 79 | + }) |
| 80 | + |
| 81 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 82 | + expect(smoothCalls(scrollToIndex)[0][0]).toEqual({ index: "LAST", align: "end", behavior: "smooth" }) |
| 83 | + }) |
| 84 | + |
| 85 | + test("a burst of taller rows collapses into one smooth scroll", () => { |
| 86 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 87 | + enterAnchoredFollowing(result) |
| 88 | + |
| 89 | + act(() => { |
| 90 | + result.current.handleRowHeightChange(true) |
| 91 | + vi.advanceTimersByTime(3) |
| 92 | + result.current.handleRowHeightChange(true) |
| 93 | + vi.advanceTimersByTime(3) |
| 94 | + result.current.handleRowHeightChange(true) |
| 95 | + }) |
| 96 | + |
| 97 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 98 | + }) |
| 99 | + |
| 100 | + test("the smooth scroll is never deferred to the trailing edge", () => { |
| 101 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 102 | + enterAnchoredFollowing(result) |
| 103 | + |
| 104 | + act(() => { |
| 105 | + result.current.handleRowHeightChange(true) |
| 106 | + }) |
| 107 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 108 | + |
| 109 | + act(() => { |
| 110 | + vi.advanceTimersByTime(SMOOTH_WINDOW_MS * 100) |
| 111 | + }) |
| 112 | + |
| 113 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 114 | + }) |
| 115 | + |
| 116 | + test("the window reopens once 10 ms have elapsed since the last taller row", () => { |
| 117 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 118 | + enterAnchoredFollowing(result) |
| 119 | + |
| 120 | + act(() => { |
| 121 | + result.current.handleRowHeightChange(true) |
| 122 | + }) |
| 123 | + act(() => { |
| 124 | + vi.advanceTimersByTime(SMOOTH_WINDOW_MS) |
| 125 | + }) |
| 126 | + act(() => { |
| 127 | + result.current.handleRowHeightChange(true) |
| 128 | + }) |
| 129 | + |
| 130 | + expect(smoothCalls(scrollToIndex)).toHaveLength(2) |
| 131 | + }) |
| 132 | + |
| 133 | + test("a shorter row scrolls immediately with behavior auto, not through the debounce", () => { |
| 134 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 135 | + enterAnchoredFollowing(result) |
| 136 | + |
| 137 | + act(() => { |
| 138 | + result.current.handleRowHeightChange(false) |
| 139 | + }) |
| 140 | + |
| 141 | + expect(scrollToIndex).toHaveBeenCalledWith({ index: "LAST", align: "end", behavior: "auto" }) |
| 142 | + expect(smoothCalls(scrollToIndex)).toHaveLength(0) |
| 143 | + }) |
| 144 | + |
| 145 | + test("unmount clears the pending window so a late timer cannot scroll", () => { |
| 146 | + const { result, scrollToIndex, unmount } = renderScrollLifecycle() |
| 147 | + enterAnchoredFollowing(result) |
| 148 | + |
| 149 | + act(() => { |
| 150 | + result.current.handleRowHeightChange(true) |
| 151 | + }) |
| 152 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 153 | + |
| 154 | + unmount() |
| 155 | + |
| 156 | + // Checked before advancing: expired timers stop counting, so a removed |
| 157 | + // clear() would still pass the assertion at the end of this test. |
| 158 | + expect(vi.getTimerCount()).toBe(0) |
| 159 | + |
| 160 | + act(() => { |
| 161 | + vi.advanceTimersByTime(SMOOTH_WINDOW_MS * 100) |
| 162 | + }) |
| 163 | + |
| 164 | + expect(smoothCalls(scrollToIndex)).toHaveLength(1) |
| 165 | + }) |
| 166 | + |
| 167 | + test("no smooth scroll is issued while the user browses history", () => { |
| 168 | + const { result, scrollToIndex } = renderScrollLifecycle() |
| 169 | + |
| 170 | + act(() => { |
| 171 | + result.current.handleRowHeightChange(true) |
| 172 | + }) |
| 173 | + |
| 174 | + expect(smoothCalls(scrollToIndex)).toHaveLength(0) |
| 175 | + }) |
| 176 | +}) |
0 commit comments