@@ -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
0 commit comments