We’re patching to a LiveView but assert_patch
doesn’t seem to like it. We’re patching to the same route /
, but changing the query params.
How can we assert on patch?
Minimally Reproducible Example:
# live view
defmodule PushingParamsWeb.ParamsLive do
use PushingParamsWeb, :live_view
def mount(_params, _session, socket) do
{:ok, socket}
end
def handle_params(%{"my_param" => "true"} = params, _uri, socket) do
if connected?(socket) do
{:noreply, push_patch(socket, to: ~p"/")}
else
{:noreply, socket}
end
end
def handle_params(_params, _uri, socket) do
{:noreply, socket}
end
def render(assigns) do
~H"""
Some homepage
"""
end
end
and test:
defmodule ParamsLiveTest do
use PushingParamsWeb.ConnCase, async: true
import Phoenix.LiveViewTest
describe "push patch" do
test "remove param", %{conn: conn} do
{:ok, live, _html} = live(conn, "/?my_param=true")
assert_patch live, "/"
end
end
end
and error:
1) test push patch remove param (ParamsLiveTest)
test/pushing_params_web/live/params_live_test.exs:7
** (ArgumentError) expected PushingParamsWeb.ParamsLive to patch to "/", but got none
code: assert_patch live, "/"
stacktrace:
(phoenix_live_view 0.19.5) lib/phoenix_live_view/test/live_view_test.ex:1361: Phoenix.LiveViewTest.assert_navigation/4
(phoenix_live_view 0.19.5) lib/phoenix_live_view/test/live_view_test.ex:1254: Phoenix.LiveViewTest.assert_patch/3
test/pushing_params_web/live/params_live_test.exs:9: (test)