diff --git a/Cargo.lock b/Cargo.lock index a4f33db..f058041 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + [[package]] name = "bumpalo" version = "3.16.0" @@ -42,6 +48,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "downcast" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" + +[[package]] +name = "fragile" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" + [[package]] name = "js-sys" version = "0.3.70" @@ -73,12 +91,64 @@ dependencies = [ "walkdir", ] +[[package]] +name = "mockall" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c28b3fb6d753d28c20e826cd46ee611fda1cf3cde03a443a974043247c065a" +dependencies = [ + "cfg-if", + "downcast", + "fragile", + "mockall_derive", + "predicates", + "predicates-tree", +] + +[[package]] +name = "mockall_derive" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "341014e7f530314e9a1fdbc7400b244efea7122662c96bfa248c31da5bfb2020" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "predicates" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9086cc7640c29a356d1a29fd134380bee9d8f79a17410aa76e7ad295f42c97" +dependencies = [ + "anstyle", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae8177bee8e75d6846599c6b9ff679ed51e882816914eec639944d7c9aa11931" + +[[package]] +name = "predicates-tree" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b740d195ed3166cd147c8047ec98db0e22ec019eb8eeb76d343b795304fb13" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -158,10 +228,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "testing-library-dom" version = "0.0.2" dependencies = [ + "mockall", "regex", "thiserror", "wasm-bindgen", diff --git a/packages/dom/Cargo.toml b/packages/dom/Cargo.toml index 89966a1..b27a04b 100644 --- a/packages/dom/Cargo.toml +++ b/packages/dom/Cargo.toml @@ -24,3 +24,4 @@ web-sys = { workspace = true, features = [ [dev-dependencies] wasm-bindgen-test.workspace = true +mockall = "0.13.0" diff --git a/packages/dom/src/lib.rs b/packages/dom/src/lib.rs index 7c4dc9d..d32ef3e 100644 --- a/packages/dom/src/lib.rs +++ b/packages/dom/src/lib.rs @@ -10,6 +10,7 @@ mod types; mod util; pub use config::{configure, get_config}; +pub use error::QueryError; pub use get_node_text::*; pub use get_queries_for_element::*; pub use matches::get_default_normalizer; diff --git a/packages/dom/tests/query_helpers.rs b/packages/dom/tests/query_helpers.rs index 8b13789..d7fc8a7 100644 --- a/packages/dom/tests/query_helpers.rs +++ b/packages/dom/tests/query_helpers.rs @@ -1 +1,42 @@ +use std::sync::Arc; +use mockall::automock; +use testing_library_dom::{ + configure, get_element_error, ConfigFnOrPartial, PartialConfig, QueryError, +}; +use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; +use web_sys::{Document, Element}; + +wasm_bindgen_test_configure!(run_in_browser); + +#[automock] +trait GetElementError { + fn get_element_error(message: Option, container: Element) -> QueryError; +} + +#[wasm_bindgen_test] +fn should_delegate_to_config_get_element_error() { + let message = Some("test Message".to_string()); + let container = Document::new() + .expect("Document should be created.") + .create_element("div") + .expect("Element should be created"); + + let mock = MockGetElementError::get_element_error_context(); + mock.expect() + .withf_st({ + let message = message.clone(); + let container = container.clone(); + + move |m, c| *m == message && *c == container + }) + .returning(|_, _| QueryError::Element("".into())); + + configure(ConfigFnOrPartial::Partial( + PartialConfig::default().get_element_error(Arc::new(|message, container| { + MockGetElementError::get_element_error(message, container) + })), + )); + + get_element_error(message, container); +}