Skip to content

Commit

Permalink
Rename build_data -> build_payload to clarify
Browse files Browse the repository at this point in the history
What changed?
============

We rename some `build_data` -> `build_payload` to clarify distinction
between the "form data" we send via `dispatch` and functions like
`render_submit` and the "form data" that we keep in `ActiveForm` (and
that we generate in `Form.form_data`.
  • Loading branch information
germsvel committed Oct 9, 2024
1 parent 3b175fb commit b37199d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
18 changes: 8 additions & 10 deletions lib/phoenix_test/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ defmodule PhoenixTest.Form do
|> build()
end

def build_data(%__MODULE__{} = form), do: build_data(form.form_data)

def build_data(data) when is_list(data) do
def build_payload(data) when is_list(data) do
data
|> Enum.map_join("&", fn {key, value} ->
"#{URI.encode_www_form(key)}=#{if(value, do: URI.encode_www_form(value))}"
end)
|> Plug.Conn.Query.decode()
end

def add_upload_payloads(data, uploads) when is_map(data) and is_list(uploads) do
Enum.reduce(uploads, data, fn {name, upload}, acc ->
with_placeholder = Plug.Conn.Query.decode("#{URI.encode_www_form(name)}=placeholder")
put_at_placeholder(acc, with_placeholder, upload)
end)
end

defp build(form) do
raw = Html.raw(form)
id = Html.attribute(form, "id")
Expand All @@ -49,13 +54,6 @@ defmodule PhoenixTest.Form do
}
end

def inject_uploads(data, uploads) when is_map(data) and is_list(uploads) do
Enum.reduce(uploads, data, fn {name, upload}, acc ->
with_placeholder = Plug.Conn.Query.decode("#{URI.encode_www_form(name)}=placeholder")
put_at_placeholder(acc, with_placeholder, upload)
end)
end

defp put_at_placeholder(_, "placeholder", upload), do: upload
defp put_at_placeholder(list, ["placeholder"], upload), do: (list || []) ++ [upload]

Expand Down
6 changes: 3 additions & 3 deletions lib/phoenix_test/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ defmodule PhoenixTest.Live do
additional_data = %{"_target" => field.name}

session.view
|> form(form.selector, Form.build_data(data_to_submit))
|> form(form.selector, Form.build_payload(data_to_submit))
|> render_change(additional_data)
|> maybe_redirect(session)
else
Expand Down Expand Up @@ -286,8 +286,8 @@ defmodule PhoenixTest.Live do
cond do
Form.phx_submit?(form) ->
session.view
|> form(selector, Form.build_data(form_data))
|> render_submit(Form.build_data(additional_data))
|> form(selector, Form.build_payload(form_data))
|> render_submit(Form.build_payload(additional_data))
|> maybe_redirect(session)

Form.has_action?(form) ->
Expand Down
16 changes: 8 additions & 8 deletions lib/phoenix_test/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule PhoenixTest.Static do
if active_form.selector == form.selector do
submit_active_form(session, form)
else
perform_submit(session, form, build_data(form))
perform_submit(session, form, build_payload(form))
end
end
end
Expand Down Expand Up @@ -188,7 +188,7 @@ defmodule PhoenixTest.Static do
Form.put_button_data(form, form.submit_button)
end)

to_submit = Form.build_data(form.form_data ++ form_data)
to_submit = Form.build_payload(form.form_data ++ form_data)

session
|> Map.put(:active_form, ActiveForm.new())
Expand Down Expand Up @@ -235,15 +235,15 @@ defmodule PhoenixTest.Static do

session
|> Map.put(:active_form, ActiveForm.new())
|> perform_submit(form, build_data(form, active_form))
|> perform_submit(form, build_payload(form, active_form))
end

defp perform_submit(session, form, form_data) do
defp perform_submit(session, form, payload) do
conn = session.conn

conn
|> recycle(all_headers(conn))
|> dispatch(@endpoint, form.method, form.action, form_data)
|> dispatch(@endpoint, form.method, form.action, payload)
|> maybe_redirect(session)
end

Expand All @@ -257,10 +257,10 @@ defmodule PhoenixTest.Static do
}
end

defp build_data(form, active_form \\ ActiveForm.new()) do
defp build_payload(form, active_form \\ ActiveForm.new()) do
(form.form_data ++ active_form.form_data)
|> Form.build_data()
|> Form.inject_uploads(active_form.uploads)
|> Form.build_payload()
|> Form.add_upload_payloads(active_form.uploads)
end

