Skip to content

Commit

Permalink
Submit phx-value-* attributes on <form> in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teamon committed May 24, 2024
1 parent e742ca8 commit 92281f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/phoenix_live_view/test/client_proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,11 @@ defmodule Phoenix.LiveViewTest.ClientProxy do

with {:ok, defaults} <- maybe_submitter(defaults, type, node, element),
{:ok, value} <- fill_in_map(Enum.to_list(element.form_data || %{}), "", node, []) do
{:ok, DOM.deep_merge(Query.decode_done(defaults), value)}
{:ok,
defaults
|> Query.decode_done()
|> DOM.deep_merge(DOM.all_values(node))
|> DOM.deep_merge(value)}
else
{:error, _, _} = error -> error
end
Expand Down
5 changes: 5 additions & 0 deletions test/phoenix_live_view/integrations/elements_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ defmodule Phoenix.LiveView.ElementsTest do
assert last_event(view) =~ ~s|form-submit: %{"foo" => "bar"}|
end

test "submits data passed as phx-value-* attributes", %{live: view} do
assert view |> element("#phx-value-form") |> render_submit()
assert last_event(view) =~ ~s|form-submit: %{"foo" => "bar", "key" => "val"}|
end

test "raises on invalid submitter", %{live: view} do
assert_raise ArgumentError, ~r"invalid form submitter", fn ->
assert view
Expand Down
4 changes: 3 additions & 1 deletion test/support/live_views/elements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ defmodule Phoenix.LiveViewTest.ElementsLive do
<%# forms %>
<a id="a-no-form" phx-change="hello" phx-submit="world">Change</a>
<form id="empty-form" phx-change="form-change" phx-submit="form-submit" phx-value-key="value">
<form id="empty-form" phx-change="form-change" phx-submit="form-submit">
</form>
<form id="phx-value-form" phx-change="form-change" phx-submit="form-submit" phx-value-key="val" phx-value-foo="bar">
</form>
<form id="form" phx-change="form-change" phx-submit="form-submit" phx-value-key="value">
<input value="no-name">
Expand Down

0 comments on commit 92281f1

Please sign in to comment.