Raxol. UI. ComponentTest
(Raxol v2.6.0)
View Source
Test utilities for Raxol UI components.
Provides helpers for rendering components in isolation, simulating events, and asserting on component output.
Usage
defmodule MyComponentTest do
use ExUnit.Case
import Raxol.UI.ComponentTest
test "renders button with label" do
result = render_component(MyButton, label: "Click me")
assert_text(result, "Click me")
end
test "handles click event" do
{result, events} = render_with_events(MyButton, label: "Click")
result = simulate_click(result, 5, 1)
assert_event(events, :clicked)
end
end
Summary
Functions
Assert that a specific event was captured.
Assert that the rendered buffer contains specific text.
Clear all captured events.
Get the character at a specific position in the buffer.
Get all captured events.
Get the text content of a specific line from the buffer.
Assert that the rendered buffer does not contain specific text.
Render a component to a buffer for testing.
Render a component and capture events.
Simulate a click event on a rendered component.
Simulate any event on a rendered component.
Simulate a key press event on a rendered component.
Types
@type render_result() :: %{ buffer: term(), state: term(), component: module(), props: keyword(), width: non_neg_integer(), height: non_neg_integer() }
Functions
Assert that a specific event was captured.
Example
assert_event(events, :clicked)
assert_event(events, {:value_changed, 42})
Assert that the rendered buffer contains specific text.
Example
assert_text(result, "Hello")
@spec clear_events(pid()) :: :ok
Clear all captured events.
Example
clear_events(events_agent)
@spec get_char_at(map(), non_neg_integer(), non_neg_integer()) :: String.t() | nil
Get the character at a specific position in the buffer.
Example
char = get_char_at(result, 5, 2)
Get all captured events.
Example
events = get_events(events_agent)
@spec get_line(map(), non_neg_integer()) :: String.t()
Get the text content of a specific line from the buffer.
Example
line = get_line(result, 0)
Assert that the rendered buffer does not contain specific text.
Example
refute_text(result, "Error")
@spec render_component( module(), keyword() ) :: render_result()
Render a component to a buffer for testing.
Options
:width- Buffer width (default: 80):height- Buffer height (default: 24):props- Component props
Example
result = render_component(MyButton, label: "Click", width: 20, height: 3)
@spec render_with_events( module(), keyword() ) :: {render_result(), pid()}
Render a component and capture events.
Returns a tuple of {render_result, event_agent} where event_agent can be used to check which events were emitted.
Example
{result, events} = render_with_events(MyButton, label: "Click")
@spec simulate_click(map(), non_neg_integer(), non_neg_integer()) :: map()
Simulate a click event on a rendered component.
Example
result = simulate_click(result, x: 5, y: 1)
Simulate any event on a rendered component.
Example
result = simulate_event(result, %{type: :focus, data: %{}})
@spec simulate_key(render_result(), atom() | String.t()) :: render_result()
Simulate a key press event on a rendered component.
Example
result = simulate_key(result, :enter)
result = simulate_key(result, "a")