diff --git a/README.md b/README.md index 9510d3d..49bc0f7 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ Rust port of [Testing Library](https://testing-library.com/). Rust Testing Library is available for these Rust frameworks: -- [DOM](./packages/dom) ([`web-sys`](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html)) +- [DOM](./packages/dom) ([`web-sys`](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html)) The following frameworks are under consideration: -- [Dioxus](https://dioxuslabs.com/) -- [Leptos](https://leptos.dev/) -- [Yew](https://yew.rs/) +- [Dioxus](https://dioxuslabs.com/) +- [Leptos](https://leptos.dev/) +- [Yew](https://yew.rs/) See [the Rust Testing Library book](https://testing-library.rustforweb.org/introduction.html#frameworks) for more information. diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 75fc41c..6a3c761 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -1,34 +1,34 @@ # Summary -- [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]() -- [Contributing]() +- [Introduction](./introduction.md) +- [Core API](./core/README.md) + - [Queries](./core/queries/README.md) + - [About Queries](./core/queries/about-queries.md) + - [By Role](./core/queries/by-role.md) + - [By Label Text](./core/queries/by-label-text.md) + - [By Placeholder Text]() + - [By Text]() + - [By Display Value]() + - [By Alt Text]() + - [By Title]() + - [By Test ID]() + - [User Actions](./core/user-actions/README.md) + - [Firing Events](./core/user-actions/firing-events.md) + - [Async Methods]() + - [Appearance and Disappearance]() + - [Considerations]() + - [Advanced](./core/advanced/README.md) + - [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]() +- [Contributing]() diff --git a/book/src/core/README.md b/book/src/core/README.md index 24d73e4..45b7e60 100644 --- a/book/src/core/README.md +++ b/book/src/core/README.md @@ -1,3 +1,5 @@ # Core API -TODO +- [Queries](./queries/README.md) +- [User Actions](./user-actions/README.md) +- [Advanced](./advanced/README.md) diff --git a/book/src/core/advanced/README.md b/book/src/core/advanced/README.md new file mode 100644 index 0000000..da10b4a --- /dev/null +++ b/book/src/core/advanced/README.md @@ -0,0 +1,3 @@ +# Advanced + +TODO diff --git a/book/src/core/queries/README.md b/book/src/core/queries/README.md new file mode 100644 index 0000000..0da7501 --- /dev/null +++ b/book/src/core/queries/README.md @@ -0,0 +1,11 @@ +# Queries + +- [About Queries](./about-queries.md) +- [By Role](./by-role.md) + diff --git a/book/src/core/queries/about-queries.md b/book/src/core/queries/about-queries.md new file mode 100644 index 0000000..702f139 --- /dev/null +++ b/book/src/core/queries/about-queries.md @@ -0,0 +1,3 @@ +# About Queries + +TODO diff --git a/book/src/core/queries/by-label-text.md b/book/src/core/queries/by-label-text.md new file mode 100644 index 0000000..5ecb942 --- /dev/null +++ b/book/src/core/queries/by-label-text.md @@ -0,0 +1,3 @@ +# By Label Text + +TODO diff --git a/book/src/core/queries/by-role.md b/book/src/core/queries/by-role.md new file mode 100644 index 0000000..b612501 --- /dev/null +++ b/book/src/core/queries/by-role.md @@ -0,0 +1,124 @@ +# By Role + +> `get_by_role`, `query_by_role`, `get_all_by_role`, `query_all_by_role`, `find_by_role`, `find_all_by_role` + +## API + +```rust,ignore +use aria_query::AriaRole; +use testing_library_dom::{Matcher, QueryError}; +use web_sys::HtmlElement; + +fn get_by_role( + // If you're using `screen`, then skip the container argument: + container: &HtmlElement, + role: AriaRole, + options: ByRoleOptions +) -> Result, QueryError>; + +struct ByRoleOptions { + hidden: Option, + name: Option, + description: Option, + selected: Option, + busy: Option, + checked: Option, + pressed: Option, + suggest: Option, + current: Option, + expanded: Option, + query_fallbacks: Option, + level: Option, + value: Option, +} + +enum ByRoleOptionsCurrent { + Bool(bool), + String(String), +} + +struct ByRoleOptionsValue { + now: Option, + min: Option, + max: Option, + text: Option, +} +``` + +Queries for elements with the given role . Default roles are taken into consideration e.g. ` + +use testing_library_dom::{get_by_text, fire_event}; +use web_sys::MouseEvent; + +let event = MouseEvent::new("click"); +event.set_bubbles(true); +event.set_cancelable(true); + +fire_event( + get_by_text(&container, "Submit") + .expect("Get should succeed.") + .expect("Get should return an element."), + &event, +).expect("Event should be fired."); +``` + +## `FireEvent::[]` + +```rust,ignore +fn [](node: &EventTarget, event_properties: []) -> Result; +``` + +Convenience methods for firing DOM events. Check out `src/events.rs` for a full list as well as default event proprties. + + + +### Keyboard events + +There are three event types related to keyboard input - `keyPress`, `keyDown`, and `keyUp`. When firing these you need to reference an element in the DOM and the key you want to fire. + +```rust,ignore +use testing_library_dom::FireEvent; +use web_sys::KeyboardEventInit; + +let init = KeyboardEventInit::new(); +init.set_key("Enter"); +init.set_code("Enter"); +init.set_char_code(13); +FireEvent::key_down(&dom_node, &init).expect("Event should be fired."); + + +let init = KeyboardEventInit::new(); +init.set_key("A"); +init.set_code("KeyA"); +FireEvent::key_down(&dom_node, &init).expect("Event should be fired."); +``` + +You can find out which key code to use at https://www.toptal.com/developers/keycode. + +## `CreateEvent::[]` + +```rust,ignore +fn [](node: &EventTarget, event_properties: []) -> Result<[], CreateOrFireEventError>; +``` + +Convenience methods for creating DOM events that can then be fired by `fire_event`, allowing you to have a reference to the event created: this might be useful if you need to access event properties that cannot be initiated programmatically (such as [`time_stamp`](https://docs.rs/web-sys/latest/web_sys/struct.Event.html#method.time_stamp)). + +```rust,ignore +use testing_library_dom::{CreateEvent, fire_event}; +use web_sys::MouseEventInit; + +let init = MouseEventInit::new(); +init.set_button(2); +let my_event = CreateEvent::click(&node, &init).expect("Event should be created."); + +fire_event(&node, &my_event).expect("Event should be fired."); + +// `my_event.time_stamp()` can be accessed just like any other properties from `my_event`. +// Note: The access to the events created by `create_event` is based on the native event API. +// Therefore, native properties of HTML Event object (e.g. `timeStamp`, `cancelable`, `type`) should be set using `Object.defineProperty`. +// For more info see: https://developer.mozilla.org/en-US/docs/Web/API/Event. +``` + +## `create_event` + +```rust,ignore +use testing_library_dom::{CreateEventError, EventType}; +use web_sys::EventTarget; + +fn create_event( + event_name: &str, + node: &EventTarget, + init: Option, + options: CreateEventOptions, +) -> Result; + +struct CreateEventOptions<'a, E: EventType> { + default_init: Option<&'a DefaultInitFn>, +} + +type DefaultInitFn = dyn Fn(&::Init); +``` + +Create DOM events. + +```rust,ignore +use testing_library_dom::{CreateEventOptions, create_event}; +use web_sys::{InputEvent, InputEventInit}; + +let init = InputEventInit::new(); +init.set_data(Some("a")); +let event = create_event::( + "input", + &input, + Some(&init), + CreateEventOptions::default() +).expect("Event should be created."); +``` diff --git a/book/src/frameworks/README.md b/book/src/frameworks/README.md index 50b88ab..ae4aa15 100644 --- a/book/src/frameworks/README.md +++ b/book/src/frameworks/README.md @@ -1,3 +1,3 @@ # Frameworks -- [DOM Testing Library](./dom.md) +- [DOM Testing Library](./dom.md) diff --git a/book/src/introduction.md b/book/src/introduction.md index bc7dd84..304b06c 100644 --- a/book/src/introduction.md +++ b/book/src/introduction.md @@ -12,13 +12,13 @@ Rust Testing Library is a Rust port of [Testing Library](https://testing-library Rust Testing Library is available for these Rust frameworks: -- [DOM](./packages/dom) ([`web-sys`](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html)) +- [DOM](./packages/dom) ([`web-sys`](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html)) The following frameworks are under consideration: -- [Dioxus](https://dioxuslabs.com/) -- [Leptos](https://leptos.dev/) -- [Yew](https://yew.rs/) +- [Dioxus](https://dioxuslabs.com/) +- [Leptos](https://leptos.dev/) +- [Yew](https://yew.rs/) ### Support