Open History to see the last commit
1/**
2 * Commit-time observation for Preact drivers.
3 *
4 * The probe component records the hook result in a layout effect, which
5 * Preact runs synchronously when it commits a render. A source write outside
6 * `act` still schedules a real Preact render, and that render commits on the
7 * next microtask. Passive effects are different: Preact runs them after
8 * paint, and `act` drains only the ones scheduled inside it, so a `useEffect`
9 * recorder (as in `@testing-library/preact`'s `renderHook`) can miss a
10 * committed render.
11 *
12 * Rejection: the probe sits under an error boundary, as a real app's hook
13 * would. A render that throws is caught there, the boundary unmounts the
14 * failed hook (releasing its subscription), and the probe rethrows the
15 * captured error from the mount or rerender that caused it, and from every
16 * later read: the failed hook is no longer mounted. Without the
17 * boundary, Preact keeps the failed component mounted, and a later
18 * notification re-renders it outside `act`.
19 */
20import { Component, h, render } from 'preact'
21import { useLayoutEffect } from 'preact/hooks'
22import { act } from 'preact/test-utils'
23
24class RejectionBoundary extends Component<{
25 onError: (error: unknown) => void
26 children?: any
27}> {
28 state = { failed: false }
29 componentDidCatch(error: unknown) {
30 this.props.onError(error)
31 this.setState({ failed: true })
32 }
33 render() {
34 return this.state.failed ? null : this.props.children
35 }
36}
37
38export interface MountedHook<P> {
39 result: { current: any }
40 rerender: (props: P) => void
41 unmount: () => void
42}
43
44/** Mount one hook in a probe component and record each committed result. */
45export function renderHook<P>(
46 useResult: (props: P) => unknown,
47 options?: { initialProps?: P; onCommit?: (result: any) => void },
48): MountedHook<P> {
49 const container = document.createElement(`div`)
50 let committed: unknown
51 let rejection: { error: unknown } | undefined
52 const result = {
53 get current(): any {
54 if (rejection) throw rejection.error
55 return committed
56 },
57 }
58 function Probe({ props }: { props: P }) {
59 const value = useResult(props)
60 useLayoutEffect(() => {
61 committed = value
62 options?.onCommit?.(value)
63 })
64 return null
65 }
66 const unmount = () => act(() => render(null, container))
67 /** Render inside `act`, then rethrow a rejection the boundary caught. */
68 const commit = (props: P) => {
69 act(() => {
70 render(
71 h(
72 RejectionBoundary,
73 { onError: (error) => (rejection ??= { error }) },
74 h(Probe, { props }),
75 ),
76 container,
77 )
78 })
79 if (rejection) throw rejection.error
80 }
81 // A rejected first render must not leave its container mounted.
82 try {
83 commit(options?.initialProps as P)
84 } catch (error) {
85 unmount()
86 throw error
87 }
88 return { result, rerender: commit, unmount }
89}
90
91export async function settle(): Promise<void> {
92 await act(async () => {
93 await new Promise((resolve) => setTimeout(resolve, 0))
94 })
95}