Skip to content

Commit f5c4635

Browse files
authored
actually warn, don't raise on duplicate ids in LiveViewTest (#3603)
1 parent 4aa5c83 commit f5c4635

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

lib/phoenix_live_view/test/dom.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ defmodule Phoenix.LiveViewTest.DOM do
4444
case Floki.attribute(node, "id") do
4545
[id] ->
4646
if MapSet.member?(ids, id) do
47-
raise """
48-
Duplicate id found: #{id}
47+
IO.warn("""
48+
Duplicate id found while testing LiveView: #{id}
49+
50+
#{inspect_html(node)}
4951
5052
LiveView requires that all elements have unique ids, duplicate IDs will cause
5153
undefined behavior at runtime, as DOM patching will not be able to target the correct
5254
elements.
53-
"""
54-
else
55-
detect_duplicate_ids(children, MapSet.put(ids, id))
55+
""")
5656
end
5757

58+
detect_duplicate_ids(children, MapSet.put(ids, id))
59+
5860
_ ->
5961
detect_duplicate_ids(children, ids)
6062
end

test/phoenix_live_view/integrations/nested_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ defmodule Phoenix.LiveView.NestedTest do
147147
:ok = GenServer.call(view.pid, {:dynamic_child, :static})
148148

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

153153
describe "navigation helpers" do

test/phoenix_live_view/test/dom_test.exs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -292,24 +292,4 @@ defmodule Phoenix.LiveViewTest.DOMTest do
292292
%{s: "bar", streams: []}
293293
end
294294
end
295-
296-
describe "parse" do
297-
test "detects duplicate ids" do
298-
assert_raise RuntimeError, fn ->
299-
DOM.parse("""
300-
<div id="foo">
301-
<div id="foo"></div>
302-
</div>
303-
""")
304-
end
305-
end
306-
307-
test "handles declarations (issue #3594)" do
308-
assert DOM.parse("""
309-
<div id="foo">
310-
<?xml version="1.0" standalone="yes"?>
311-
</div>
312-
""")
313-
end
314-
end
315295
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Phoenix.LiveViewTest.DOMWarnTest do
2+
use ExUnit.Case, async: false
3+
4+
import ExUnit.CaptureIO
5+
6+
alias Phoenix.LiveViewTest.DOM
7+
8+
describe "parse" do
9+
test "detects duplicate ids" do
10+
assert capture_io(:stderr, fn ->
11+
DOM.parse("""
12+
<div id="foo">
13+
<div id="foo"></div>
14+
</div>
15+
""")
16+
end) =~ "Duplicate id found while testing LiveView"
17+
end
18+
19+
test "handles declarations (issue #3594)" do
20+
assert DOM.parse("""
21+
<div id="foo">
22+
<?xml version="1.0" standalone="yes"?>
23+
</div>
24+
""")
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)