Skip to content

Commit 0bbadd3

Browse files
authored
Simplify after introducing separate selected option. (#310)
Follow up for #309. With the new approach (separate selected option), some changes to existing value finder path became unnecessary. This just cleans up some unnecessary functions.
1 parent e18894b commit 0bbadd3

2 files changed

Lines changed: 28 additions & 61 deletions

File tree

lib/phoenix_test/assertions.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,12 @@ defmodule PhoenixTest.Assertions do
464464
&Query.find_by_label(&1, selector, label, Opts.to_list(opts))
465465

466466
{[value: value], %Opts{label: :no_label}, _} ->
467-
&Query.find_by_value(&1, selector, ensure_binary(value), Opts.to_list(opts))
467+
selector = selector <> "[value=#{value |> ensure_binary() |> inspect()}]"
468+
&Query.find(&1, selector, Opts.to_list(opts))
468469

469470
{[value: value], %Opts{label: label}, _} when is_binary(label) ->
470-
&Query.find_by_label_and_value(&1, selector, label, ensure_binary(value), Opts.to_list(opts))
471+
selector = selector <> "[value=#{value |> ensure_binary() |> inspect()}]"
472+
&Query.find_by_label(&1, selector, label, Opts.to_list(opts))
471473

472474
{[selected: selected], %Opts{label: :no_label}, _} ->
473475
&Query.find_by_selected(&1, selector, ensure_binary(selected), Opts.to_list(opts))

lib/phoenix_test/query.ex

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ defmodule PhoenixTest.Query do
5757

5858
def find(html, selector, opts) when is_list(opts) do
5959
html
60-
|> all_by_selector(selector)
60+
|> Html.parse_fragment()
61+
|> Html.all(selector)
6162
|> filter_by_position(opts)
6263
|> case do
6364
[] ->
@@ -73,51 +74,40 @@ defmodule PhoenixTest.Query do
7374
end
7475

7576
def find(html, selector, text, opts \\ []) when is_binary(text) and is_list(opts) do
76-
elements_matched_selector = all_by_selector(html, selector)
77+
elements_matched_selector =
78+
html
79+
|> Html.parse_fragment()
80+
|> Html.all(selector)
7781

7882
elements_matched_selector
7983
|> filter_by_position(opts)
8084
|> filter_by_element_text(text, opts)
81-
|> find_result(elements_matched_selector)
82-
end
83-
84-
def find_by_value(html, selector, value, opts \\ []) when is_binary(value) and is_list(opts) do
85-
elements_matched_selector = all_by_selector(html, selector)
86-
87-
elements_matched_selector
88-
|> filter_by_position(opts)
89-
|> find_by_element_value_result(value, elements_matched_selector)
90-
end
91-
92-
def find_by_label_and_value(html, input_selectors, label, value, opts \\ []) when is_binary(value) and is_list(opts) do
93-
case find_by_label(html, input_selectors, label, opts) do
94-
{:found, element} ->
95-
find_by_element_value_result([element], value, [element])
96-
97-
{:not_found, :found_many_labels_with_inputs, _labels, elements} ->
98-
find_by_element_value_result(elements, value, elements)
99-
100-
other ->
101-
other
85+
|> case do
86+
[] -> {:not_found, elements_matched_selector}
87+
[found] -> {:found, found}
88+
[_ | _] = found_many -> {:found_many, found_many}
10289
end
10390
end
10491

10592
def find_by_selected(html, selector, selected, opts \\ []) when is_binary(selected) and is_list(opts) do
106-
elements_matched_selector = all_by_selector(html, selector)
93+
elements_matched_selector =
94+
html
95+
|> Html.parse_fragment()
96+
|> Html.all(selector)
10797

10898
elements_matched_selector
10999
|> filter_by_position(opts)
110-
|> find_by_selected_option_text_result(selected, elements_matched_selector)
100+
|> selected_result(selected, elements_matched_selector)
111101
end
112102

113103
def find_by_label_and_selected(html, input_selectors, label, selected, opts \\ [])
114104
when is_binary(selected) and is_list(opts) do
115105
case find_by_label(html, input_selectors, label, opts) do
116106
{:found, element} ->
117-
find_by_selected_option_text_result([element], selected, [element])
107+
selected_result([element], selected, [element])
118108

119109
{:not_found, :found_many_labels_with_inputs, _labels, elements} ->
120-
find_by_selected_option_text_result(elements, selected, elements)
110+
selected_result(elements, selected, elements)
121111

122112
other ->
123113
other
@@ -128,7 +118,10 @@ defmodule PhoenixTest.Query do
128118
#
129119
# This is a performance improvement when you only need to confirm that at least one match exists.
130120
def find_first(html, selector, text, opts \\ []) when is_binary(text) and is_list(opts) do
131-
elements_matched_selector = all_by_selector(html, selector)
121+
elements_matched_selector =
122+
html
123+
|> Html.parse_fragment()
124+
|> Html.all(selector)
132125

133126
case find_first_by_element_text(elements_matched_selector, text, opts) do
134127
nil -> {:not_found, elements_matched_selector}
@@ -591,44 +584,16 @@ defmodule PhoenixTest.Query do
591584
end
592585
end
593586

594-
defp all_by_selector(html, selector) do
595-
html
596-
|> Html.parse_fragment()
597-
|> Html.all(selector)
598-
end
599-
600-
defp filter_by_element_value(elements, value) do
601-
Enum.filter(elements, &(value in element_values(&1)))
602-
end
603-
604-
defp filter_by_selected_option_text(elements, selected) do
605-
Enum.filter(elements, &(selected in selected_option_texts(&1)))
606-
end
607-
608-
defp find_by_element_value_result(elements, value, potential_matches) do
587+
defp selected_result(elements, selected, potential_matches) do
609588
elements
610-
|> filter_by_element_value(value)
611-
|> find_result(potential_matches)
612-
end
613-
614-
defp find_by_selected_option_text_result(elements, selected, potential_matches) do
615-
elements
616-
|> filter_by_selected_option_text(selected)
617-
|> find_result(potential_matches)
618-
end
619-
620-
defp find_result(elements, potential_matches) do
621-
case elements do
589+
|> Enum.filter(&(selected in selected_option_texts(&1)))
590+
|> case do
622591
[] -> {:not_found, potential_matches}
623592
[found] -> {:found, found}
624593
[_ | _] = found_many -> {:found_many, found_many}
625594
end
626595
end
627596

628-
defp element_values(element) do
629-
List.wrap(Html.attribute(element, "value"))
630-
end
631-
632597
defp selected_option_texts(element) do
633598
case Html.element(element) do
634599
{"select", _attrs, _children} ->

0 commit comments

Comments
 (0)