Hi all
Apologies if this question has already been answered, or if there’s a page dedicated to it in the documentation, but I could not find one.
I’m having a problem testing a live view following a call to push_navigate/2
. I am trying to navigate from a LiveView to another on the same live_session
, and the transition works as expected, but I’m having a hard time unit-testing the flow.
This is the code in which I’m pushing the navigation to another LiveView:
def handle_event("create", %{"privee_form" => params}, socket) do
changeset = get_privee_form_changeset(params, socket)
if changeset.valid? do
selected_session = Ecto.Changeset.get_field(changeset, :session_name)
{:noreply,
socket
|> push_navigate(to: ~p"/chat/#{selected_session}")}
else
{:noreply,
socket
|> assign_form(params)}
end
end
This function is invoked when the page form is submitted; as previously said, this code behaves exactly as it’s supposed to. Now, I wrote this unit test to test the flow:
test " redirect to the chat when the right session name has been selected", %{conn: conn} do
session = session_fixture()
%{session_name: session_name} = session_fixture(%{
session_name: "session_name",
recovery_phrase: "recovery_phrase"
})
{:ok, privee_selector_live, _html} =
conn
|> log_in_session(session)
|> live(~p"/privee")
result =
privee_selector_live
|> form("#privee_form", %{privee_form: %{session_name: session_name}})
|> render_submit()
assert_redirect(result, ~p"/chat/#{session_name}")
html = render(result)
assert html =~ "#{session_name}"
end
The test fails at the assert_redirect
, as the result
variable has the following value:
{:error, {:live_redirect, %{kind: :push, to: "/chat/session_name"}}}
Hence a match error is raised.
I have the following questions:
- Why the
render_submit
function is returning an{:error, _}
tuple? - How can I properly test the
push_navigate
transition?