def handle_event("duplicate", _params, socket) do
{:noreply, assign(socket, :duplicate, true)}
end
Heex
<% if @duplicate do %>
DUPLICATE ID
<% end %>
Haven’t tested the above, but the general plan is to set up teh JS to check the dom for all ID values, compare them against each other then trigger a server side event to toggle a flag that in turn conditionally renders a message/beacon of your choice.
You could also just set the event to trigger IO.puts as well.
Random add on answers because components solve all of lifes problems.
attr :hook, :string, default: nil, examples: [target="yourHook"]
slot :inner_block
def hook(assigns) do
~H"""
<div id={@hook} phx-hook={@hook}>
{render_slot(@inner_block)}
</div>
"""
end
attr :id, :string, default: Ecto.UUID.generate()
slot :inner_block
def component(assigns) do
~H"""
<div id={@id}>
{render_slot(@inner_block)}
</div>
"""
end
I use the page pattern for testing, which makes it really easy to have shared test behavior. So when visiting a page, my shared page-visiting code can make those types of assertions.
Even if you don’t use the page pattern, you can probably find a way to check everything. Perhaps a custom render function for your tests that delegates to whatever render function you are already using, and then checks for duplicate IDs.