|
| 1 | +import { AsyncResult } from "effect/reactivity"; |
| 2 | +import { act, useImperativeHandle, useRef, type ReactNode, type Ref } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +vi.mock("react-native", () => { |
| 7 | + const Slot = ({ children }: { readonly children?: ReactNode }) => <div>{children}</div>; |
| 8 | + return { |
| 9 | + View: Slot, |
| 10 | + Text: Slot, |
| 11 | + ScrollView: Slot, |
| 12 | + RefreshControl: () => null, |
| 13 | + Platform: { OS: "android", select: (options: { android: string }) => options.android }, |
| 14 | + useWindowDimensions: () => ({ width: 400, height: 800 }), |
| 15 | + FlatList: (props: { |
| 16 | + readonly ref: Ref<unknown>; |
| 17 | + readonly data: string[]; |
| 18 | + readonly renderItem: (item: { item: string; index: number }) => ReactNode; |
| 19 | + }) => { |
| 20 | + const element = useRef<HTMLDivElement>(null); |
| 21 | + useImperativeHandle(props.ref, () => ({ |
| 22 | + props: { data: props.data }, |
| 23 | + scrollToIndex: ({ index }: { index: number }) => { |
| 24 | + element.current!.scrollTop = index * 24; |
| 25 | + }, |
| 26 | + })); |
| 27 | + return ( |
| 28 | + <div ref={element} data-testid="source-lines"> |
| 29 | + {props.data.map((item, index) => ( |
| 30 | + <div key={item}>{props.renderItem({ item, index })}</div> |
| 31 | + ))} |
| 32 | + </div> |
| 33 | + ); |
| 34 | + }, |
| 35 | + }; |
| 36 | +}); |
| 37 | +vi.mock("@t3tools/mobile-source-text", () => ({ |
| 38 | + SourceText: ({ children }: { readonly children?: ReactNode }) => <span>{children}</span>, |
| 39 | +})); |
| 40 | +vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ top: 0 }) })); |
| 41 | +vi.mock("../../native/native-layout-metrics", () => ({ useNativeColumnLayoutMetrics: () => null })); |
| 42 | +vi.mock("../diffs/nativeReviewDiffSurface", () => ({ resolveNativeReviewDiffView: () => null })); |
| 43 | +vi.mock("../review/nativeReviewDiffAdapter", () => ({ createNativeReviewDiffTheme: () => ({}) })); |
| 44 | +vi.mock("../../components/ui/AppText", () => ({ AppText: () => null })); |
| 45 | +vi.mock("../../components/ui/LoadingStrip", () => ({ LoadingStrip: () => null })); |
| 46 | +vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({ |
| 47 | + useAppearancePreferences: () => ({ themeAppearance: "light" }), |
| 48 | +})); |
| 49 | +vi.mock("../settings/appearance/useAppearanceCodeSurface", () => ({ |
| 50 | + useAppearanceCodeSurface: () => ({ |
| 51 | + codeWordBreak: true, |
| 52 | + codeSurface: { rowHeight: 24, fontSize: 14, lineNumberFontSize: 12, gutterWidth: 40 }, |
| 53 | + }), |
| 54 | +})); |
| 55 | +vi.mock("../../lib/useUniwindTheme", () => ({ |
| 56 | + useUniwindTheme: () => ({ "--color-foreground": "#000000" }), |
| 57 | +})); |
| 58 | +vi.mock("./sourceHighlightingState", () => ({ sourceHighlightAtom: () => null })); |
| 59 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => AsyncResult.success(null) })); |
| 60 | + |
| 61 | +import { SourceFileSurface } from "./SourceFileSurface"; |
| 62 | + |
| 63 | +function setup() { |
| 64 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 65 | + vi.useFakeTimers(); |
| 66 | + vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16)); |
| 67 | + vi.stubGlobal("cancelAnimationFrame", (frame: ReturnType<typeof setTimeout>) => |
| 68 | + clearTimeout(frame), |
| 69 | + ); |
| 70 | + const container = document.body.appendChild(document.createElement("div")); |
| 71 | + const root = createRoot(container); |
| 72 | + const contents = Array.from({ length: 80 }, (_, index) => `line ${index + 1}`).join("\n"); |
| 73 | + return { |
| 74 | + render(selectable: boolean) { |
| 75 | + act(() => |
| 76 | + root.render( |
| 77 | + <SourceFileSurface |
| 78 | + contents={contents} |
| 79 | + path="fixture.txt" |
| 80 | + initialLine={40} |
| 81 | + selectable={selectable} |
| 82 | + />, |
| 83 | + ), |
| 84 | + ); |
| 85 | + act(() => vi.runAllTimers()); |
| 86 | + }, |
| 87 | + lineList: () => container.querySelector<HTMLDivElement>('[data-testid="source-lines"]'), |
| 88 | + [Symbol.dispose]() { |
| 89 | + act(() => root.unmount()); |
| 90 | + container.remove(); |
| 91 | + vi.unstubAllGlobals(); |
| 92 | + vi.useRealTimers(); |
| 93 | + }, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +it("restores the requested wrapped line after leaving selection mode", () => { |
| 98 | + using screen = setup(); |
| 99 | + screen.render(false); |
| 100 | + const firstList = screen.lineList()!; |
| 101 | + expect(firstList.scrollTop).toBe(39 * 24); |
| 102 | + |
| 103 | + screen.render(true); |
| 104 | + expect(screen.lineList()).toBeNull(); |
| 105 | + |
| 106 | + screen.render(false); |
| 107 | + const returnedList = screen.lineList()!; |
| 108 | + expect(returnedList).not.toBe(firstList); |
| 109 | + expect(returnedList.scrollTop).toBe(39 * 24); |
| 110 | +}); |
0 commit comments