testing_library_dom/
get_queries_for_element.rs

1use web_sys::HtmlElement;
2
3use crate::{
4    error::QueryError,
5    queries::*,
6    types::{Matcher, MatcherOptions, SelectorMatcherOptions, WaitForOptions},
7    ByRoleMatcher, ByRoleOptions,
8};
9
10pub fn get_queries_for_element(element: HtmlElement) -> BoundFunctions {
11    BoundFunctions { element }
12}
13
14pub struct BoundFunctions {
15    element: HtmlElement,
16}
17
18macro_rules! queries_for_element {
19    ($(($name:ident, $matcher_type:ty, $options_type:ty)),*,) => {
20        paste::paste! {
21            impl BoundFunctions {
22                $(pub fn [< find_by_ $name >]<M: Into<$matcher_type>>(
23                    &self,
24                    matcher: M,
25                    options: $options_type,
26                    wait_for_options: WaitForOptions,
27                ) -> Result<Option<HtmlElement>, QueryError> {
28                    [< find_by_ $name >](&self.element, matcher, options, wait_for_options)
29                })*
30
31                $(pub fn [< find_all_by_ $name >]<M: Into<$matcher_type>>(
32                    &self,
33                    matcher: M,
34                    options: $options_type,
35                    wait_for_options: WaitForOptions,
36                ) -> Result<Vec<HtmlElement>, QueryError> {
37                    [< find_all_by_ $name >](&self.element, matcher, options, wait_for_options)
38                })*
39
40                $(pub fn [< get_by_ $name >]<M: Into<$matcher_type>>(
41                    &self,
42                    matcher: M,
43                    options: $options_type,
44                ) -> Result<Option<HtmlElement>, QueryError> {
45                    [< get_by_ $name >](&self.element, matcher, options)
46                })*
47
48                $(pub fn [< get_all_by_ $name >]<M: Into<$matcher_type>>(
49                    &self,
50                    matcher: M,
51                    options: $options_type,
52                ) -> Result<Vec<HtmlElement>, QueryError> {
53                    [< get_all_by_ $name >](&self.element, matcher, options)
54                })*
55
56                $(pub fn [< query_by_ $name >]<M: Into<$matcher_type>>(
57                    &self,
58                    matcher: M,
59                    options: $options_type,
60                ) -> Result<Option<HtmlElement>, QueryError> {
61                    [< query_by_ $name >](&self.element, matcher, options)
62                })*
63
64                $(pub fn [< query_all_by_ $name >]<M: Into<$matcher_type>>(
65                    &self,
66                    matcher: M,
67                    options: $options_type,
68                ) -> Result<Vec<HtmlElement>, QueryError> {
69                    [< query_all_by_ $name >](&self.element, matcher, options)
70                })*
71            }
72        }
73    }
74}
75
76queries_for_element!(
77    (alt_text, Matcher, MatcherOptions),
78    (display_value, Matcher, MatcherOptions),
79    (label_text, Matcher, SelectorMatcherOptions),
80    (placeholder_text, Matcher, MatcherOptions),
81    (role, ByRoleMatcher, ByRoleOptions),
82    (test_id, Matcher, MatcherOptions),
83    (text, Matcher, SelectorMatcherOptions),
84    (title, Matcher, MatcherOptions),
85);