@@ -25,7 +25,7 @@ defmodule Phoenix.LiveViewTest.DOM do
2525 tree = LazyHTML . to_tree ( lazydoc )
2626
2727 if is_function ( error_reporter , 1 ) do
28- Tree . detect_duplicate_ids ( tree , error_reporter )
28+ detect_duplicate_ids ( lazydoc , error_reporter )
2929 end
3030
3131 { lazydoc , tree }
@@ -37,12 +37,32 @@ defmodule Phoenix.LiveViewTest.DOM do
3737 tree = LazyHTML . to_tree ( lazydoc )
3838
3939 if is_function ( error_reporter , 1 ) do
40- Tree . detect_duplicate_ids ( tree , error_reporter )
40+ detect_duplicate_ids ( lazydoc , error_reporter )
4141 end
4242
4343 { lazydoc , tree }
4444 end
4545
46+ defp detect_duplicate_ids ( lazydoc , error_reporter ) do
47+ lazydoc
48+ |> LazyHTML . query ( "[id]" )
49+ |> LazyHTML . attribute ( "id" )
50+ |> Enum . frequencies ( )
51+ |> Enum . each ( fn { id , count } ->
52+ if count > 1 do
53+ error_reporter . ( """
54+ Duplicate id found while testing LiveView: #{ id }
55+
56+ #{ lazydoc |> by_id! ( id ) |> to_tree ( ) |> Tree . inspect_html ( ) }
57+
58+ LiveView requires that all elements have unique ids, duplicate IDs will cause
59+ undefined behavior at runtime, as DOM patching will not be able to target the correct
60+ elements.
61+ """ )
62+ end
63+ end )
64+ end
65+
4666 def all ( lazy , selector ) do
4767 LazyHTML . query ( lazy , selector )
4868 end
0 commit comments