Skip to content

Commit

Permalink
small refactor of on_error tests
Browse files Browse the repository at this point in the history
backports 3f2fe87
  • Loading branch information
SteffenDE committed Jan 27, 2025
1 parent f1b9d2b commit df82b86
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
5 changes: 5 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ import Config
config :logger, :level, :debug
config :logger, :default_handler, false

# we still support 1.14, silence logs in tests
if Version.match?(System.version(), "< 1.15.0") do
config :logger, :backends, []
end

config :phoenix_live_view, enable_expensive_runtime_checks: true
63 changes: 38 additions & 25 deletions test/phoenix_live_view/integrations/live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,30 @@ defmodule Phoenix.LiveView.LiveViewTest do
test "raises for duplicate ids when on_error: :raise", %{conn: conn} do
Process.flag(:trap_exit, true)

{:ok, view, _html} = live(conn, "/duplicate-id", on_error: :raise)
{{exception, _}, _} = catch_exit(render(view))
fun = fn ->
{:ok, view, _html} = live(conn, "/duplicate-id", on_error: :raise)
render(view)
end
assert catch_exit(fun.())
assert_receive {:EXIT, _pid, {exception, _}}
assert Exception.message(exception) =~ "Duplicate id found while testing LiveView: a"
assert_receive {:EXIT, _, _}
end

test "raises for duplicate components when on_error: :raise", %{conn: conn} do
Process.flag(:trap_exit, true)

{:ok, view, _html} = live(conn, "/dynamic-duplicate-component", on_error: :raise)
view |> element("button", "Toggle duplicate LC") |> render_click() =~ "I am LiveComponent2"

{{exception, _}, _} = catch_exit(render(view))
fun = fn ->
{:ok, view, _html} = live(conn, "/dynamic-duplicate-component", on_error: :raise)
view |> element("button", "Toggle duplicate LC") |> render_click()
render(view)
end

assert catch_exit(fun.())
assert_receive {:EXIT, _pid, {exception, _}}
message = Exception.message(exception)
assert message =~ "Duplicate live component found while testing LiveView:"
assert message =~ "I am LiveComponent2"
refute message =~ "I am a LC inside nested LV"

assert_receive {:EXIT, _, _}
end
end

Expand Down Expand Up @@ -417,34 +421,43 @@ defmodule Phoenix.LiveView.LiveViewTest do
test "raises for duplicate ids when on_error: raise" do
Process.flag(:trap_exit, true)

{:ok, view, _html} =
live_isolated(Phoenix.ConnTest.build_conn(), Phoenix.LiveViewTest.Support.DuplicateIdLive,
on_error: :raise
)
fun = fn ->
{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DuplicateIdLive,
on_error: :raise
)
# errors are detected asynchronously, so we need to render again for the message to be processed
render(view)
end

{{exception, _}, _} = catch_exit(render(view))
assert catch_exit(fun.())
assert_receive {:EXIT, _, {exception, _}}
assert Exception.message(exception) =~ "Duplicate id found while testing LiveView: a"
assert_receive {:EXIT, _, _}
end

test "raises for duplicate components when on_error: raise" do
Process.flag(:trap_exit, true)

{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DynamicDuplicateComponentLive,
on_error: :raise
)
fun = fn ->
{:ok, view, _html} =
live_isolated(
Phoenix.ConnTest.build_conn(),
Phoenix.LiveViewTest.Support.DynamicDuplicateComponentLive,
on_error: :raise
)
view |> element("button", "Toggle duplicate LC") |> render_click()
render(view)
end

view |> element("button", "Toggle duplicate LC") |> render_click() =~ "I am LiveComponent2"
# errors are detected asynchronously, so we need to render again for the message to be processed
assert catch_exit(fun.())

{{exception, _}, _} = catch_exit(render(view))
assert_receive {:EXIT, _, {exception, _}}

assert Exception.message(exception) =~
"Duplicate live component found while testing LiveView:"

assert_receive {:EXIT, _, _}
end
end

Expand Down

0 comments on commit df82b86

Please sign in to comment.