TermUI.Test.ComponentHarness (TermUI v0.2.0)
View SourceTest harness for isolated component testing.
Mounts a component in isolation with a test renderer, allowing event simulation and state/render inspection.
Usage
# Mount component
{:ok, harness} = ComponentHarness.mount_test(MyButton, label: "Click me")
# Render
harness = ComponentHarness.render(harness)
# Send events
harness = ComponentHarness.send_event(harness, Event.key(:enter))
# Inspect state and render
state = ComponentHarness.get_state(harness)
renderer = ComponentHarness.get_renderer(harness)
# Cleanup
ComponentHarness.unmount(harness)Component Interface
Components must implement these callbacks:
init/1- Initialize state from propsrender/1- Render component to nodeshandle_event/2(optional) - Handle events
Example Component
defmodule Counter do
def init(props) do
%{count: Keyword.get(props, :initial, 0)}
end
def render(state) do
text("Count: #{state.count}")
end
def handle_event(%Event.Key{key: :up}, state) do
{:noreply, %{state | count: state.count + 1}}
end
def handle_event(_event, state) do
{:noreply, state}
end
end
Summary
Functions
Simulates an event cycle: send event -> render -> check.
Gets the render area dimensions.
Gets all events sent (most recent first).
Gets the most recent render result.
Gets the test renderer for inspection.
Gets all render results (most recent first).
Gets the current component state.
Gets state value at path.
Mounts a component in isolation for testing.
Renders the component to the test renderer.
Simulates a render cycle: render -> wait -> check.
Resets the harness to initial state.
Sends an event to the component.
Sends multiple events in sequence.
Sets component state directly.
Checks if state has changed since last render.
Unmounts the component and cleans up resources.
Updates component state directly (for testing edge cases).
Types
Functions
Simulates an event cycle: send event -> render -> check.
Gets the render area dimensions.
Gets all events sent (most recent first).
Gets the most recent render result.
@spec get_renderer(t()) :: TermUI.Test.TestRenderer.t()
Gets the test renderer for inspection.
Gets all render results (most recent first).
Gets the current component state.
Gets state value at path.
Mounts a component in isolation for testing.
Options
:width- Renderer width (default: 80):height- Renderer height (default: 24):props- Initial props to pass to component
Examples
{:ok, harness} = ComponentHarness.mount_test(MyButton, label: "Click")
{:ok, harness} = ComponentHarness.mount_test(MyWidget, width: 40, height: 10)
Renders the component to the test renderer.
Returns the updated harness with render result stored.
Simulates a render cycle: render -> wait -> check.
Renders the component and returns the harness for assertions.
Resets the harness to initial state.
Sends an event to the component.
Returns the updated harness with new state.
Sends multiple events in sequence.
Sets component state directly.
Checks if state has changed since last render.
@spec unmount(t()) :: :ok
Unmounts the component and cleans up resources.
Updates component state directly (for testing edge cases).
Use sparingly - prefer sending events for realistic testing.