Skip to content

Commit

Permalink
Refactor: Move to_form_data! to Field
Browse files Browse the repository at this point in the history
What changed?
=============

We move `to_form_data!` from `FormData` to `Field` since it's field data
that's using it. We want to create a better `FormData` structure to
handle form data transformations, but for now, we make this refactor to
clean up where we're using FormData.
  • Loading branch information
germsvel committed Oct 8, 2024
1 parent 73f2b83 commit 3b175fb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 60 deletions.
19 changes: 19 additions & 0 deletions lib/phoenix_test/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,23 @@ defmodule PhoenixTest.Field do
_ -> false
end
end

def to_form_data!(%{value: values} = field) when is_list(values) do
Enum.map(values, &{field.name, &1})
end

def to_form_data!(field) do
validate!(field)
[{field.name, field.value}]
end

defp validate!(field) do
if field.name == nil do
raise ArgumentError, """
Field is missing a `name` attribute:
#{Html.raw(field.parsed)}
"""
end
end
end
24 changes: 0 additions & 24 deletions lib/phoenix_test/form_data.ex

This file was deleted.

3 changes: 1 addition & 2 deletions lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule PhoenixTest.Live do
alias PhoenixTest.Field
alias PhoenixTest.FileUpload
alias PhoenixTest.Form
alias PhoenixTest.FormData
alias PhoenixTest.Html
alias PhoenixTest.Query
alias PhoenixTest.Select
Expand Down Expand Up @@ -227,7 +226,7 @@ defmodule PhoenixTest.Live do
end

defp fill_in_field_data(session, field) do
new_form_data = FormData.to_form_data!(field)
new_form_data = Field.to_form_data!(field)
form = Field.parent_form!(field)

session =
Expand Down
3 changes: 1 addition & 2 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule PhoenixTest.Static do
alias PhoenixTest.Field
alias PhoenixTest.FileUpload
alias PhoenixTest.Form
alias PhoenixTest.FormData
alias PhoenixTest.Html
alias PhoenixTest.Link
alias PhoenixTest.OpenBrowser
Expand Down Expand Up @@ -219,7 +218,7 @@ defmodule PhoenixTest.Static do
end

defp fill_in_field_data(session, field) do
new_form_data = FormData.to_form_data!(field)
new_form_data = Field.to_form_data!(field)
form = Field.parent_form!(field)

Map.update!(session, :active_form, fn active_form ->
Expand Down
26 changes: 26 additions & 0 deletions test/phoenix_test/field_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,30 @@ defmodule PhoenixTest.FieldTest do
refute Field.belongs_to_form?(field)
end
end

describe "to_form_data!" do
test "transforms a field into a name/value pair" do
html = """
<label for="name">Name</label>
<input id="name" type="text" name="name" value="Hello world"/>
"""

field = Field.find_input!(html, "input", "Name", exact: true)

assert [{"name", "Hello world"}] = Field.to_form_data!(field)
end

test "raises error if name attribute is missing" do
html = """
<label for="name">Name</label>
<input id="name" type="text" value="Hello world"/>
"""

field = Field.find_input!(html, "input", "Name", exact: true)

assert_raise ArgumentError, ~r/missing a `name`/, fn ->
Field.to_form_data!(field)
end
end
end
end
32 changes: 0 additions & 32 deletions test/phoenix_test/form_data_test.exs

This file was deleted.

0 comments on commit 3b175fb

Please sign in to comment.