Skip to content

Commit

Permalink
Add macro for bound functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Sep 7, 2024
1 parent c03893c commit 426d7dc
Showing 1 changed file with 57 additions and 82 deletions.
139 changes: 57 additions & 82 deletions packages/dom/src/get_queries_for_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,74 @@ use web_sys::HtmlElement;

use crate::{
error::QueryError,
get_by_placeholder_text, get_by_test_id, get_by_title,
queries::{get_by_alt_text, get_by_display_value, query_by_alt_text, query_by_display_value},
query_by_placeholder_text, query_by_test_id, query_by_title,
types::{Matcher, MatcherOptions},
queries::*,
types::{Matcher, MatcherOptions, WaitForOptions},
};

pub fn get_queries_for_element(element: HtmlElement) -> BoundFunctions {
BoundFunctions { element }
}

pub struct BoundFunctions {
element: HtmlElement,
}

impl BoundFunctions {
pub fn get_by_alt_text<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
get_by_alt_text(&self.element, matcher, options)
}

pub fn get_by_display_value<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
get_by_display_value(&self.element, matcher, options)
}

pub fn get_by_placeholder_text<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
get_by_placeholder_text(&self.element, matcher, options)
}

pub fn get_by_test_id<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
get_by_test_id(&self.element, matcher, options)
}
macro_rules! queries_for_element {
($($name:ident),*) => {
paste::paste! {
impl BoundFunctions {
$(pub fn [< find_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
wait_for_options: WaitForOptions,
) -> Result<Option<HtmlElement>, QueryError> {
[< find_by_ $name >](&self.element, matcher, options, wait_for_options)
})*

pub fn get_by_title<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
get_by_title(&self.element, matcher, options)
}
$(pub fn [< find_all_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
wait_for_options: WaitForOptions,
) -> Result<Vec<HtmlElement>, QueryError> {
[< find_all_by_ $name >](&self.element, matcher, options, wait_for_options)
})*

pub fn query_by_alt_text<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
query_by_alt_text(&self.element, matcher, options)
}

pub fn query_by_display_value<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
query_by_display_value(&self.element, matcher, options)
}
$(pub fn [< get_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
[< get_by_ $name >](&self.element, matcher, options)
})*

pub fn query_by_placeholder_text<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
query_by_placeholder_text(&self.element, matcher, options)
}
$(pub fn [< get_all_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Vec<HtmlElement>, QueryError> {
[< get_all_by_ $name >](&self.element, matcher, options)
})*

pub fn query_by_test_id<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
query_by_test_id(&self.element, matcher, options)
}
$(pub fn [< query_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
[< query_by_ $name >](&self.element, matcher, options)
})*

pub fn query_by_title<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Option<HtmlElement>, QueryError> {
query_by_title(&self.element, matcher, options)
$(pub fn [< query_all_by_ $name >]<M: Into<Matcher>>(
&self,
matcher: M,
options: MatcherOptions,
) -> Result<Vec<HtmlElement>, QueryError> {
[< query_all_by_ $name >](&self.element, matcher, options)
})*
}
}
}
}

pub fn get_queries_for_element(element: HtmlElement) -> BoundFunctions {
// TODO
BoundFunctions { element }
}
queries_for_element!(alt_text, display_value, placeholder_text, test_id, title);

0 comments on commit 426d7dc

Please sign in to comment.