Skip to content

Commit 1aaa48c

Browse files
committed
Extract Query label error messages
Minor refactoring. We extract the complex error messages in Query that are relatred to labels.
1 parent 52e369a commit 1aaa48c

2 files changed

Lines changed: 102 additions & 97 deletions

File tree

lib/phoenix_test/query.ex

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule PhoenixTest.Query do
44
alias PhoenixTest.Element
55
alias PhoenixTest.Html
66
alias PhoenixTest.Locators
7+
alias PhoenixTest.Query.LabelError
78

89
def find!(html, selector) do
910
case find(html, selector) do
@@ -204,103 +205,12 @@ defmodule PhoenixTest.Query do
204205
{:found, element} ->
205206
element
206207

207-
{:not_found, :no_label, potential_matches} ->
208-
if Enum.empty?(potential_matches) do
209-
msg = """
210-
Could not find element with label #{inspect(label)}
211-
"""
212-
213-
raise ArgumentError, msg
214-
else
215-
msg = """
216-
Could not find element with label #{inspect(label)} and provided selectors #{inspect(input_selectors)}.
217-
218-
Labels found
219-
============
220-
221-
#{Enum.map_join(potential_matches, "\n", &Html.raw/1)}
222-
223-
Searched for labeled elements with these selectors: #{format_selectors_for_error_msg(input_selectors)}
224-
"""
225-
226-
raise ArgumentError, msg
227-
end
228-
229-
{:not_found, :missing_for, found_label} ->
230-
msg = """
231-
Found label, but it doesn't have `for` attribute.
232-
233-
(Label's `for` attribute must point to element's `id`)
234-
235-
Label found
236-
===========
237-
238-
#{Html.raw(found_label)}
239-
"""
240-
241-
raise ArgumentError, msg
242-
243-
{:not_found, :missing_input, found_label} ->
244-
msg = """
245-
Found label but can't find labeled element whose `id` matches label's `for` attribute.
246-
247-
(Label's `for` attribute must point to element's `id`)
248-
249-
Label found
250-
===========
251-
252-
#{Html.raw(found_label)}
253-
254-
Searched for elements with these selectors: #{format_selectors_for_error_msg(input_selectors)}
255-
"""
256-
257-
raise ArgumentError, msg
258-
259-
{:not_found, :found_many_labels, potential_matches} ->
260-
msg = """
261-
Found many labels with text #{inspect(label)}:
262-
263-
#{Enum.map_join(potential_matches, "\n", &Html.raw/1)}
264-
"""
265-
266-
raise ArgumentError, msg
267-
268-
{:not_found, :found_many_labels_with_inputs, label_elements, input_elements} ->
269-
msg = """
270-
Found many elements with label #{inspect(label)} and matching the provided selectors.
271-
272-
Labels found
273-
============
274-
275-
#{Enum.map_join(label_elements, "\n", &Html.raw/1)}
276-
277-
Elements found
278-
==============
279-
280-
#{Enum.map_join(input_elements, "\n", &Html.raw/1)}
281-
"""
282-
283-
raise ArgumentError, msg
284-
285-
{:not_found, :mismatched_id, label_element, input_element} ->
286-
msg = """
287-
Found label and labeled element matching provided selectors. But the
288-
label's `for` attribute did not match the labeled element's `id`.
289-
290-
Label found
291-
============
292-
293-
#{Html.raw(label_element)}
294-
295-
Element found
296-
=============
297-
298-
#{Html.raw(input_element)}
299-
300-
Searched for elements with these selectors: #{format_selectors_for_error_msg(input_selectors)}
301-
"""
302-
303-
raise ArgumentError, msg
208+
error ->
209+
raise ArgumentError,
210+
LabelError.message(error, label,
211+
input_selectors: input_selectors,
212+
formatted_selectors: format_selectors_for_error_msg(input_selectors)
213+
)
304214
end
305215
end
306216

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
defmodule PhoenixTest.Query.LabelError do
2+
@moduledoc false
3+
4+
alias PhoenixTest.Html
5+
6+
def message({:not_found, :no_label, potential_matches}, label, opts) do
7+
if Enum.empty?(potential_matches) do
8+
"""
9+
Could not find element with label #{inspect(label)}
10+
"""
11+
else
12+
"""
13+
Could not find element with label #{inspect(label)} and provided selectors #{inspect(opts[:input_selectors])}.
14+
15+
Labels found
16+
============
17+
18+
#{Enum.map_join(potential_matches, "\n", &Html.raw/1)}
19+
20+
Searched for labeled elements with these selectors: #{opts[:formatted_selectors]}
21+
"""
22+
end
23+
end
24+
25+
def message({:not_found, :missing_for, found_label}, _label, _opts) do
26+
"""
27+
Found label, but it doesn't have `for` attribute.
28+
29+
(Label's `for` attribute must point to element's `id`)
30+
31+
Label found
32+
===========
33+
34+
#{Html.raw(found_label)}
35+
"""
36+
end
37+
38+
def message({:not_found, :missing_input, found_label}, _label, opts) do
39+
"""
40+
Found label but can't find labeled element whose `id` matches label's `for` attribute.
41+
42+
(Label's `for` attribute must point to element's `id`)
43+
44+
Label found
45+
===========
46+
47+
#{Html.raw(found_label)}
48+
49+
Searched for elements with these selectors: #{opts[:formatted_selectors]}
50+
"""
51+
end
52+
53+
def message({:not_found, :found_many_labels, potential_matches}, label, _opts) do
54+
"""
55+
Found many labels with text #{inspect(label)}:
56+
57+
#{Enum.map_join(potential_matches, "\n", &Html.raw/1)}
58+
"""
59+
end
60+
61+
def message({:not_found, :found_many_labels_with_inputs, label_elements, input_elements}, label, _opts) do
62+
"""
63+
Found many elements with label #{inspect(label)} and matching the provided selectors.
64+
65+
Labels found
66+
============
67+
68+
#{Enum.map_join(label_elements, "\n", &Html.raw/1)}
69+
70+
Elements found
71+
==============
72+
73+
#{Enum.map_join(input_elements, "\n", &Html.raw/1)}
74+
"""
75+
end
76+
77+
def message({:not_found, :mismatched_id, label_element, input_element}, _label, opts) do
78+
"""
79+
Found label and labeled element matching provided selectors. But the
80+
label's `for` attribute did not match the labeled element's `id`.
81+
82+
Label found
83+
============
84+
85+
#{Html.raw(label_element)}
86+
87+
Element found
88+
=============
89+
90+
#{Html.raw(input_element)}
91+
92+
Searched for elements with these selectors: #{opts[:formatted_selectors]}
93+
"""
94+
end
95+
end

0 commit comments

Comments
 (0)