Skip to content

Commit 264c59a

Browse files
Add role query to container queries
1 parent dd02df2 commit 264c59a

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

packages/dom/src/get_queries_for_element.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
error::QueryError,
55
queries::*,
66
types::{Matcher, MatcherOptions, SelectorMatcherOptions, WaitForOptions},
7+
ByRoleMatcher, ByRoleOptions,
78
};
89

910
pub fn get_queries_for_element(element: HtmlElement) -> BoundFunctions {
@@ -15,10 +16,10 @@ pub struct BoundFunctions {
1516
}
1617

1718
macro_rules! queries_for_element {
18-
($(($name:ident, $options_type:ty)),*,) => {
19+
($(($name:ident, $matcher_type:ty, $options_type:ty)),*,) => {
1920
paste::paste! {
2021
impl BoundFunctions {
21-
$(pub fn [< find_by_ $name >]<M: Into<Matcher>>(
22+
$(pub fn [< find_by_ $name >]<M: Into<$matcher_type>>(
2223
&self,
2324
matcher: M,
2425
options: $options_type,
@@ -27,7 +28,7 @@ macro_rules! queries_for_element {
2728
[< find_by_ $name >](&self.element, matcher, options, wait_for_options)
2829
})*
2930

30-
$(pub fn [< find_all_by_ $name >]<M: Into<Matcher>>(
31+
$(pub fn [< find_all_by_ $name >]<M: Into<$matcher_type>>(
3132
&self,
3233
matcher: M,
3334
options: $options_type,
@@ -36,31 +37,31 @@ macro_rules! queries_for_element {
3637
[< find_all_by_ $name >](&self.element, matcher, options, wait_for_options)
3738
})*
3839

39-
$(pub fn [< get_by_ $name >]<M: Into<Matcher>>(
40+
$(pub fn [< get_by_ $name >]<M: Into<$matcher_type>>(
4041
&self,
4142
matcher: M,
4243
options: $options_type,
4344
) -> Result<Option<HtmlElement>, QueryError> {
4445
[< get_by_ $name >](&self.element, matcher, options)
4546
})*
4647

47-
$(pub fn [< get_all_by_ $name >]<M: Into<Matcher>>(
48+
$(pub fn [< get_all_by_ $name >]<M: Into<$matcher_type>>(
4849
&self,
4950
matcher: M,
5051
options: $options_type,
5152
) -> Result<Vec<HtmlElement>, QueryError> {
5253
[< get_all_by_ $name >](&self.element, matcher, options)
5354
})*
5455

55-
$(pub fn [< query_by_ $name >]<M: Into<Matcher>>(
56+
$(pub fn [< query_by_ $name >]<M: Into<$matcher_type>>(
5657
&self,
5758
matcher: M,
5859
options: $options_type,
5960
) -> Result<Option<HtmlElement>, QueryError> {
6061
[< query_by_ $name >](&self.element, matcher, options)
6162
})*
6263

63-
$(pub fn [< query_all_by_ $name >]<M: Into<Matcher>>(
64+
$(pub fn [< query_all_by_ $name >]<M: Into<$matcher_type>>(
6465
&self,
6566
matcher: M,
6667
options: $options_type,
@@ -73,11 +74,12 @@ macro_rules! queries_for_element {
7374
}
7475

7576
queries_for_element!(
76-
(alt_text, MatcherOptions),
77-
(display_value, MatcherOptions),
78-
(label_text, SelectorMatcherOptions),
79-
(placeholder_text, MatcherOptions),
80-
(test_id, MatcherOptions),
81-
(text, SelectorMatcherOptions),
82-
(title, MatcherOptions),
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),
8385
);

packages/dom/tests/element_queries.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,22 @@ fn get_throws_a_useful_error_message() -> Result<(), QueryError> {
193193
)),
194194
container_queries.get_by_display_value("LucyRicardo", MatcherOptions::default())
195195
);
196+
// TODO
197+
// assert_eq!(
198+
// Err(QueryError::Element(
199+
// indoc! {"
200+
// Unable to find an accessible element with the role: \"LucyRicardo\"
201+
202+
// There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
203+
204+
// Ignored nodes: comments, script, style
205+
// <div>
206+
// <div />
207+
// </div>"}
208+
// .into()
209+
// )),
210+
// container_queries.get_by_role("LucyRicardo", ByRoleOptions::default())
211+
// );
196212

197213
after_each();
198214

0 commit comments

Comments
 (0)