Current section
Files
Jump to
Current section
Files
priv/templates/live_view_test.eex
defmodule <%= inspect @module %> do
@moduledoc """
Generated by Caravela from <%= inspect @domain_module %>.
Skeleton tests for the `<%= @singular %>` LiveView trio (Index /
Show / Form). One `describe` per module, one test per action —
fill in fixture setup where marked `# TODO:` and replace the
placeholder assertions with real ones for your app.
Regenerate with `mix caravela.gen.live <%= inspect @domain_module %>`.
Custom code placed below the `# --- CUSTOM ---` marker is preserved.
"""
use <%= inspect @conn_case %>, async: true
import Phoenix.LiveViewTest
alias <%= inspect @context_module %>
# --- Fixtures --------------------------------------------------------
# TODO: replace with your app's factory / fixture module. The
# generated tests below call `build_<%= @singular %>_fixture/1` with
# attribute overrides; have it insert a record through
# `<%= @context_short %>.<%= @create_fn %>/2` (or an Ecto repo
# insert) and return the struct. Keeping the function here means
# `<%= @singular %>` tests can evolve independently of app-wide
# factories.
defp build_<%= @singular %>_fixture(attrs \\ %{}) do
{:ok, entity} =
attrs
|> Enum.into(%{})
|> <%= @context_short %>.<%= @create_fn %>(build_context())
entity
end
defp build_context, do: %{current_user: nil}
# --- <%= inspect @index_module %> -------------------------------------------------
describe "index" do
test "lists <%= @plural %>", %{conn: conn} do
_entity = build_<%= @singular %>_fixture()
{:ok, _view, html} = live(conn, "<%= @index_path %>")
# TODO: assert the fixture is visible in the rendered list.
assert is_binary(html)
end
test "delete event removes a <%= @singular %>", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
{:ok, view, _html} = live(conn, "<%= @index_path %>")
# TODO: trigger the `delete` event and assert the row is gone.
#
# view
# |> element("[data-row='#{entity.id}'] button[phx-click='delete']")
# |> render_click()
#
# refute render(view) =~ entity.id
assert view
assert entity.id
end
test "new event navigates to the form", %{conn: conn} do
{:ok, view, _html} = live(conn, "<%= @index_path %>")
# TODO: dispatch the `new` event (e.g. via `live.pushEvent` from
# the Svelte component) and assert the LiveView pushed a
# navigation to the :new route.
assert view
end
end
# --- <%= inspect @show_module %> --------------------------------------------------
describe "show" do
test "renders a single <%= @singular %>", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
{:ok, _view, html} = live(conn, "<%= @index_path %>/" <> to_string(entity.id))
# TODO: assert that a user-visible attribute of `entity` is
# present in the rendered HTML.
assert is_binary(html)
end
test "redirects on missing record", %{conn: conn} do
# TODO: assert a redirect (or flash) when visiting a non-existent id.
assert conn
end
end
# --- <%= inspect @form_module %> --------------------------------------------------
describe "form — create" do
test "mount renders an empty form", %{conn: conn} do
{:ok, _view, html} = live(conn, "<%= @index_path %>/new")
assert is_binary(html)
end
test "save event persists a valid <%= @singular %>", %{conn: conn} do
{:ok, view, _html} = live(conn, "<%= @index_path %>/new")
# TODO: send a `save` event with valid params and assert the
# LiveView push-navigates to the index (or show) route.
assert view
end
test "save event rejects invalid params with structured errors", %{conn: conn} do
{:ok, view, _html} = live(conn, "<%= @index_path %>/new")
# TODO: send a `save` event with attrs missing a required field,
# assert the form re-renders with a %{code: :required} entry
# under that field (see Caravela.ChangesetTranslator).
assert view
end
end
describe "form — edit" do
test "mount prefills the form with existing data", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
{:ok, _view, html} =
live(conn, "<%= @index_path %>/" <> to_string(entity.id) <> "/edit")
assert is_binary(html)
end
test "save event updates a persisted <%= @singular %>", %{conn: conn} do
entity = build_<%= @singular %>_fixture()
{:ok, view, _html} =
live(conn, "<%= @index_path %>/" <> to_string(entity.id) <> "/edit")
# TODO: send a `save` event with new attrs; assert the update
# persisted and the LiveView navigates away.
assert view
end
end
<%= @custom_marker %>
end