Skip to content

Commit

Permalink
Handle form redirects in Static pages (#22)
Browse files Browse the repository at this point in the history
What changed?
============

Commit 531e5e9 updated Live form submissions to follow redirects to
other LiveView and Static pages.

This commit does the same but for Static pages.

Thus, now when a `fill_form` + `click_button`, a single-field form
`click_button`, or a `submit_button` redirects, we follow the redirect
to the other page -- whether that be a LiveView or a static page.
  • Loading branch information
germsvel authored Feb 10, 2024
1 parent 4b92eee commit 4c39920
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 33 deletions.
40 changes: 29 additions & 11 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do

data = form["data"]

conn = dispatch(session.conn, @endpoint, method, action, data)

%{session | conn: conn}
session.conn
|> dispatch(@endpoint, method, action, data)
|> maybe_redirect(session)
end

defp single_button_form_submit(session, text) do
Expand All @@ -98,9 +98,9 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
action = Html.attribute(form, "action")
method = Html.attribute(form, "method") || "get"

conn = dispatch(session.conn, @endpoint, method, action)

%{session | conn: conn}
session.conn
|> dispatch(@endpoint, method, action)
|> maybe_redirect(session)
end

def submit_form(session, selector, form_data) do
Expand All @@ -123,6 +123,22 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
|> PhoenixTest.Static.put_private(:active_form, form)
end

def render_html(%{conn: conn}) do
conn
|> html_response(200)
end

defp maybe_redirect(conn, session) do
case conn do
%{status: 302} ->
path = redirected_to(conn)
PhoenixTest.visit(conn, path)

%{status: _} ->
%{session | conn: conn}
end
end

defp verify_expected_form_data(form, form_data) do
action = form["action"]
unless action, do: raise("Expected form to have an action but found none")
Expand All @@ -144,6 +160,13 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
end)
end

defp verify_field_presence([], expected_field) do
raise ArgumentError, """
Expected form to have #{inspect(expected_field)} form field, but found no
existing fields.
"""
end

defp verify_field_presence(existing_fields, expected_field) do
if Enum.all?(existing_fields, fn field ->
field["name"] != expected_field
Expand All @@ -161,9 +184,4 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
defp format_field_error(field) do
"#{field["type"]} with name=#{inspect(field["name"])}"
end

def render_html(%{conn: conn}) do
conn
|> html_response(200)
end
end
68 changes: 46 additions & 22 deletions test/phoenix_test/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ defmodule PhoenixTest.StaticTest do
|> assert_has("h1", "Record deleted")
end

test "can handle redirects to a LiveView", %{conn: conn} do
conn
|> visit("/page/index")
|> click_button("Post and Redirect")
|> assert_has("h1", "LiveView main page")
end

test "raises an error when there are no buttons on page", %{conn: conn} do
assert_raise ArgumentError, ~r/Could not find an element with given selector/, fn ->
conn
Expand All @@ -116,6 +123,32 @@ defmodule PhoenixTest.StaticTest do
end

describe "fill_form/3" do
test "raises an error when form cannot be found with given selector", %{conn: conn} do
assert_raise ArgumentError, ~r/Could not find element with selector/, fn ->
conn
|> visit("/page/index")
|> fill_form("#no-existing-form", name: "Aragorn")
end
end

test "raises an error when form input cannot be found", %{conn: conn} do
message = """
Expected form to have "location[user][name]" form field, but found none.
Found the following fields:
- input with name="user[name]"
"""

assert_raise ArgumentError, message, fn ->
conn
|> visit("/page/index")
|> fill_form("#nested-form", location: %{user: %{name: "Aragorn"}})
end
end
end

describe "fill_form + click_button" do
test "can submit forms with input type submit", %{conn: conn} do
conn
|> visit("/page/index")
Expand Down Expand Up @@ -150,28 +183,12 @@ defmodule PhoenixTest.StaticTest do
|> assert_has("#form-data", "member_of_fellowship: on")
end

test "raises an error when form cannot be found with given selector", %{conn: conn} do
assert_raise ArgumentError, ~r/Could not find element with selector/, fn ->
conn
|> visit("/page/index")
|> fill_form("#no-existing-form", name: "Aragorn")
end
end

test "raises an error when form input cannot be found", %{conn: conn} do
message = """
Expected form to have "location[user][name]" form field, but found none.
Found the following fields:
- input with name="user[name]"
"""

assert_raise ArgumentError, message, fn ->
conn
|> visit("/page/index")
|> fill_form("#nested-form", location: %{user: %{name: "Aragorn"}})
end
test "can handle redirects into a LiveView", %{conn: conn} do
conn
|> visit("/page/index")
|> fill_form("#redirect-to-liveview-form", name: "Aragorn")
|> click_button("Save and Redirect to LiveView")
|> assert_has("h1", "LiveView main page")
end
end

Expand All @@ -183,6 +200,13 @@ defmodule PhoenixTest.StaticTest do
|> assert_has("#form-data", "name: Aragorn")
end

test "can handle redirects", %{conn: conn} do
conn
|> visit("/page/index")
|> submit_form("#no-submit-button-and-redirect", name: "Aragorn")
|> assert_has("h1", "LiveView main page")
end

test "raises an error if the form can't be found", %{conn: conn} do
assert_raise ArgumentError, ~r/Could not find element with selector/, fn ->
conn
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 @@ -23,4 +23,9 @@ defmodule PhoenixTest.PageController do
conn
|> render("record_deleted.html")
end

def redirect_to_liveview(conn, _) do
conn
|> redirect(to: "/live/index")
end
end
15 changes: 15 additions & 0 deletions test/support/page_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ defmodule PhoenixTest.PageView do
<input type="checkbox" name="member_of_fellowship" />
</div>
</form>
<form id="redirect-to-liveview-form" method="post" action="/page/redirect_to_liveview">
<label for="name">Name</label>
<input name="name" />
<button type="submit">Save and Redirect to LiveView</button>
</form>
<form method="post" action="/page/redirect_to_liveview">
<button>Post and Redirect</button>
</form>
<form id="no-submit-button-and-redirect" method="post" action="/page/redirect_to_liveview">
<label for="name">Name</label>
<input name="name" />
</form>
"""
end

Expand Down
1 change: 1 addition & 0 deletions test/support/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule PhoenixTest.Router do
put "/page/update_record", PageController, :update
delete "/page/delete_record", PageController, :delete
get "/page/:page", PageController, :show
post "/page/redirect_to_liveview", PageController, :redirect_to_liveview

live_session :live_pages do
live "/live/index", IndexLive
Expand Down

0 comments on commit 4c39920

Please sign in to comment.