Helpers for testing LiveReact components and views.
Overview
LiveReact testing differs from traditional Phoenix LiveView testing in how components are rendered and inspected:
- In Phoenix LiveView testing, you use
Phoenix.LiveViewTest.render_component/2to get the final rendered HTML - In LiveReact testing,
render_component/2returns an unrendered LiveReact root element containing the React component's configuration
This module provides helpers to extract and inspect React component data from the LiveReact root element, including:
- Component name and ID
- Props passed to the component
- Event handlers and their operations
- Server-side rendering (SSR) status
- Slot content
- CSS classes
Examples
# Render a LiveReact component and inspect its properties
{:ok, view, _html} = live(conn, "/")
react = LiveReact.Test.get_react(view)
# Basic component info
assert react.component == "MyComponent"
assert react.props["title"] == "Hello"
# Event handlers
assert react.handlers["click"] == JS.push("click")
# SSR status and styling
assert react.ssr == true
assert react.class == "my-custom-class"Configuration
enable_props_diff
When set to false in your config, LiveReact will always send full props and not send diffs.
This is useful for testing scenarios where you need to inspect the complete props state
rather than just the changes.
# config/test.exs
config :live_react,
enable_props_diff: falseWhen disabled, the props field returned by get_react/2 will always contain
the complete props state, making it easier to write comprehensive tests that verify the
full component state rather than just the incremental changes.
Summary
Functions
Extracts React component information from a LiveView or HTML string.
Functions
Extracts React component information from a LiveView or HTML string.
When multiple React components are present, you can specify which one to extract using
either the :name or :id option.
Returns a map containing the component's configuration:
:component- The React component name (fromv-componentattribute):id- The unique component identifier (auto-generated or explicitly set):props- The decoded props passed to the component:handlers- Map of event handlers (v-on:*) and their operations:slots- Base64 encoded slot content:ssr- Boolean indicating if server-side rendering was performed:class- CSS classes applied to the component root element
Options
:name- Find component by name (fromv-componentattribute):id- Find component by ID
Examples
# From a LiveView, get first React component
{:ok, view, _html} = live(conn, "/")
react = LiveReact.Test.get_react(view)
# Get specific component by name
react = LiveReact.Test.get_react(view, name: "MyComponent")
# Get specific component by ID
react = LiveReact.Test.get_react(view, id: "my-component-1")