dioxus/packages/core/tests/event_propagation.rs

70 lines
1.7 KiB
Rust
Raw Normal View History

2023-08-23 18:01:37 +00:00
use dioxus::prelude::*;
use dioxus_core::ElementId;
use std::{rc::Rc, sync::Mutex};
static CLICKS: Mutex<usize> = Mutex::new(0);
#[test]
fn events_propagate() {
let mut dom = VirtualDom::new(app);
_ = dom.rebuild();
// Top-level click is registered
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(1), true);
assert_eq!(*CLICKS.lock().unwrap(), 1);
// break reference....
for _ in 0..5 {
dom.mark_dirty(ScopeId(0));
2023-09-05 00:10:11 +00:00
_ = dom.render_immediate();
2023-08-23 18:01:37 +00:00
}
// Lower click is registered
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true);
assert_eq!(*CLICKS.lock().unwrap(), 3);
// break reference....
for _ in 0..5 {
dom.mark_dirty(ScopeId(0));
2023-09-05 00:10:11 +00:00
_ = dom.render_immediate();
2023-08-23 18:01:37 +00:00
}
// Stop propagation occurs
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(2), true);
assert_eq!(*CLICKS.lock().unwrap(), 3);
}
fn app(cx: Scope) -> Element {
render! {
div {
onclick: move |_| {
println!("top clicked");
*CLICKS.lock().unwrap() += 1;
},
2023-09-06 02:21:54 +00:00
vec![
render! {
problematic_child {}
}
].into_iter()
2023-08-23 18:01:37 +00:00
}
}
}
fn problematic_child(cx: Scope) -> Element {
render! {
button {
onclick: move |evt| {
2023-09-11 21:16:16 +00:00
println!("bottom clicked");
2023-08-23 18:01:37 +00:00
let mut clicks = CLICKS.lock().unwrap();
if *clicks == 3 {
evt.stop_propagation();
} else {
*clicks += 1;
}
}
}
}
}