Skip to content

Commit

Permalink
Add 'checked' radio button default to Static
Browse files Browse the repository at this point in the history
What changed?
=============

In a previous commit, we introduced the `choose` function. That function
didn't handle the case when a user doesn't select any option but the
HTML has a `checked` default.

This commit does that by "prepending" the default value when
`click_button` is called. (by "prepending" we mean adding the default
but letting a value that has been selected override it).
  • Loading branch information
germsvel committed Apr 1, 2024
1 parent bb5803b commit d253eba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do

cond do
has_active_form?(form) and is_submit_button?(form.form_element, selector, text) ->
submit_active_form(session)
session
|> put_default_form_values()
|> submit_active_form()

data_attribute_form?(session, selector, text) ->
form =
Expand All @@ -107,6 +109,28 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
end
end

def put_default_form_values(session) do
active_form = PhoenixTest.Static.get_private(session, :active_form, %{})

html = active_form.form_element |> Html.raw()

active_form = session |> add_default_radio_buttons(html)

session
|> PhoenixTest.Static.put_private(:active_form, active_form)
end

defp add_default_radio_buttons(session, html) do
case Query.find(html, "input[type='radio'][checked='checked']") do
{:found, element} ->
new_form_data = %{Html.attribute(element, "name") => Html.attribute(element, "value")}
prepend_to_active_form_data(session, new_form_data)

:not_found ->
PhoenixTest.Static.get_private(session, :active_form, %{})
end
end

def fill_in(session, label, with: value) do
field =
session
Expand Down Expand Up @@ -187,6 +211,14 @@ defimpl PhoenixTest.Driver, for: PhoenixTest.Static do
|> fill_form("form##{form.id}", active_form.form_data)
end

defp prepend_to_active_form_data(session, default_form_data) do
session
|> PhoenixTest.Static.get_private(:active_form, %{form_data: %{}})
|> Map.update(:form_data, %{}, fn form_data ->
DeepMerge.deep_merge(default_form_data, form_data)
end)
end

defp add_to_active_form_data(session, new_form_data) do
session
|> PhoenixTest.Static.get_private(:active_form, %{form_data: %{}})
Expand Down
9 changes: 9 additions & 0 deletions test/phoenix_test/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ defmodule PhoenixTest.StaticTest do
|> click_button("Save")
|> assert_has("#form-data", text: "contact: email")
end

test "uses the default 'checked' if present", %{conn: conn} do
conn
|> visit("/page/index")
# other field to trigger form save
|> fill_in("First Name", with: "Not important")
|> click_button("Save")
|> assert_has("#form-data", text: "contact: mail")
end
end

describe "fill_form/3" do
Expand Down

0 comments on commit d253eba

Please sign in to comment.