Plushie.Test.WidgetCase (Plushie v0.6.0)

Copy Markdown View Source

ExUnit case template for testing stateful widgets.

Hosts the widget in a parameterized harness app and records emitted events. The widget is initialized in a setup block via init_widget/2, which starts a real Plushie session (Runtime + Bridge) connected to the renderer binary.

Usage

defmodule ColorPickerWidgetTest do
  use Plushie.Test.WidgetCase, widget: ColorPickerWidget

  setup do
    init_widget("picker")
  end

  test "drag on hue ring emits :change" do
    canvas_press("#picker", 370.0, 200.0)
    assert last_event().type == :change
    assert_in_delta last_event().data.hue, 90.0, 2.0
  end

  test "model reflects emitted data" do
    canvas_press("#picker", 370.0, 200.0)
    assert_in_delta model().hue, 90.0, 2.0
  end
end

How it works

The case template uses a static harness app module parameterized via init_arg. The harness:

  • Hosts a single widget instance in a window > column layout
  • Records the most recent %WidgetEvent{} event in model().last_event
  • Merges emitted event data into the model for direct assertions

The init_widget/2 function must be called in a setup block. It configures the widget ID and props, then starts the session. All standard test helpers (click, find!, canvas_press, model, assert_text, etc.) are available.

Widget-specific helpers

  • last_event/0 -- returns the most recent %WidgetEvent{} event emitted by the widget, or nil if none
  • events/0 -- returns all emitted events (newest first)