Skip to content

Commit

Permalink
Handle regular form submission from a LiveView page (#24)
Browse files Browse the repository at this point in the history
Closes #16

What changed?
=============

Currently, we assume that if you're in a LiveView page, you're trying to
submit LiveView forms with `phx-submit`. But people can have regular
forms in LiveView pages.

This commit handles that scenario.

In order to handle that we introduce an error message when there are
neither a `phx-submit` nor an `action`. In theory, form's don't need an
`action`. The form's `action` defaults to the same page (i.e. `"/"`).

But we don't want to just fallback to the normal form if there's no
`action`. Instead we want to notify the user of an error -- we should
expect a `phx-submit` or an `action`.
  • Loading branch information
germsvel authored Feb 12, 2024
1 parent 45e0063 commit fe755de
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/phoenix_test/live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule PhoenixTest.LiveTest do
use ExUnit.Case, async: true

import PhoenixTest
import PhoenixTest.TestHelpers

alias PhoenixTest.Driver

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/support/index_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ defmodule PhoenixTest.IndexLive do
Save
</button>
</form>
<form id="invalid-form">
<label for="name">Name</label>
<input name="name" />
<button type="submit">
Submit Invalid Form
</button>
</form>
<form id="non-liveview-form" action="/page/redirect_to_static" method="post">
<label for="name">Name</label>
<input name="name" />
<button type="submit">
Submit Non LiveView
</button>
</form>
"""
end

Expand Down
5 changes: 5 additions & 0 deletions test/support/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/support/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe755de

Please sign in to comment.