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

render_result()

@type render_result() :: %{
  buffer: term(),
  state: term(),
  component: module(),
  props: keyword(),
  width: non_neg_integer(),
  height: non_neg_integer()
}

Functions

assert_event(events_agent, expected_event)

@spec assert_event(pid(), term()) :: :ok

Assert that a specific event was captured.

Example

assert_event(events, :clicked)
assert_event(events, {:value_changed, 42})

assert_text(result, expected_text)

@spec assert_text(map(), String.t()) :: :ok

Assert that the rendered buffer contains specific text.

Example

assert_text(result, "Hello")

clear_events(events_agent)

@spec clear_events(pid()) :: :ok

Clear all captured events.

Example

clear_events(events_agent)

get_char_at(result, x, y)

@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_events(events_agent)

@spec get_events(pid()) :: list()

Get all captured events.

Example

events = get_events(events_agent)

get_line(result, line_number)

@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)

refute_text(result, unexpected_text)

@spec refute_text(map(), String.t()) :: :ok

Assert that the rendered buffer does not contain specific text.

Example

refute_text(result, "Error")

render_component(component, opts \\ [])

@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)

render_with_events(component, opts \\ [])

@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")

simulate_click(result, x, y)

@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_event(result, event)

@spec simulate_event(map(), map()) :: map()

Simulate any event on a rendered component.

Example

result = simulate_event(result, %{type: :focus, data: %{}})

simulate_key(result, key)

@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")