Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seperate some code from the mess currently in the form module #97

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/test_dispatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 10 additions & 37 deletions lib/test_dispatch/form.ex
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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}

Expand All @@ -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) ->
Expand All @@ -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)
Expand All @@ -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
48 changes: 48 additions & 0 deletions lib/test_dispatch/helpers/errors.ex
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions lib/test_dispatch/helpers/html.ex
Original file line number Diff line number Diff line change
@@ -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
14 changes: 2 additions & 12 deletions lib/test_dispatch/link.ex
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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