* Minimal dependency-free hook harness (the repo has no `@testing-library/react`). * Mounts the hook in a real React 19 root under jsdom, wrapped in a real * `QueryClientProvider`, so query/mutation lifecycles run exactly as in the app.
(useHook: () => T)
| 45 | * `QueryClientProvider`, so query/mutation lifecycles run exactly as in the app. |
| 46 | */ |
| 47 | function renderHookWithClient<T>(useHook: () => T): { |
| 48 | result: () => T |
| 49 | queryClient: QueryClient |
| 50 | unmount: () => void |
| 51 | } { |
| 52 | ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 53 | const queryClient = new QueryClient({ |
| 54 | defaultOptions: { |
| 55 | queries: { retry: false }, |
| 56 | mutations: { retry: false }, |
| 57 | }, |
| 58 | }) |
| 59 | const container = document.createElement('div') |
| 60 | const root: Root = createRoot(container) |
| 61 | let latest: T |
| 62 | |
| 63 | function Probe() { |
| 64 | latest = useHook() |
| 65 | return null |
| 66 | } |
| 67 | |
| 68 | function Wrapper({ children }: { children: ReactNode }) { |
| 69 | return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 70 | } |
| 71 | |
| 72 | act(() => { |
| 73 | root.render( |
| 74 | <Wrapper> |
| 75 | <Probe /> |
| 76 | </Wrapper> |
| 77 | ) |
| 78 | }) |
| 79 | |
| 80 | return { |
| 81 | result: () => latest, |
| 82 | queryClient, |
| 83 | unmount: () => act(() => root.unmount()), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** Flush pending microtasks and the macrotask queue (query observer scheduling) inside act(). */ |
| 88 | async function flush() { |
no test coverage detected