|
| 1 | +import * as NodePath from "node:path"; |
1 | 2 | import * as NodeModule from "node:module"; |
| 3 | +import * as React from "react"; |
2 | 4 | import { act, useLayoutEffect } from "react"; |
3 | 5 | import { createRoot } from "react-dom/client"; |
4 | 6 | import { expect, it, vi } from "vite-plus/test"; |
@@ -167,3 +169,120 @@ it("keeps the parent when the maximized child's Agents sheet opens another child |
167 | 169 | expect(screen.open({ type: "thread", ...child, threadId: "grandchild" }, child)).toBe(false); |
168 | 170 | expect(screen.state).toEqual(originalState); |
169 | 171 | }); |
| 172 | + |
| 173 | +it("keeps a focused inspector registered across renders and updates its content", () => { |
| 174 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 175 | + const requireMobile = NodeModule.createRequire(import.meta.url); |
| 176 | + const listeners = { focus: new Set<() => void>(), blur: new Set<() => void>() }; |
| 177 | + let focused = true; |
| 178 | + let activeRender: (() => React.ReactNode) | undefined; |
| 179 | + const deactivate = vi.fn(() => { |
| 180 | + activeRender = undefined; |
| 181 | + }); |
| 182 | + const registerWorkspaceInspector = vi.fn((render: () => React.ReactNode) => { |
| 183 | + activeRender = render; |
| 184 | + return deactivate; |
| 185 | + }); |
| 186 | + const navigation = { |
| 187 | + isFocused: () => focused, |
| 188 | + addListener(event: "focus" | "blur", listener: () => void) { |
| 189 | + listeners[event].add(listener); |
| 190 | + return () => { |
| 191 | + listeners[event].delete(listener); |
| 192 | + }; |
| 193 | + }, |
| 194 | + }; |
| 195 | + const navigationContext = React.createContext(navigation); |
| 196 | + const routeContext = React.createContext({ key: "file" }); |
| 197 | + const babel = requireMobile("@babel/core") as { |
| 198 | + transformFileSync(file: string, options: Record<string, unknown>): { code: string }; |
| 199 | + }; |
| 200 | + const compiled = babel.transformFileSync( |
| 201 | + NodePath.resolve(__dirname, "../layout/AdaptiveWorkspaceLayout.tsx"), |
| 202 | + { |
| 203 | + babelrc: false, |
| 204 | + configFile: false, |
| 205 | + presets: [ |
| 206 | + [ |
| 207 | + requireMobile.resolve("babel-preset-expo"), |
| 208 | + { unstable_transformImportMeta: true, enableBabelRuntime: false }, |
| 209 | + ], |
| 210 | + ], |
| 211 | + caller: { |
| 212 | + name: "metro", |
| 213 | + bundler: "metro", |
| 214 | + platform: "ios", |
| 215 | + isDev: false, |
| 216 | + isServer: false, |
| 217 | + isNodeModule: false, |
| 218 | + supportsStaticESM: false, |
| 219 | + supportsReactCompiler: true, |
| 220 | + }, |
| 221 | + }, |
| 222 | + ); |
| 223 | + const exports: { |
| 224 | + useRegisterWorkspaceInspector?: (render: (() => React.ReactNode) | undefined) => void; |
| 225 | + } = {}; |
| 226 | + const load = (name: string) => { |
| 227 | + if (name === "react" || name.startsWith("react/")) return requireMobile(name); |
| 228 | + if (name === "./adaptive-workspace-context") |
| 229 | + return { |
| 230 | + useAdaptiveWorkspaceLayout: () => ({ registerWorkspaceInspector }), |
| 231 | + }; |
| 232 | + if (name === "@react-navigation/native") |
| 233 | + return { |
| 234 | + NavigationContext: navigationContext, |
| 235 | + NavigationRouteContext: routeContext, |
| 236 | + useFocusEffect: (callback: () => (() => void) | undefined) => |
| 237 | + React.useEffect(callback, [callback]), |
| 238 | + }; |
| 239 | + return {}; |
| 240 | + }; |
| 241 | + new Function("require", "exports", compiled.code)(load, exports); |
| 242 | + const useRegisterWorkspaceInspector = exports.useRegisterWorkspaceInspector!; |
| 243 | + function Screen({ render }: { render: (() => React.ReactNode) | undefined }) { |
| 244 | + useRegisterWorkspaceInspector(render); |
| 245 | + return null; |
| 246 | + } |
| 247 | + const root = createRoot(document.createElement("div")); |
| 248 | + using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 249 | + const render = (content: (() => React.ReactNode) | undefined) => |
| 250 | + act(() => root.render(<Screen render={content} />)); |
| 251 | + const first = () => "first"; |
| 252 | + render(first); |
| 253 | + registerWorkspaceInspector.mockClear(); |
| 254 | + deactivate.mockClear(); |
| 255 | + render(first); |
| 256 | + render(first); |
| 257 | + expect(registerWorkspaceInspector).not.toHaveBeenCalled(); |
| 258 | + expect(deactivate).not.toHaveBeenCalled(); |
| 259 | + const second = () => "second"; |
| 260 | + render(second); |
| 261 | + expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 262 | + const paneContainer = document.createElement("div"); |
| 263 | + const paneRoot = createRoot(paneContainer); |
| 264 | + using _pane = { [Symbol.dispose]: () => act(() => paneRoot.unmount()) }; |
| 265 | + act(() => paneRoot.render(activeRender?.())); |
| 266 | + expect(paneContainer.textContent).toBe("second"); |
| 267 | + const emit = (event: "focus" | "blur") => |
| 268 | + act(() => { |
| 269 | + focused = event === "focus"; |
| 270 | + for (const listener of listeners[event]) listener(); |
| 271 | + }); |
| 272 | + emit("blur"); |
| 273 | + expect(deactivate).toHaveBeenCalledTimes(1); |
| 274 | + registerWorkspaceInspector.mockClear(); |
| 275 | + render(first); |
| 276 | + expect(registerWorkspaceInspector).not.toHaveBeenCalled(); |
| 277 | + emit("focus"); |
| 278 | + expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 279 | + emit("focus"); |
| 280 | + expect(registerWorkspaceInspector).toHaveBeenCalledTimes(1); |
| 281 | + render(undefined); |
| 282 | + expect(activeRender).toBeUndefined(); |
| 283 | + render(second); |
| 284 | + expect(activeRender).toBeDefined(); |
| 285 | + act(() => root.unmount()); |
| 286 | + expect(activeRender).toBeUndefined(); |
| 287 | + expect(listeners.focus.size + listeners.blur.size).toBe(0); |
| 288 | +}); |
0 commit comments