@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
tsx 95 lines 3.34 KB
Raw
1 import type { ParamListBase, StackNavigationState } from "@react-navigation/native";
2 import { act, createContext, type ComponentProps, type ReactNode } from "react";
3 import { createRoot } from "react-dom/client";
4 import type { Stack as NativeStack } from "react-native-screens";
5 import { expect, it, vi } from "vite-plus/test";
6
7 type ScreenProps = ComponentProps<typeof NativeStack.Screen>;
8 const screens = vi.hoisted(() => new Map<string, ScreenProps>());
9
10 vi.mock("react-native", () => ({
11 StyleSheet: { flatten: (style: unknown) => style },
12 View: ({ children }: { children?: ReactNode }) => children,
13 }));
14 vi.mock("react-native-screens", () => ({
15 Stack: {
16 Host: ({ children }: { children?: ReactNode }) => children,
17 Screen: (props: ScreenProps) => {
18 screens.set(props.screenKey, props);
19 return null;
20 },
21 },
22 }));
23 vi.mock("@react-navigation/native", () => ({
24 NavigationContext: createContext(undefined),
25 NavigationRouteContext: createContext(undefined),
26 usePreventRemoveContext: () => ({ preventedRoutes: {} }),
27 }));
28 vi.mock("@react-navigation/native-stack", () => ({ NativeStackView: () => null }));
29 vi.mock("./V5StackHeader.ios", () => ({ V5StackHeader: () => null }));
30 vi.mock("./NativeColumnContent.ios", () => ({ NativeColumnContent: () => null }));
31 vi.mock("./NativeScrollViewMarker", () => ({
32 NativeScrollEdgeEffectsContext: createContext(undefined),
33 }));
34
35 import { V5CardStackView, type V5StackViewProps } from "./createV5StackNavigator.ios";
36
37 const home = { key: "home", name: "Home" };
38 const thread = { key: "thread", name: "Thread" };
39
40 function fire(key: string, event: "onWillAppear" | "onWillDisappear" | "onDidDisappear") {
41 const handler = screens.get(key)?.[event] as (() => void) | undefined;
42 if (!handler) throw new Error(`No ${event} for ${key}`);
43 act(() => handler());
44 }
45
46 it("forgets native Back when a full-screen presentation covers the top card", () => {
47 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
48 const state: StackNavigationState<ParamListBase> = {
49 key: "stack",
50 type: "stack",
51 stale: false,
52 index: 1,
53 routeNames: ["Home", "Thread"],
54 routes: [home, thread],
55 preloadedRoutes: [],
56 };
57 const navigation = { getState: () => state, emit: vi.fn(), dispatch: vi.fn() };
58 const descriptor = (route: typeof home) => ({
59 route,
60 navigation,
61 options: {},
62 render: () => null,
63 });
64 const onNativePopChange = vi.fn();
65 const root = createRoot(document.createElement("div"));
66 try {
67 act(() =>
68 root.render(
69 <V5CardStackView
70 {...({
71 state,
72 navigation,
73 descriptors: { home: descriptor(home), thread: descriptor(thread) },
74 describe: () => descriptor(home),
75 } as unknown as V5StackViewProps)}
76 onNativePopChange={onNativePopChange}
77 />,
78 ),
79 );
80
81 // A presentation covers the thread: nothing in the stack appears beneath it.
82 fire("thread", "onWillDisappear");
83 fire("thread", "onDidDisappear");
84 expect(onNativePopChange).toHaveBeenLastCalledWith(null);
85
86 // Native Back reveals Home, so the thread stays the card being popped.
87 fire("thread", "onWillDisappear");
88 fire("home", "onWillAppear");
89 fire("thread", "onDidDisappear");
90 expect(onNativePopChange).toHaveBeenLastCalledWith("thread");
91 } finally {
92 act(() => root.unmount());
93 vi.unstubAllGlobals();
94 }
95 });