Breeze.Test (Breeze v0.4.0)

Copy Markdown View Source

Public helpers for deterministic Breeze view tests.

Breeze.Test can render a view against a fixed terminal size, dispatch input and events without starting the interactive server.

Example

defmodule MyApp.CounterTest do
  use ExUnit.Case, async: true

  test "counter snapshot" do
    session = Breeze.Test.start!(MyApp.CounterView, size: {30, 5})
    on_exit(fn -> Breeze.Test.stop(session) end)

    assert Breeze.Test.render!(session) =~ "Counter: 0"

    assert {:noreply, _focused, true} = Breeze.Test.input(session, "ArrowUp")
    assert Breeze.Test.render!(session) =~ "Counter: 1"
  end
end

Summary

Types

t()

A deterministic view-test session.

Functions

Dispatches a named event and payload directly to the view.

Dispatches a message to the view's handle_info/2 callback.

Dispatches a decoded key or input event to the view.

Returns the current metadata for the view session.

Renders the current view state and returns its terminal content.

Renders the current view state, raising on failure.

Starts a deterministic test session for view.

Starts a deterministic test session and returns it, raising on failure.

Stops a test session.

Types

t()

@type t() :: %Breeze.Test{
  pid: pid(),
  terminal: %Termite.Terminal{adapter: term(), reader: term(), size: term()}
}

A deterministic view-test session.

Functions

event(session, change, event)

@spec event(t(), term(), map()) :: term()

Dispatches a named event and payload directly to the view.

info(session, message)

@spec info(t(), term()) :: term()

Dispatches a message to the view's handle_info/2 callback.

input(session, key)

@spec input(t(), term()) :: term()

Dispatches a decoded key or input event to the view.

metadata(session)

@spec metadata(t()) :: term()

Returns the current metadata for the view session.

render(session, opts \\ [])

@spec render(t(), keyword()) :: {:ok, String.t()} | {:error, term()}

Renders the current view state and returns its terminal content.

render!(session, opts \\ [])

@spec render!(t(), keyword()) :: String.t()

Renders the current view state, raising on failure.

start(view, opts \\ [])

@spec start(module(), keyword()) :: {:ok, t()} | {:error, term()}

Starts a deterministic test session for view.

Options include:

  • :size - terminal size as {width, height} or a dimensions map. Defaults to 80x24.
  • :terminal - an existing %Termite.Terminal{}. Takes precedence over :size.
  • :theme - the theme used to render the view.
  • :start_opts - options passed to the view's mount/2 callback.
  • :global_keybindings - keybindings active for the test session.

start!(view, opts \\ [])

@spec start!(module(), keyword()) :: t()

Starts a deterministic test session and returns it, raising on failure.

stop(test)

@spec stop(t()) :: :ok

Stops a test session.