Skip to content

Commit

Permalink
Add placeholder text queries
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Sep 4, 2024
1 parent 795033b commit b4c74d2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/dom/src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod alt_text;
mod display_value;
mod placeholder_text;

pub use alt_text::*;
pub use display_value::*;
pub use placeholder_text::*;
36 changes: 36 additions & 0 deletions packages/dom/src/queries/placeholder_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use web_sys::HtmlElement;

use crate::{
build_queries,
error::QueryError,
query_all_by_attribute,
types::{Matcher, MatcherOptions},
};

pub fn _query_all_by_placeholder_text<M: Into<Matcher>>(
container: &HtmlElement,
text: M,
options: MatcherOptions,
) -> Result<Vec<HtmlElement>, QueryError> {
query_all_by_attribute("placeholder".to_string(), container, text, options)
}

fn get_multiple_error(_container: &HtmlElement, text: Matcher) -> String {
format!("Found multiple elements with the placeholder text: {text}")
}

fn get_missing_error(_container: &HtmlElement, text: Matcher) -> String {
format!("Unable to find an element with the placeholder text: {text}")
}

build_queries!(
_query_all_by_placeholder_text,
get_multiple_error,
get_missing_error,
placeholder_text
);

pub use internal::{
find_all_by_placeholder_text, find_by_placeholder_text, get_all_by_placeholder_text,
get_by_placeholder_text, query_all_by_placeholder_text, query_by_placeholder_text,
};
15 changes: 15 additions & 0 deletions packages/dom/tests/element_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ fn query_can_return_none() -> Result<(), QueryError> {
assert!(container_queries
.query_by_display_value("LucyRicardo", MatcherOptions::default())?
.is_none());
assert!(container_queries
.query_by_placeholder_text("LucyRicardo", MatcherOptions::default())?
.is_none());
assert!(container_queries
.query_by_alt_text("LucyRicardo", MatcherOptions::default())?
.is_none());
Expand All @@ -63,6 +66,18 @@ fn get_throws_a_useful_error_message() -> Result<(), QueryError> {
container_queries, ..
} = render("<div></div><!-- Ignored comment --><style type=\"text/css\">body {} </style><script type=\"text/javascript\"></script>", None);

assert_eq!(
Err(QueryError::Element(
"Unable to find an element with the placeholder text: LucyRicardo\n\
\n\
Ignored nodes: comments, script, style\n\
<div>\n\
<div />\n\
</div>"
.into()
)),
container_queries.get_by_placeholder_text("LucyRicardo", MatcherOptions::default())
);
assert_eq!(
Err(QueryError::Element(
"Unable to find an element with the alt text: LucyRicardo\n\
Expand Down

0 comments on commit b4c74d2

Please sign in to comment.