-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`.
- Loading branch information
Showing
3 changed files
with
94 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,37 +126,13 @@ 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") | ||
|> fill_form("#email-form", email: nil) | ||
|> 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: "[email protected]") | ||
|> click_button("Save") | ||
|> assert_has("#form-data", "email: [email protected]") | ||
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: "[email protected]") | ||
|> click_button("Save") | ||
|> assert_has("#form-data", "email: [email protected]") | ||
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: [email protected]") | ||
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/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters