Extract LiveView async test helper

This commit is contained in:
Claudio Ortolina
2026-05-16 22:27:46 +01:00
parent 4fa05d6ad1
commit 90ac4fa23a
10 changed files with 90 additions and 49 deletions
-1
View File
@@ -14,7 +14,6 @@ defmodule MusicLibraryWeb.ConnCase do
import Phoenix.LiveViewTest,
# The default endpoint for testing
only: [
render_async: 1,
render_change: 1,
render_click: 3,
render_hook: 2,
+33 -1
View File
@@ -1,10 +1,42 @@
defmodule MusicLibraryWeb.LiveTestHelpers do
@moduledoc false
@moduledoc """
Shared helpers for LiveView tests.
These helpers sit alongside PhoenixTest and Phoenix.LiveViewTest so tests can
keep PhoenixTest sessions in the pipeline while still reaching for lower-level
LiveView interactions when PhoenixTest does not cover them directly.
"""
@doc """
Escapes text for assertions against rendered HTML.
"""
def escape(string) do
LazyHTML.html_escape(string)
end
@doc """
Waits for LiveView async work to finish.
Accepts either a PhoenixTest session, returning the session for continued
piping, or a raw LiveView view/element, returning the rendered HTML from
`Phoenix.LiveViewTest.render_async/2`.
"""
def render_async(
session_or_view,
timeout \\ Application.fetch_env!(:ex_unit, :assert_receive_timeout)
)
def render_async(%{view: %Phoenix.LiveViewTest.View{}} = session, timeout) do
PhoenixTest.unwrap(session, &Phoenix.LiveViewTest.render_async(&1, timeout))
end
def render_async(view_or_element, timeout) do
Phoenix.LiveViewTest.render_async(view_or_element, timeout)
end
@doc """
Dispatches a hook event to an element inside a PhoenixTest session.
"""
def trigger_hook(session, selector, hook, params \\ %{}) do
session
|> PhoenixTest.unwrap(fn view ->