Hi All.
I’m trying to wrap my head around live view testing (and failing I have a live view page which has a phx_click on it that triggers a modal form to be displayed. It works exactly the same as if I’d used live_patch.
<%= link "New EUR Exchange Rate", to: "#",
phx_click: "new_exchange_rate",
phx_value_to_currency: "EUR"
%>
the code to handle the click is…
def handle_event("new_exchange_rate", %{"to-currency" => to_currency}, socket) do
socket =
socket
|> assign(:live_action, :new_exchange)
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:to_currency, to_currency)
|> assign(:exchange_rate, %ExchangeRate{})
{:noreply, socket}
end
The actual code works correctly in the app but then I came to start testing and hit a roadblock. My test is as follows…
{:ok, show_live, html} = live(conn, Routes.currency_show_path(conn, :show, currency))
assert show_live |> element("a", "New GBP Exchange Rate") |> render_click() =~
"Show Currency"
{:ok, show_live, html} =
show_live
|> form("#exchange-rate-form", currency: @currency_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.currency_show_path(conn, :index, currency))
end
This fails because the test can’t find the form("#exchange-rate-form"). The reason is that the result of the render_click doesn’t actually update show_live (which I assumed it would). I then tried
show_live =
show_live |> element("a", "New GBP Exchange Rate") |> render_click() =~
"Show Currency"
show_live now contains the form (as I’d expect but as pure html - not as a view - so the call to form fails.
I’m. really hoping I’m doing something daft as I don’t want to use live_patch (for various reasons). Any ideas out there that might help would be gratefully received.
cheers
Dave