diff --git a/Cargo.lock b/Cargo.lock index e4724d0..cf9cc0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" +[[package]] +name = "aria-query" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22b7f13ec201267aa3eb14a08d0e7a9beae90fb748aaeac072c2681a8f3df02" + [[package]] name = "bumpalo" version = "3.16.0" @@ -266,6 +272,7 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" name = "testing-library-dom" version = "0.0.1" dependencies = [ + "aria-query", "indoc", "log", "mockall", diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index b8ed5a3..ed4a51a 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -2,5 +2,32 @@ - [Introduction](./introduction.md) - [Core API](./core/README.md) + - [Queries]() + - [About Queries]() + - [By Role]() + - [By Label Text]() + - [By Placeholder Text]() + - [By Text]() + - [By Display Value]() + - [By Alt Text]() + - [By Title]() + - [By Test ID]() + - [User Actions]() + - [Firing Events]() + - [Async Methods]() + - [Appearance and Disappearance]() + - [Considerations]() + - [Advanced]() + - [Accessibility]() + - [Custom Queries]() + - [Debugging]() + - [Querying Within Elements]() + - [Configuration Options]() - [Frameworks](./frameworks/README.md) - [DOM Testing Library](./frameworks/dom.md) + - [Introduction]() + - [Install]() + - [Example]() + - [Setup]() + - [API]() + - [Cheatsheet]() diff --git a/packages/dom/Cargo.toml b/packages/dom/Cargo.toml index 6944ef4..d68d92c 100644 --- a/packages/dom/Cargo.toml +++ b/packages/dom/Cargo.toml @@ -10,6 +10,7 @@ repository.workspace = true version.workspace = true [dependencies] +aria-query = "0.0.1" log.workspace = true paste = "1.0.15" pretty-format = { path = "../pretty-format", version = "0.0.1" } diff --git a/packages/dom/src/queries.rs b/packages/dom/src/queries.rs index 585b0fe..ab89985 100644 --- a/packages/dom/src/queries.rs +++ b/packages/dom/src/queries.rs @@ -2,6 +2,7 @@ mod alt_text; mod display_value; mod label_text; mod placeholder_text; +mod role; mod test_id; mod text; mod title; @@ -10,6 +11,7 @@ pub use alt_text::*; pub use display_value::*; pub use label_text::*; pub use placeholder_text::*; +pub use role::*; pub use test_id::*; pub use text::*; pub use title::*; diff --git a/packages/dom/src/queries/role.rs b/packages/dom/src/queries/role.rs new file mode 100644 index 0000000..f0627bc --- /dev/null +++ b/packages/dom/src/queries/role.rs @@ -0,0 +1,45 @@ +use web_sys::HtmlElement; + +use crate::{ + build_queries, + error::QueryError, + types::{ByRoleOptions, Matcher}, +}; + +pub fn _query_all_by_role>( + _container: &HtmlElement, + _alt: M, + options: ByRoleOptions, +) -> Result, QueryError> { + if options.selected.is_some() { + // TODO + } + + Ok(vec![]) +} + +fn get_multiple_error(_container: &HtmlElement, alt: Matcher) -> Result { + Ok(format!("Found multiple elements with the alt text: {alt}")) +} + +fn get_missing_error( + _container: &HtmlElement, + alt: Matcher, + _options: ByRoleOptions, +) -> Result { + Ok(format!( + "Unable to find an element with the alt text: {alt}" + )) +} + +build_queries!( + _query_all_by_role, + get_multiple_error, + get_missing_error, + role, + crate::types::ByRoleOptions +); + +pub use internal::{ + find_all_by_role, find_by_role, get_all_by_role, get_by_role, query_all_by_role, query_by_role, +}; diff --git a/packages/dom/src/types.rs b/packages/dom/src/types.rs index 94f7d00..811f577 100644 --- a/packages/dom/src/types.rs +++ b/packages/dom/src/types.rs @@ -1,9 +1,11 @@ mod config; mod matches; +mod queries; mod suggestions; mod wait_for; pub use config::*; pub use matches::*; +pub use queries::*; pub use suggestions::*; pub use wait_for::*; diff --git a/packages/dom/src/types/queries.rs b/packages/dom/src/types/queries.rs new file mode 100644 index 0000000..25afd2f --- /dev/null +++ b/packages/dom/src/types/queries.rs @@ -0,0 +1,116 @@ +use regex::Regex; +// use web_sys::Element; + +use crate::types::Matcher; + +#[derive(Clone, Default)] +pub struct ByRoleOptionsValue { + pub now: Option, + pub min: Option, + pub max: Option, + pub text: Option, +} + +#[derive(Clone)] +pub enum ByRoleOptionsCurrent { + Bool(bool), + String(String), +} + +#[derive(Clone)] +pub enum ByRoleOptionsName { + Regex(Regex), + String(String), + // Fn(Box bool>), +} + +#[derive(Clone)] +pub enum ByRoleOptionsDescription { + Regex(Regex), + String(String), + // Fn(Box bool>), +} + +#[derive(Clone, Default)] +pub struct ByRoleOptions { + pub suggest: Option, + pub hidden: Option, + pub selected: Option, + pub busy: Option, + pub checked: Option, + pub pressed: Option, + pub current: Option, + pub expanded: Option, + pub level: Option, + pub value: Option, + pub query_fallbacks: Option, + pub name: Option, + pub description: Option, +} + +impl ByRoleOptions { + pub fn suggest(mut self, value: bool) -> Self { + self.suggest = Some(value); + self + } + + pub fn hidden(mut self, value: bool) -> Self { + self.hidden = Some(value); + self + } + + pub fn selected(mut self, value: bool) -> Self { + self.selected = Some(value); + self + } + + pub fn busy(mut self, value: bool) -> Self { + self.busy = Some(value); + self + } + + pub fn checked(mut self, value: bool) -> Self { + self.checked = Some(value); + self + } + + pub fn pressed(mut self, value: bool) -> Self { + self.pressed = Some(value); + self + } + + pub fn current(mut self, value: ByRoleOptionsCurrent) -> Self { + self.current = Some(value); + self + } + + pub fn expanded(mut self, value: bool) -> Self { + self.expanded = Some(value); + self + } + + pub fn level(mut self, value: isize) -> Self { + self.level = Some(value); + self + } + + pub fn value(mut self, value: ByRoleOptionsValue) -> Self { + self.value = Some(value); + self + } + + pub fn query_fallbacks(mut self, value: bool) -> Self { + self.query_fallbacks = Some(value); + self + } + + pub fn name(mut self, value: ByRoleOptionsName) -> Self { + self.name = Some(value); + self + } + + pub fn description(mut self, value: ByRoleOptionsDescription) -> Self { + self.description = Some(value); + self + } +}