|
| 1 | +import { Activity, act, useImperativeHandle, useRef, type Ref } from "react"; |
| 2 | +import { createRoot } from "react-dom/client"; |
| 3 | +import { expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { useSearchScrollRestoration } from "./useSearchScrollRestoration"; |
| 6 | +import type { WorkspaceSearchMode } from "./workspace-tabs-state"; |
| 7 | + |
| 8 | +it("preserves the live scroll position across Activity reconnections and restores cancelled search", async () => { |
| 9 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 10 | + using _globals = { [Symbol.dispose]: () => vi.unstubAllGlobals() }; |
| 11 | + let liveOffset = 0; |
| 12 | + let connections = 0; |
| 13 | + let disconnections = 0; |
| 14 | + const scroll = vi.fn((offset: number) => { |
| 15 | + liveOffset = offset; |
| 16 | + }); |
| 17 | + type ListHandle = { scrollToOffset: (offset: number) => void }; |
| 18 | + function List({ ref }: { ref: Ref<ListHandle> }) { |
| 19 | + useImperativeHandle(ref, () => { |
| 20 | + connections += 1; |
| 21 | + return { scrollToOffset: scroll }; |
| 22 | + }); |
| 23 | + return ( |
| 24 | + <div |
| 25 | + ref={(node) => { |
| 26 | + if (node === null) disconnections += 1; |
| 27 | + }} |
| 28 | + > |
| 29 | + List |
| 30 | + </div> |
| 31 | + ); |
| 32 | + } |
| 33 | + function Screen(props: { |
| 34 | + searchMode: WorkspaceSearchMode | undefined; |
| 35 | + savedOffset: number; |
| 36 | + inset: number; |
| 37 | + }) { |
| 38 | + const list = useRef<ListHandle>(null); |
| 39 | + useSearchScrollRestoration(props.searchMode, () => { |
| 40 | + list.current?.scrollToOffset(props.savedOffset - props.inset); |
| 41 | + }); |
| 42 | + return <List ref={list} />; |
| 43 | + } |
| 44 | + const root = createRoot(document.createElement("div")); |
| 45 | + using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 46 | + const input: Parameters<typeof Screen>[0] = { |
| 47 | + searchMode: "resting", |
| 48 | + savedOffset: 0, |
| 49 | + inset: 0, |
| 50 | + }; |
| 51 | + const render = async (visible: boolean) => { |
| 52 | + await act(async () => |
| 53 | + root.render( |
| 54 | + <Activity mode={visible ? "visible" : "hidden"}> |
| 55 | + <Screen {...input} /> |
| 56 | + </Activity>, |
| 57 | + ), |
| 58 | + ); |
| 59 | + }; |
| 60 | + |
| 61 | + await render(true); |
| 62 | + expect(scroll).not.toHaveBeenCalled(); |
| 63 | + liveOffset = 640; |
| 64 | + await render(false); |
| 65 | + expect(disconnections).toBeGreaterThan(0); |
| 66 | + const connectionsBeforeReturn = connections; |
| 67 | + await render(true); |
| 68 | + expect(connections).toBeGreaterThan(connectionsBeforeReturn); |
| 69 | + expect(liveOffset).toBe(640); |
| 70 | + expect(scroll).not.toHaveBeenCalled(); |
| 71 | + |
| 72 | + input.searchMode = "active"; |
| 73 | + input.savedOffset = liveOffset; |
| 74 | + await render(true); |
| 75 | + input.searchMode = "expanded"; |
| 76 | + await render(true); |
| 77 | + liveOffset = 0; |
| 78 | + input.searchMode = "resting"; |
| 79 | + input.inset = 24; |
| 80 | + await render(true); |
| 81 | + expect(liveOffset).toBe(616); |
| 82 | + expect(scroll.mock.calls).toEqual([[616]]); |
| 83 | + |
| 84 | + liveOffset = 1280; |
| 85 | + input.inset = 48; |
| 86 | + await render(true); |
| 87 | + await render(false); |
| 88 | + await render(true); |
| 89 | + expect(liveOffset).toBe(1280); |
| 90 | + expect(scroll.mock.calls).toEqual([[616]]); |
| 91 | + |
| 92 | + input.searchMode = undefined; |
| 93 | + await render(true); |
| 94 | + input.searchMode = "resting"; |
| 95 | + await render(true); |
| 96 | + expect(scroll.mock.calls).toEqual([[616]]); |
| 97 | +}); |
0 commit comments