The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 72 lines 2.88 KB
Raw
1 import { LegendList, type LegendListRef } from "@legendapp/list/react";
2 import { createRef, useState, type RefObject } from "react";
3 import { expect, it, vi } from "vite-plus/test";
4 import { act, render } from "../../test/render";
5 import { useLegendListRef } from "./useLegendListRef";
6
7 function ListProbe({
8 items,
9 listRef,
10 }: {
11 items: string[];
12 listRef: RefObject<LegendListRef | null>;
13 }) {
14 const [edges, setEdges] = useState("");
15 const ref = useLegendListRef(listRef, () => {
16 const state = listRef.current?.getState();
17 if (state) setEdges(`${state.isAtStart}:${state.isAtEnd}`);
18 });
19 return (
20 <>
21 <output aria-label="List edges">{edges}</output>
22 <LegendList
23 ref={ref}
24 data={items}
25 keyExtractor={(item) => item}
26 renderItem={({ item }) => <div style={{ height: 40 }}>{item}</div>}
27 style={{ height: 120, width: 300 }}
28 />
29 </>
30 );
31 }
32
33 it("keeps rows at their layout height when they re-render while an ancestor is scaled", async () => {
34 const items = Array.from({ length: 6 }, (_, index) => `Row ${index}`);
35 // Dialogs and popovers scale in from 0.98; a re-render mid-animation must not record scaled sizes.
36 const list = (highlighted: string) => (
37 <div style={{ scale: "0.98" }}>
38 <LegendList
39 data={items}
40 extraData={highlighted}
41 keyExtractor={(item) => item}
42 renderItem={({ item }) => <div style={{ height: 40 }}>{item}</div>}
43 style={{ height: 300, width: 300 }}
44 />
45 </div>
46 );
47 await using screen = await render(list("Row 0"));
48 await expect.element(screen.getByText("Row 5")).toBeVisible();
49 await screen.rerender(list("Row 1"));
50 await vi.waitFor(() => {
51 const first = screen.getByText("Row 0").element().getBoundingClientRect();
52 const last = screen.getByText("Row 5").element().getBoundingClientRect();
53 expect((last.top - first.top) / 0.98).toBeCloseTo(200, 0);
54 });
55 });
56
57 it("refreshes list edges after content shrinks without scrolling and after the list reopens", async () => {
58 const listRef = createRef<LegendListRef | null>();
59 const items = Array.from({ length: 20 }, (_, index) => `Row ${index}`);
60 await using screen = await render(<ListProbe items={items} listRef={listRef} />);
61 await expect.element(screen.getByLabelText("List edges")).toHaveTextContent("true:false");
62 await act(() => {
63 void listRef.current!.scrollToEnd({ animated: false });
64 });
65 await expect.element(screen.getByLabelText("List edges")).toHaveTextContent("false:true");
66 await screen.rerender(<ListProbe items={["Only row"]} listRef={listRef} />);
67 await expect.element(screen.getByLabelText("List edges")).toHaveTextContent("true:true");
68 await screen.rerender(<></>);
69 expect(listRef.current).toBeNull();
70 await screen.rerender(<ListProbe items={items} listRef={listRef} />);
71 await expect.element(screen.getByLabelText("List edges")).toHaveTextContent("true:false");
72 });