diff --git a/lib/phoenix_test/live.ex b/lib/phoenix_test/live.ex index a1dedb35..6a3f453f 100644 --- a/lib/phoenix_test/live.ex +++ b/lib/phoenix_test/live.ex @@ -81,10 +81,22 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do defp submit_active_form(session) do {form, session} = PhoenixTest.Live.pop_private(session, :active_form) - session.view - |> form(form.selector, form.form_data) - |> render_submit() - |> maybe_redirect(session) + cond do + phx_submit_form?(session, form.selector) -> + session.view + |> form(form.selector, form.form_data) + |> render_submit() + |> maybe_redirect(session) + + action_form?(session, form.selector) -> + session.conn + |> PhoenixTest.Static.build() + |> PhoenixTest.submit_form(form.selector, form.form_data) + + true -> + raise ArgumentError, + "Expected form with selector #{inspect(form.selector)} to have a `phx-submit` or `action` defined." + end end defp regular_click(session, selector, text) do @@ -115,6 +127,26 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Live do end) end + defp action_form?(session, selector) do + action = + session + |> render_html() + |> Query.find!(selector) + |> Html.attribute("action") + + action != nil && action != "" + end + + defp phx_submit_form?(session, selector) do + phx_submit = + session + |> render_html() + |> Query.find!(selector) + |> Html.attribute("phx-submit") + + phx_submit != nil && phx_submit != "" + end + defp phx_change_form?(session, selector) do phx_change = session diff --git a/test/phoenix_test/live_test.exs b/test/phoenix_test/live_test.exs index e87a47d6..c72b0d4a 100644 --- a/test/phoenix_test/live_test.exs +++ b/test/phoenix_test/live_test.exs @@ -2,6 +2,7 @@ defmodule PhoenixTest.LiveTest do use ExUnit.Case, async: true import PhoenixTest + import PhoenixTest.TestHelpers alias PhoenixTest.Driver @@ -198,6 +199,29 @@ defmodule PhoenixTest.LiveTest do |> click_button("#redirect-form-to-static-submit", "Save") |> assert_has("h1", "Main page") end + + test "submits regular (non phx-submit) form", %{conn: conn} do + conn + |> visit("/live/index") + |> fill_form("#non-liveview-form", name: "Aragorn") + |> click_button("Submit Non LiveView") + |> assert_has("h1", "Main page") + end + + test "raises an error if form doesn't have a `phx-submit` or `action`", %{conn: conn} do + msg = + """ + Expected form with selector "#invalid-form" to have a `phx-submit` or `action` defined. + """ + |> ignore_whitespace() + + assert_raise ArgumentError, msg, fn -> + conn + |> visit("/live/index") + |> fill_form("#invalid-form", name: "Aragorn") + |> click_button("Submit Invalid Form") + end + end end describe "submit_form/3" do diff --git a/test/support/index_live.ex b/test/support/index_live.ex index 5efc3251..77a8d58e 100644 --- a/test/support/index_live.ex +++ b/test/support/index_live.ex @@ -75,6 +75,22 @@ defmodule PhoenixTest.IndexLive do Save + +
+ + """ end diff --git a/test/support/page_controller.ex b/test/support/page_controller.ex index fbed7e19..b7593b5b 100644 --- a/test/support/page_controller.ex +++ b/test/support/page_controller.ex @@ -28,4 +28,9 @@ defmodule PhoenixTest.PageController do conn |> redirect(to: "/live/index") end + + def redirect_to_static(conn, _) do + conn + |> redirect(to: "/page/index") + end end diff --git a/test/support/router.ex b/test/support/router.ex index 8d525398..222c5f9e 100644 --- a/test/support/router.ex +++ b/test/support/router.ex @@ -30,6 +30,7 @@ defmodule PhoenixTest.Router do delete "/page/delete_record", PageController, :delete get "/page/:page", PageController, :show post "/page/redirect_to_liveview", PageController, :redirect_to_liveview + post "/page/redirect_to_static", PageController, :redirect_to_static live_session :live_pages do live "/live/index", IndexLive