How to test clicking on `live_patch` in live view

:wave:

I have a template like

<%= live_patch to: Routes.new_widget_live_path(@socket, DashboardLive.Show, @dashboard) do %>
  <span  id="add-widget"><%= gettext("Add Widget") %></span>
<% end %>

and I want to test clicking on the live_patch anchor tag.

Is there something like render_click which would cause a patch to be applied to the view?

view
|> element("#add-widget")
|> render_click() =~ "New Widget"

Using render_click fails since the live_patch anchor doesn’t have phx-click attribute:

1) test create widget (AppWeb.DashboardLive.ShowTest)
     test/app_web/live/dashboard/show_test.exs:28
     ** (ArgumentError) element selected by "#add-widget" does not have phx-click attribute
     code: |> render_click() =~ "New Widget"
     stacktrace:
       (phoenix_live_view 0.12.1) lib/phoenix_live_view/test/live_view_test.ex:858: Phoenix.LiveViewTest.call/2
       test/app_web/live/dashboard/show_test.exs:41: (test)

You can pass the id to live_patch:

<%= live_patch to: Routes.new_widget_live_path(@socket, DashboardLive.Show, @dashboard), id: "add-widget" do %>
  <%= gettext("Add Widget") %
<% end %>

Or better written as:

<%= live_patch gettext("Add Widget"), to: Routes.new_widget_live_path(@socket, DashboardLive.Show, @dashboard), id: "add-widget" %>
4 Likes