Skip to content

Commit

Permalink
actually warn, don't raise on duplicate ids in LiveViewTest (#3603)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE authored Dec 26, 2024
1 parent 4aa5c83 commit f5c4635
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
12 changes: 7 additions & 5 deletions lib/phoenix_live_view/test/dom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ defmodule Phoenix.LiveViewTest.DOM do
case Floki.attribute(node, "id") do
[id] ->
if MapSet.member?(ids, id) do
raise """
Duplicate id found: #{id}
IO.warn("""
Duplicate id found while testing LiveView: #{id}
#{inspect_html(node)}
LiveView requires that all elements have unique ids, duplicate IDs will cause
undefined behavior at runtime, as DOM patching will not be able to target the correct
elements.
"""
else
detect_duplicate_ids(children, MapSet.put(ids, id))
""")
end

detect_duplicate_ids(children, MapSet.put(ids, id))

_ ->
detect_duplicate_ids(children, ids)
end
Expand Down
2 changes: 1 addition & 1 deletion test/phoenix_live_view/integrations/nested_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ defmodule Phoenix.LiveView.NestedTest do
:ok = GenServer.call(view.pid, {:dynamic_child, :static})

assert Exception.format(:exit, catch_exit(render(view))) =~
"Duplicate id found: static"
"expected selector \"#static\" to return a single element, but got 2"
end

describe "navigation helpers" do
Expand Down
20 changes: 0 additions & 20 deletions test/phoenix_live_view/test/dom_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -292,24 +292,4 @@ defmodule Phoenix.LiveViewTest.DOMTest do
%{s: "bar", streams: []}
end
end

describe "parse" do
test "detects duplicate ids" do
assert_raise RuntimeError, fn ->
DOM.parse("""
<div id="foo">
<div id="foo"></div>
</div>
""")
end
end

test "handles declarations (issue #3594)" do
assert DOM.parse("""
<div id="foo">
<?xml version="1.0" standalone="yes"?>
</div>
""")
end
end
end
27 changes: 27 additions & 0 deletions test/phoenix_live_view/test/dom_warn_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Phoenix.LiveViewTest.DOMWarnTest do
use ExUnit.Case, async: false

import ExUnit.CaptureIO

alias Phoenix.LiveViewTest.DOM

describe "parse" do
test "detects duplicate ids" do
assert capture_io(:stderr, fn ->
DOM.parse("""
<div id="foo">
<div id="foo"></div>
</div>
""")
end) =~ "Duplicate id found while testing LiveView"
end

test "handles declarations (issue #3594)" do
assert DOM.parse("""
<div id="foo">
<?xml version="1.0" standalone="yes"?>
</div>
""")
end
end
end

0 comments on commit f5c4635

Please sign in to comment.