defp maybe_redirect(conn, session) do
Expand Down
30 changes: 15 additions & 15 deletions test/phoenix_test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ defmodule PhoenixTest.FormTest do
"checkbox" => "checked",
"radio" => "checked",
"textarea" => "Default text"
} = Form.build_data(form.form_data)
} = Form.build_payload(form.form_data)
end

test "does not include disabled inputs in form_data" do
Expand All @@ -160,7 +160,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{} == Form.build_data(form.form_data)
assert %{} == Form.build_payload(form.form_data)
end

test "does not include inputs with a `name` attribute" do
Expand All @@ -174,7 +174,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{} == Form.build_data(form.form_data)
assert %{} == Form.build_payload(form.form_data)
end

test "ignores hidden value for checkbox when checked" do
Expand All @@ -187,7 +187,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"checkbox" => "checked"} = Form.build_data(form.form_data)
assert %{"checkbox" => "checked"} = Form.build_payload(form.form_data)
end

test "uses hidden value for checkbox when unchecked" do
Expand All @@ -200,7 +200,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"checkbox" => "unchecked"} = Form.build_data(form.form_data)
assert %{"checkbox" => "unchecked"} = Form.build_payload(form.form_data)
end
end

Expand All @@ -215,7 +215,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"checkbox" => ["some_value", "another_value"]} = Form.build_data(form.form_data)
assert %{"checkbox" => ["some_value", "another_value"]} = Form.build_payload(form.form_data)
end

test "checkboxes, single" do
Expand All @@ -228,7 +228,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"checkbox" => ["some_value"]} = Form.build_data(form.form_data)
assert %{"checkbox" => ["some_value"]} = Form.build_payload(form.form_data)
end

test "hidden, multiple" do
Expand All @@ -241,7 +241,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"hidden" => ["some_value", "another_value"]} = Form.build_data(form.form_data)
assert %{"hidden" => ["some_value", "another_value"]} = Form.build_payload(form.form_data)
end

test "hidden, single" do
Expand All @@ -253,7 +253,7 @@ defmodule PhoenixTest.FormTest do

form = Form.find!(html, "form")

assert %{"hidden" => ["some_value"]} = Form.build_data(form.form_data)
assert %{"hidden" => ["some_value"]} = Form.build_payload(form.form_data)
end
end

Expand Down Expand Up @@ -356,12 +356,12 @@ defmodule PhoenixTest.FormTest do
end
end

describe "inject_uploads" do
describe "add_upload_payloads" do
test "injects top-level upload" do
form_data = %{"name" => "Frodo"}
uploads = [{"avatar", upload()}]

injected = Form.inject_uploads(form_data, uploads)
injected = Form.add_upload_payloads(form_data, uploads)

assert injected == %{"name" => "Frodo", "avatar" => upload()}
end
Expand All @@ -370,7 +370,7 @@ defmodule PhoenixTest.FormTest do
form_data = %{"avatar" => "how did this string get here?"}
uploads = [{"avatar", upload()}]

injected = Form.inject_uploads(form_data, uploads)
injected = Form.add_upload_payloads(form_data, uploads)

assert injected == %{"avatar" => upload()}
end
Expand All @@ -379,7 +379,7 @@ defmodule PhoenixTest.FormTest do
form_data = %{"user" => %{"name" => "Frodo"}}
uploads = [{"user[avatar]", upload()}]

injected = Form.inject_uploads(form_data, uploads)
injected = Form.add_upload_payloads(form_data, uploads)

assert injected == %{"user" => %{"name" => "Frodo", "avatar" => upload()}}
end
Expand All @@ -388,7 +388,7 @@ defmodule PhoenixTest.FormTest do
form_data = %{}
uploads = [{"avatar[]", upload(0)}, {"avatar[]", upload(1)}]

injected = Form.inject_uploads(form_data, uploads)
injected = Form.add_upload_payloads(form_data, uploads)

assert injected == %{"avatar" => [upload(0), upload(1)]}
end
Expand All @@ -397,7 +397,7 @@ defmodule PhoenixTest.FormTest do
form_data = %{}
uploads = [{"avatar[0][file]", upload(0)}, {"avatar[1][file]", upload(1)}]

injected = Form.inject_uploads(form_data, uploads)
injected = Form.add_upload_payloads(form_data, uploads)

assert injected == %{"avatar" => %{"0" => %{"file" => upload(0)}, "1" => %{"file" => upload(1)}}}
end
Expand Down

0 comments on commit b37199d

Please sign in to comment.