Skip to content

Commit

Permalink
Add start of role queries
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Sep 20, 2024
1 parent e639d0b commit a35bbf2
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]()
1 change: 1 addition & 0 deletions packages/dom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions packages/dom/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::*;
45 changes: 45 additions & 0 deletions packages/dom/src/queries/role.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use web_sys::HtmlElement;

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

pub fn _query_all_by_role<M: Into<Matcher>>(
_container: &HtmlElement,
_alt: M,
options: ByRoleOptions,
) -> Result<Vec<HtmlElement>, QueryError> {
if options.selected.is_some() {
// TODO
}

Ok(vec![])
}

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

fn get_missing_error(
_container: &HtmlElement,
alt: Matcher,
_options: ByRoleOptions,
) -> Result<String, QueryError> {
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,
};
2 changes: 2 additions & 0 deletions packages/dom/src/types.rs
Original file line number Diff line number Diff line change
@@ -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::*;
116 changes: 116 additions & 0 deletions packages/dom/src/types/queries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use regex::Regex;
// use web_sys::Element;

use crate::types::Matcher;

#[derive(Clone, Default)]
pub struct ByRoleOptionsValue {
pub now: Option<isize>,
pub min: Option<isize>,
pub max: Option<isize>,
pub text: Option<Matcher>,
}

#[derive(Clone)]
pub enum ByRoleOptionsCurrent {
Bool(bool),
String(String),
}

#[derive(Clone)]
pub enum ByRoleOptionsName {
Regex(Regex),
String(String),
// Fn(Box<dyn Fn(String, Element) -> bool>),
}

#[derive(Clone)]
pub enum ByRoleOptionsDescription {
Regex(Regex),
String(String),
// Fn(Box<dyn Fn(String, Element) -> bool>),
}

#[derive(Clone, Default)]
pub struct ByRoleOptions {
pub suggest: Option<bool>,
pub hidden: Option<bool>,
pub selected: Option<bool>,
pub busy: Option<bool>,
pub checked: Option<bool>,
pub pressed: Option<bool>,
pub current: Option<ByRoleOptionsCurrent>,
pub expanded: Option<bool>,
pub level: Option<isize>,
pub value: Option<ByRoleOptionsValue>,
pub query_fallbacks: Option<bool>,
pub name: Option<ByRoleOptionsName>,
pub description: Option<ByRoleOptionsDescription>,
}

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
}
}

0 comments on commit a35bbf2

Please sign in to comment.