From 531e5e9aa68d7a690a1b6791239e1ba18f163f6c Mon Sep 17 00:00:00 2001 From: German Velasco Date: Sat, 10 Feb 2024 11:04:34 -0500 Subject: [PATCH] Handle form redirects in Live pages (#21) Closes #19 What changed? ============ Our `Live` implementation doesn't handle form redirects, be they to another LiveView with `push_navigate/2` or to a static page via `redirect/2`. This commit updates the implementation to follow redirects the same way we follow redirects when we click on links. Testing note ----------- We also create a new describe block to encapsulate all the testing for `fill_form/3` + `click_button/2`. --- lib/phoenix_test/live.ex | 48 ++++++-------------- test/phoenix_test/live_test.exs | 80 +++++++++++++++++++++++---------- test/support/index_live.ex | 24 ++++++++++ 3 files changed, 94 insertions(+), 58 deletions(-) diff --git a/lib/phoenix_test/live.ex b/lib/phoenix_test/live.ex index 57e728da..a1dedb35 100644 --- a/lib/phoenix_test/live.ex +++ b/lib/phoenix_test/live.ex @@ -42,19 +42,10 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do end def click_link(session, selector, text) do - result = - session.view - |> element(selector, text) - |> render_click() - |> maybe_redirect(session) - - case result do - {:ok, view, _} -> - %{session | view: view} - - {:static_view, conn, path} -> - PhoenixTest.visit(conn, path) - end + session.view + |> element(selector, text) + |> render_click() + |> maybe_redirect(session) end def click_button(session, text) do @@ -93,24 +84,14 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do session.view |> form(form.selector, form.form_data) |> render_submit() - - session + |> maybe_redirect(session) end defp regular_click(session, selector, text) do - result = - session.view - |> element(selector, text) - |> render_click() - |> maybe_redirect(session) - - case result do - {:ok, view, _} -> - %{session | view: view} - - {:static_view, conn, path} -> - PhoenixTest.visit(conn, path) - end + session.view + |> element(selector, text) + |> render_click() + |> maybe_redirect(session) end def fill_form(session, selector, form_data) do @@ -148,8 +129,7 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do session.view |> form(selector, form_data) |> render_submit() - - session + |> maybe_redirect(session) end def render_html(%{view: view}) do @@ -157,15 +137,15 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do end defp maybe_redirect({:error, {:redirect, %{to: path}}}, session) do - {:static_view, session.conn, path} + PhoenixTest.visit(session.conn, path) end defp maybe_redirect({:error, {:live_redirect, _}} = result, session) do - result - |> follow_redirect(session.conn) + {:ok, view, _} = follow_redirect(result, session.conn) + %{session | view: view} end defp maybe_redirect(html, session) when is_binary(html) do - {:ok, session.view, html} + session end end diff --git a/test/phoenix_test/live_test.exs b/test/phoenix_test/live_test.exs index 1d34721d..e87a47d6 100644 --- a/test/phoenix_test/live_test.exs +++ b/test/phoenix_test/live_test.exs @@ -126,22 +126,6 @@ defmodule PhoenixTest.LiveTest do assert starting_html == ending_html end - test "can handle forms with inputs, checkboxes, selects, textboxes", %{conn: conn} do - conn - |> visit("/live/index") - |> fill_form("#full-form", - name: "Aragorn", - admin: "on", - race: "human", - notes: "King of Gondor" - ) - |> click_button("Save") - |> assert_has("#form-data", "name: Aragorn") - |> assert_has("#form-data", "admin: on") - |> assert_has("#form-data", "race: human") - |> assert_has("#form-data", "notes: King of Gondor") - end - test "triggers a phx-change event on a form (when it has one)", %{conn: conn} do conn |> visit("/live/index") @@ -149,14 +133,6 @@ defmodule PhoenixTest.LiveTest do |> assert_has("#form-errors", "Errors present") end - test "can be combined with click_button to submit a form", %{conn: conn} do - conn - |> visit("/live/index") - |> fill_form("#email-form", email: "some@example.com") - |> click_button("Save") - |> assert_has("#form-data", "email: some@example.com") - end - test "raises an error when form can't be found with selector", %{conn: conn} do assert_raise ArgumentError, ~r/Could not find element with selector/, fn -> conn @@ -182,6 +158,48 @@ defmodule PhoenixTest.LiveTest do end end + describe "fill_form + click_button" do + test "fill_form can be combined with click_button to submit a form", %{conn: conn} do + conn + |> visit("/live/index") + |> fill_form("#email-form", email: "some@example.com") + |> click_button("Save") + |> assert_has("#form-data", "email: some@example.com") + end + + test "can handle forms with inputs, checkboxes, selects, textboxes", %{conn: conn} do + conn + |> visit("/live/index") + |> fill_form("#full-form", + name: "Aragorn", + admin: "on", + race: "human", + notes: "King of Gondor" + ) + |> click_button("Save") + |> assert_has("#form-data", "name: Aragorn") + |> assert_has("#form-data", "admin: on") + |> assert_has("#form-data", "race: human") + |> assert_has("#form-data", "notes: King of Gondor") + end + + test "follows form's redirect to live page", %{conn: conn} do + conn + |> visit("/live/index") + |> fill_form("#redirect-form", name: "Aragorn") + |> click_button("#redirect-form-submit", "Save") + |> assert_has("h1", "LiveView page 2") + end + + test "follows form's redirect to static page", %{conn: conn} do + conn + |> visit("/live/index") + |> fill_form("#redirect-form-to-static", name: "Aragorn") + |> click_button("#redirect-form-to-static-submit", "Save") + |> assert_has("h1", "Main page") + end + end + describe "submit_form/3" do test "submits a form via phx-submit", %{conn: conn} do conn @@ -190,6 +208,20 @@ defmodule PhoenixTest.LiveTest do |> assert_has("#form-data", "email: some@example.com") end + test "follows form's redirect to live page", %{conn: conn} do + conn + |> visit("/live/index") + |> submit_form("#redirect-form", name: "Aragorn") + |> assert_has("h1", "LiveView page 2") + end + + test "follows form's redirect to static page", %{conn: conn} do + conn + |> visit("/live/index") + |> submit_form("#redirect-form-to-static", name: "Aragorn") + |> assert_has("h1", "Main page") + end + test "raises an error if the form can't be found", %{conn: conn} do message = ~r/expected selector "#no-existing-form" to return a single element/ diff --git a/test/support/index_live.ex b/test/support/index_live.ex index 493f452e..5efc3251 100644 --- a/test/support/index_live.ex +++ b/test/support/index_live.ex @@ -59,6 +59,22 @@ defmodule PhoenixTest.IndexLive do + +
+ + + +
+ +
+ + + +
""" end @@ -95,6 +111,14 @@ defmodule PhoenixTest.IndexLive do } end + def handle_event("save-redirect-form", _, socket) do + {:noreply, push_navigate(socket, to: "/live/page_2")} + end + + def handle_event("save-redirect-form-to-static", _, socket) do + {:noreply, redirect(socket, to: "/page/index")} + end + def handle_event("validate-email", %{"email" => email}, socket) do case email do empty when empty == nil or empty == "" ->