From bd41c92c7903c1f6d7abefc627a6f9757f95a819 Mon Sep 17 00:00:00 2001 From: FatboyPunk Date: Fri, 22 Oct 2021 11:52:38 +0200 Subject: [PATCH] Seperate some code from the mess currently in the form module --- lib/test_dispatch.ex | 1 + lib/test_dispatch/form.ex | 47 ++++++---------------------- lib/test_dispatch/helpers/errors.ex | 48 +++++++++++++++++++++++++++++ lib/test_dispatch/helpers/html.ex | 16 ++++++++++ lib/test_dispatch/link.ex | 14 ++------- 5 files changed, 77 insertions(+), 49 deletions(-) create mode 100644 lib/test_dispatch/helpers/errors.ex create mode 100644 lib/test_dispatch/helpers/html.ex diff --git a/lib/test_dispatch.ex b/lib/test_dispatch.ex index 9cd5918..4aa932d 100644 --- a/lib/test_dispatch.ex +++ b/lib/test_dispatch.ex @@ -9,6 +9,7 @@ defmodule TestDispatch do import Phoenix.Controller, only: [endpoint_module: 1] import TestDispatch.Form import TestDispatch.Link + import TestDispatch.Helpers.HTML, only: [floki_attribute: 2] @doc """ See `submit_form/3` for documentation. diff --git a/lib/test_dispatch/form.ex b/lib/test_dispatch/form.ex index 8cd1620..fd8c524 100644 --- a/lib/test_dispatch/form.ex +++ b/lib/test_dispatch/form.ex @@ -1,10 +1,13 @@ defmodule TestDispatch.Form do @moduledoc false - @form_methods ["post", "put", "delete", "get"] import Phoenix.ConnTest, only: [dispatch: 5, html_response: 2] import Phoenix.Controller, only: [endpoint_module: 1] import TestSelector.Test.FlokiHelpers + import TestDispatch.Helpers.Error + import TestDispatch.Helpers.HTML + + @form_methods ["post", "put", "delete", "get"] def find_inputs(form, {:entity, _} = entity_tuple), do: find_input_fields(form, entity_tuple) @@ -203,11 +206,7 @@ defmodule TestDispatch.Form do end def find_form(%Plug.Conn{status: status}, _), - do: - raise( - Plug.BadRequestError, - "The provided conn had the status #{status} that doesn't fall into the 2xx range" - ) + do: raise_status(status) def find_form_by(forms, nil), do: {List.last(forms), nil} @@ -216,20 +215,13 @@ defmodule TestDispatch.Form do if form, do: {form, :button_text}, - else: - raise(""" - No form found for the given button text: #{button_text} - Found the button texts: - - #{all_buttons(forms)} - """) + else: raise_no_button_found(forms, button_text) end - def find_form_by(forms, entity_or_test_selector) do - test_selector_result = Enum.find(forms, &find_test_selector(&1, entity_or_test_selector)) + def find_form_by(forms, selector) do + test_selector_result = Enum.find(forms, &find_test_selector(&1, selector)) - entity_result = - Enum.find(forms, &(&1 |> Floki.find("*[id^=#{entity_or_test_selector}_]") |> Enum.any?())) + entity_result = Enum.find(forms, &(Floki.find(&1, "*[id^=#{selector}_]") |> Enum.any?())) cond do is_tuple(test_selector_result) -> @@ -239,18 +231,10 @@ defmodule TestDispatch.Form do {entity_result, :entity} true -> - raise("No form found for the given test_selector or entity: #{entity_or_test_selector}") + raise_no_selector_found(selector) end end - @spec floki_attribute(binary | Floki.html_tree(), binary, binary() | nil | none()) :: - binary() | nil - def floki_attribute(html, select, name \\ nil) - def floki_attribute(html, select, nil), do: html |> Floki.attribute(select) |> List.first() - - def floki_attribute(html, select, name), - do: html |> Floki.attribute(select, name) |> List.first() - def parse_conn(%{status: status} = conn) do conn |> html_response(status) @@ -270,15 +254,4 @@ defmodule TestDispatch.Form do input_submit || button_submit end - - def text(html_tree), - do: html_tree |> Floki.text() |> String.replace(~r/\s+/, " ") |> String.trim() - - defp all_buttons(html_tree) do - all_submit_buttons = html_tree |> Floki.find("button[type=submit]") - all_submit_inputs = html_tree |> Floki.find("input[type=submit]") - - (all_submit_inputs ++ all_submit_buttons) - |> Enum.map(&(text([&1]) <> "\n ")) - end end diff --git a/lib/test_dispatch/helpers/errors.ex b/lib/test_dispatch/helpers/errors.ex new file mode 100644 index 0000000..27f2688 --- /dev/null +++ b/lib/test_dispatch/helpers/errors.ex @@ -0,0 +1,48 @@ +defmodule TestDispatch.Helpers.Error do + @moduledoc false + import TestDispatch.Helpers.HTML + + @doc false + def raise_status(status) do + raise( + Plug.BadRequestError, + "The provided conn had the status #{status} that doesn't fall into the 2xx range" + ) + end + + @doc false + def raise_no_button_found(forms, button_text) do + raise(""" + No form found for the given button text: #{button_text} + Found the button texts: + + #{all_buttons(forms)} + """) + end + + @doc false + def not_found_raise(test_selector, nil) do + raise("No `a` element found for just the selector #{inspect(test_selector)}") + end + + def not_found_raise(test_selector, test_value) do + raise( + "No `a` element found for selector #{inspect(test_selector)} with value #{ + inspect(test_value) + }" + ) + end + + @doc false + def raise_no_selector_found(selector) do + raise("No form found for the given test_selector or entity: #{selector}") + end + + defp all_buttons(html_tree) do + all_submit_buttons = html_tree |> Floki.find("button[type=submit]") + all_submit_inputs = html_tree |> Floki.find("input[type=submit]") + + (all_submit_inputs ++ all_submit_buttons) + |> Enum.map(&(text([&1]) <> "\n ")) + end +end diff --git a/lib/test_dispatch/helpers/html.ex b/lib/test_dispatch/helpers/html.ex new file mode 100644 index 0000000..402fe0a --- /dev/null +++ b/lib/test_dispatch/helpers/html.ex @@ -0,0 +1,16 @@ +defmodule TestDispatch.Helpers.HTML do + @moduledoc false + + @doc false + def text(html_tree), + do: html_tree |> Floki.text() |> String.replace(~r/\s+/, " ") |> String.trim() + + @spec floki_attribute(binary | Floki.html_tree(), binary, binary() | nil | none()) :: + binary() | nil + @doc false + def floki_attribute(html, select, name \\ nil) + def floki_attribute(html, select, nil), do: html |> Floki.attribute(select) |> List.first() + + def floki_attribute(html, select, name), + do: html |> Floki.attribute(select, name) |> List.first() +end diff --git a/lib/test_dispatch/link.ex b/lib/test_dispatch/link.ex index 7d4a5c5..65bad71 100644 --- a/lib/test_dispatch/link.ex +++ b/lib/test_dispatch/link.ex @@ -1,7 +1,9 @@ defmodule TestDispatch.Link do @moduledoc false + import TestDispatch.Form, only: [parse_conn: 1] import TestSelector.Test.FlokiHelpers + import TestDispatch.Helpers.Error, only: [not_found_raise: 2] def find_link(conn_or_floki_tree, test_selector, test_value \\ nil) do value = conn_or_floki_tree |> _find_link(test_selector, test_value) |> List.first() @@ -21,16 +23,4 @@ defmodule TestDispatch.Link do defp _find_link(tree, test_selector, test_value), do: tree |> _find_link(test_selector) |> find_test_values(test_value) - - defp not_found_raise(test_selector, nil) do - raise("No `a` element found for just the selector #{inspect(test_selector)}") - end - - defp not_found_raise(test_selector, test_value) do - raise( - "No `a` element found for selector #{inspect(test_selector)} with value #{ - inspect(test_value) - }" - ) - end end