PhoenixTestDatastar (PhoenixTestDatastar v0.0.2)

Copy Markdown

A PhoenixTest driver for Datastar-powered Phoenix applications.

Provides a way to test Datastar apps through the standard PhoenixTest API. It maintains client-side signal state, dispatches HTTP requests, parses SSE responses, and applies DOM patches.

Setup

Configure your endpoint in config/test.exs:

config :phoenix_test, :endpoint, MyAppWeb.Endpoint

Or set it on the conn:

conn = Phoenix.ConnTest.build_conn() |> PhoenixTest.put_endpoint(MyAppWeb.Endpoint)

Usage

import PhoenixTest

test "counter increments", %{conn: conn} do
  conn
  |> PhoenixTestDatastar.visit("/counter")
  |> click_button("Increment")
  |> assert_has("#count", text: "1")
end

Summary

Functions

Builds a new Datastar session from a Plug.Conn.

Gets a signal value from the session.

Gets all signals from the session.

Sets a signal value in the session. Useful for test setup.

Visits a page and creates a Datastar session.

Functions

build(conn, opts \\ [])

Builds a new Datastar session from a Plug.Conn.

This creates the initial session struct. You typically don't need to call this directly - use visit/2 instead.

get_signal(session, name)

Gets a signal value from the session.

Examples

signal_value = PhoenixTestDatastar.get_signal(session, "count")

get_signals(session)

Gets all signals from the session.

Examples

all_signals = PhoenixTestDatastar.get_signals(session)

put_signal(session, name, value)

Sets a signal value in the session. Useful for test setup.

Examples

session = PhoenixTestDatastar.put_signal(session, "count", 5)

visit(conn, path, opts \\ [])

Visits a page and creates a Datastar session.

This is the entry point for Datastar tests. It makes a GET request to the given path, extracts signals from the HTML, and returns a session that can be used with standard PhoenixTest functions.