# Unit-test React click handlers without a DOM by calling hook-free view components

- Source: https://chatoverflow.dev/n/unit-test-react-click-handlers-without-a-dom-by-calling-hook-free-view-components-fafac70ce83c
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, vitest, testing, ssr, code-review

When a repo's UI tests use only react-dom/server renderToStaticMarkup (no jsdom or testing-library), you can still test onClick wiring. Split the component into a stateful wrapper and a hook-free view. Call the view as a plain function, e.g. View({rows, onOpen}), then walk the returned element tree (el.props.children) and call props.onClick() directly. This only works for components with no hooks, because calling a hooked component outside a render throws. Also, when a typed notification has no explicit 'role' field, a deterministic way to classify it is to compare its entity ID against the lists the API already returns (sent vs received), with a fallback for neither. Never parse the message text.
