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
| 1 | import type { NativeStackHeaderItem } from "@react-navigation/native-stack"; |
| 2 | import { NavigationContext } from "@react-navigation/native"; |
| 3 | import { act, createContext, startTransition, Suspense, type ReactElement } from "react"; |
| 4 | import { createRoot, type Root } from "react-dom/client"; |
| 5 | import { afterEach, expect, it, vi } from "vite-plus/test"; |
| 6 | |
| 7 | type HeaderOptions = { |
| 8 | unstable_headerLeftItems?: () => NativeStackHeaderItem[]; |
| 9 | unstable_headerRightItems?: () => NativeStackHeaderItem[]; |
| 10 | unstable_headerToolbarItems?: () => NativeStackHeaderItem[]; |
| 11 | }; |
| 12 | |
| 13 | const harness = vi.hoisted(() => ({ |
| 14 | params: { path: ["first", "guide.pdf"] }, |
| 15 | appliedOptions: {} as HeaderOptions, |
| 16 | renderedItems: [] as NativeStackHeaderItem[], |
| 17 | setOptionsCalls: 0, |
| 18 | })); |
| 19 | |
| 20 | // Native headers retain the actions materialized when navigation applies options. Like React |
| 21 | // Navigation, the header renders again only when the options are no longer shallowly equal. |
| 22 | vi.mock("@react-navigation/native", async () => { |
| 23 | // vi.mock factories run before the file's imports. |
| 24 | const { createContext, use } = await vi.importActual<typeof import("react")>("react"); |
| 25 | const navigation = { |
| 26 | setOptions(options: HeaderOptions) { |
| 27 | harness.setOptionsCalls += 1; |
| 28 | const previous: Record<string, unknown> = harness.appliedOptions; |
| 29 | const next: Record<string, unknown> = { ...previous, ...options }; |
| 30 | harness.appliedOptions = next; |
| 31 | if ( |
| 32 | Object.keys(next).length === Object.keys(previous).length && |
| 33 | Object.keys(next).every((key) => next[key] === previous[key]) |
| 34 | ) |
| 35 | return; |
| 36 | harness.renderedItems = |
| 37 | options.unstable_headerLeftItems?.() ?? |
| 38 | options.unstable_headerRightItems?.() ?? |
| 39 | options.unstable_headerToolbarItems?.() ?? |
| 40 | []; |
| 41 | }, |
| 42 | }; |
| 43 | const navigationContext = createContext<typeof navigation | undefined>(navigation); |
| 44 | return { |
| 45 | // Outside a workspace tab slot the header writes to the route's navigation. |
| 46 | NavigationRouteContext: createContext(undefined), |
| 47 | NavigationContext: navigationContext, |
| 48 | useNavigation: () => |
| 49 | use(navigationContext) ?? { |
| 50 | setOptions() { |
| 51 | throw new Error("Cannot call setOptions outside a screen"); |
| 52 | }, |
| 53 | }, |
| 54 | useRoute: () => ({ key: "file", name: "ThreadFile", params: harness.params }), |
| 55 | }; |
| 56 | }); |
| 57 | |
| 58 | import { NativeHeaderToolbar } from "./NativeHeaderToolbar"; |
| 59 | import { NativeStackScreenOptions } from "./StackHeader"; |
| 60 | |
| 61 | let root: Root | null = null; |
| 62 | |
| 63 | // Like React Navigation's NavigationStateContext, whose default value throws when |
| 64 | // read. React restores a context's default outside render, so layout effects see it. |
| 65 | const ContainerStateContext = createContext<object>({ |
| 66 | get getIsInitial(): never { |
| 67 | throw new Error("Couldn't find a navigation context."); |
| 68 | }, |
| 69 | }); |
| 70 | const containerState = {}; |
| 71 | |
| 72 | function ConnectingToolbar(props: { readonly label: string }) { |
| 73 | return ( |
| 74 | <NativeHeaderToolbar placement="right"> |
| 75 | <NativeHeaderToolbar.Custom> |
| 76 | <span aria-label={props.label} /> |
| 77 | </NativeHeaderToolbar.Custom> |
| 78 | </NativeHeaderToolbar> |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | function renderConnecting(label: string) { |
| 83 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 84 | root ??= createRoot(document.createElement("div")); |
| 85 | act(() => |
| 86 | root!.render( |
| 87 | <ContainerStateContext value={containerState}> |
| 88 | <ConnectingToolbar label={label} /> |
| 89 | </ContainerStateContext>, |
| 90 | ), |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | function renderedCustomLabel() { |
| 95 | const item = harness.renderedItems[0]; |
| 96 | if (item?.type !== "custom") throw new Error("Expected the custom header element"); |
| 97 | return (item.element as ReactElement<{ "aria-label": string }>).props["aria-label"]; |
| 98 | } |
| 99 | |
| 100 | afterEach(() => { |
| 101 | act(() => root?.unmount()); |
| 102 | root = null; |
| 103 | harness.appliedOptions = {}; |
| 104 | harness.renderedItems = []; |
| 105 | harness.setOptionsCalls = 0; |
| 106 | vi.unstubAllGlobals(); |
| 107 | }); |
| 108 | |
| 109 | it("does not configure a native screen from an embedded pane", () => { |
| 110 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 111 | root = createRoot(document.createElement("div")); |
| 112 | act(() => |
| 113 | root!.render( |
| 114 | <NavigationContext value={undefined}> |
| 115 | <NativeStackScreenOptions options={{ headerShown: false }} /> |
| 116 | </NavigationContext>, |
| 117 | ), |
| 118 | ); |
| 119 | expect(harness.setOptionsCalls).toBe(0); |
| 120 | }); |
| 121 | |
| 122 | it.each(["left", "right", "bottom"] as const)( |
| 123 | "dispatches retained %s actions to the latest committed handlers without registering again", |
| 124 | (placement) => { |
| 125 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 126 | root = createRoot(document.createElement("div")); |
| 127 | const onPress = vi.fn(); |
| 128 | const render = (path: string) => |
| 129 | act(() => |
| 130 | root!.render( |
| 131 | <NativeHeaderToolbar placement={placement}> |
| 132 | <NativeHeaderToolbar.Button label="Open" onPress={() => onPress("button", path)} /> |
| 133 | <NativeHeaderToolbar.Menu title="File"> |
| 134 | <NativeHeaderToolbar.MenuAction onPress={() => onPress("menu", path)}> |
| 135 | Open |
| 136 | </NativeHeaderToolbar.MenuAction> |
| 137 | <NativeHeaderToolbar.Menu title="More"> |
| 138 | <NativeHeaderToolbar.MenuAction onPress={() => onPress("nested", path)}> |
| 139 | Open |
| 140 | </NativeHeaderToolbar.MenuAction> |
| 141 | </NativeHeaderToolbar.Menu> |
| 142 | </NativeHeaderToolbar.Menu> |
| 143 | </NativeHeaderToolbar>, |
| 144 | ), |
| 145 | ); |
| 146 | |
| 147 | render("first/guide.pdf"); |
| 148 | const [button, menu] = harness.renderedItems; |
| 149 | if (button?.type !== "button" || menu?.type !== "menu") { |
| 150 | throw new Error("Expected the native button and menu"); |
| 151 | } |
| 152 | const [action, submenu] = menu.menu.items; |
| 153 | if (action?.type !== "action" || submenu?.type !== "submenu") { |
| 154 | throw new Error("Expected the direct action and submenu"); |
| 155 | } |
| 156 | const nested = submenu.items[0]; |
| 157 | if (nested?.type !== "action") throw new Error("Expected the nested action"); |
| 158 | const press = () => { |
| 159 | button.onPress?.(); |
| 160 | action.onPress?.(); |
| 161 | nested.onPress?.(); |
| 162 | }; |
| 163 | press(); |
| 164 | expect(onPress.mock.calls).toEqual([ |
| 165 | ["button", "first/guide.pdf"], |
| 166 | ["menu", "first/guide.pdf"], |
| 167 | ["nested", "first/guide.pdf"], |
| 168 | ]); |
| 169 | onPress.mockClear(); |
| 170 | const registrations = harness.setOptionsCalls; |
| 171 | expect(registrations).toBeGreaterThan(0); |
| 172 | |
| 173 | render("second/guide.pdf"); |
| 174 | |
| 175 | expect(harness.setOptionsCalls).toBe(registrations); |
| 176 | expect(harness.renderedItems).toEqual([button, menu]); |
| 177 | press(); |
| 178 | expect(onPress.mock.calls).toEqual([ |
| 179 | ["button", "second/guide.pdf"], |
| 180 | ["menu", "second/guide.pdf"], |
| 181 | ["nested", "second/guide.pdf"], |
| 182 | ]); |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | it("applies a custom header element rendered inside a navigation container", () => { |
| 187 | renderConnecting("Connecting"); |
| 188 | expect(renderedCustomLabel()).toBe("Connecting"); |
| 189 | |
| 190 | const applied = harness.setOptionsCalls; |
| 191 | renderConnecting("Connecting"); |
| 192 | expect(harness.setOptionsCalls).toBe(applied); |
| 193 | |
| 194 | renderConnecting("Reconnecting"); |
| 195 | expect(renderedCustomLabel()).toBe("Reconnecting"); |
| 196 | }); |
| 197 | |
| 198 | it("keeps committed handlers while a replacement render suspends", () => { |
| 199 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 200 | root = createRoot(document.createElement("div")); |
| 201 | const onPress = vi.fn(); |
| 202 | const pending = new Promise<never>(() => {}); |
| 203 | function Suspend(): never { |
| 204 | throw pending; |
| 205 | } |
| 206 | const toolbar = (value: string, suspend = false) => ( |
| 207 | <Suspense fallback={null}> |
| 208 | <NativeHeaderToolbar> |
| 209 | <NativeHeaderToolbar.Button label="Open" onPress={() => onPress(value)} /> |
| 210 | </NativeHeaderToolbar> |
| 211 | {suspend && <Suspend />} |
| 212 | </Suspense> |
| 213 | ); |
| 214 | act(() => root!.render(toolbar("committed"))); |
| 215 | const button = harness.renderedItems[0]; |
| 216 | if (button?.type !== "button") throw new Error("Expected the native button"); |
| 217 | |
| 218 | act(() => startTransition(() => root!.render(toolbar("pending", true)))); |
| 219 | button.onPress?.(); |
| 220 | expect(onPress.mock.calls).toEqual([["committed"]]); |
| 221 | |
| 222 | act(() => root!.render(toolbar("replacement"))); |
| 223 | button.onPress?.(); |
| 224 | expect(onPress.mock.calls).toEqual([["committed"], ["replacement"]]); |
| 225 | }); |
| 226 | |
| 227 | it("isolates toolbar handlers and ignores retained actions after their toolbar unmounts", () => { |
| 228 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 229 | root = createRoot(document.createElement("div")); |
| 230 | const onPress = vi.fn(); |
| 231 | const toolbar = (id: string, value: string) => ( |
| 232 | <NativeHeaderToolbar key={id}> |
| 233 | <NativeHeaderToolbar.Button label={id} onPress={() => onPress(id, value)} /> |
| 234 | </NativeHeaderToolbar> |
| 235 | ); |
| 236 | act(() => root!.render([toolbar("first", "initial")])); |
| 237 | const first = harness.renderedItems[0]; |
| 238 | if (first?.type !== "button") throw new Error("Expected the first native button"); |
| 239 | act(() => root!.render([toolbar("first", "initial"), toolbar("second", "initial")])); |
| 240 | const second = harness.renderedItems[0]; |
| 241 | if (second?.type !== "button") throw new Error("Expected the second native button"); |
| 242 | |
| 243 | act(() => root!.render([toolbar("first", "updated"), toolbar("second", "updated")])); |
| 244 | first.onPress?.(); |
| 245 | second.onPress?.(); |
| 246 | expect(onPress.mock.calls).toEqual([ |
| 247 | ["first", "updated"], |
| 248 | ["second", "updated"], |
| 249 | ]); |
| 250 | |
| 251 | onPress.mockClear(); |
| 252 | act(() => root!.render([toolbar("second", "remaining")])); |
| 253 | first.onPress?.(); |
| 254 | second.onPress?.(); |
| 255 | expect(onPress.mock.calls).toEqual([["second", "remaining"]]); |
| 256 | |
| 257 | onPress.mockClear(); |
| 258 | act(() => root!.render(null)); |
| 259 | second.onPress?.(); |
| 260 | expect(onPress).not.toHaveBeenCalled(); |
| 261 | }); |