From 46d6229c4ecaecc314891c06956b3441df1acd98 Mon Sep 17 00:00:00 2001 From: Nils Meckmann <6068179+Nilsonn@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:31:06 +0100 Subject: [PATCH] Use Floki to find nested fields (#14) In the current version, `fill_form` can't be used if the fields are no direct children of the form element. This commit changes the `build_fields` function to use Floki in order to find all input, select and textarea elements that are somewhere in the tree of the form element. --- lib/phoenix_test/html/form.ex | 12 +++++------- test/phoenix_test/html/form_test.exs | 18 ++++++++++++++++++ test/phoenix_test/static_test.exs | 4 +++- test/support/page_view.ex | 5 +++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/phoenix_test/html/form.ex b/lib/phoenix_test/html/form.ex index a4cf4157..0f1adc9a 100644 --- a/lib/phoenix_test/html/form.ex +++ b/lib/phoenix_test/html/form.ex @@ -15,13 +15,11 @@ defmodule PhoenixTest.Html.Form do end defp build_fields(fields) do - fields - |> Enum.filter(fn - {"input", _, _} -> true - {"select", _, _} -> true - {"textarea", _, _} -> true - _ -> false - end) + inputs = fields |> Floki.find("input") + selects = fields |> Floki.find("select") + textareas = fields |> Floki.find("textarea") + + Enum.concat([inputs, selects, textareas]) |> Enum.map(&create_field/1) end diff --git a/test/phoenix_test/html/form_test.exs b/test/phoenix_test/html/form_test.exs index 3b60ea9b..65d0c8a7 100644 --- a/test/phoenix_test/html/form_test.exs +++ b/test/phoenix_test/html/form_test.exs @@ -58,6 +58,24 @@ defmodule PhoenixTest.Html.FormTest do assert %{"type" => "checkbox"} = admin end + + test "parses nested checkbox" do + data = + form_data(""" +
+ """) + + %{"fields" => fields} = Html.Form.parse(data) + + admin = Enum.find(fields, &(&1["name"] == "admin")) + + assert %{"type" => "checkbox"} = admin + end end defp form_data(html_form) do diff --git a/test/phoenix_test/static_test.exs b/test/phoenix_test/static_test.exs index 70879657..ec71b652 100644 --- a/test/phoenix_test/static_test.exs +++ b/test/phoenix_test/static_test.exs @@ -139,13 +139,15 @@ defmodule PhoenixTest.StaticTest do name: "Aragorn", admin: "on", race: "human", - notes: "King of Gondor" + notes: "King of Gondor", + member_of_fellowship: "on" ) |> 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") + |> assert_has("#form-data", "member_of_fellowship: on") end test "raises an error when form cannot be found with given selector", %{conn: conn} do diff --git a/test/support/page_view.ex b/test/support/page_view.ex index 4e6a75a8..a26a0a20 100644 --- a/test/support/page_view.ex +++ b/test/support/page_view.ex @@ -68,6 +68,11 @@ defmodule PhoenixTest.PageView do + +