(instance: unknown)
| 111 | * in which case the first DOM node rendered into the test container is returned. |
| 112 | */ |
| 113 | export function findDOMNode(instance: unknown): HTMLElement | Text | null { |
| 114 | if (instance == null) { |
| 115 | return null; |
| 116 | } else if (instance instanceof Node) { |
| 117 | return instance as HTMLElement | Text; |
| 118 | } |
| 119 | |
| 120 | const fiber = (instance as { _reactInternals?: Fiber })._reactInternals; |
| 121 | if (!fiber && instance === lastRendered && mountNode) { |
| 122 | return mountNode.firstChild as HTMLElement | Text | null; |
| 123 | } |
| 124 | if (!fiber) { |
| 125 | throw Error( |
| 126 | "findDOMNode() expects a mounted class component instance or the value returned by the most recent render() call" |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | // Depth-first search for the first host (DOM) node |
| 131 | const stack: Fiber[] = fiber.child ? [fiber.child] : []; |
| 132 | while (stack.length > 0) { |
| 133 | const current = stack.pop()!; |
| 134 | if (current.stateNode instanceof Node) { |
| 135 | return current.stateNode as HTMLElement | Text; |
| 136 | } |
| 137 | if (current.sibling) { |
| 138 | stack.push(current.sibling); |
| 139 | } |
| 140 | if (current.child) { |
| 141 | stack.push(current.child); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return null; |
| 146 | } |
no outgoing calls