2021-10-22 05:16:39 +00:00
|
|
|
#![allow(unused, non_upper_case_globals)]
|
2022-02-11 02:00:15 +00:00
|
|
|
#![allow(non_snake_case)]
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2021-10-22 05:16:39 +00:00
|
|
|
//! Tests for the lifecycle of components.
|
2024-01-16 01:14:11 +00:00
|
|
|
use dioxus::dioxus_core::{ElementId, Mutation::*};
|
2024-01-31 02:17:45 +00:00
|
|
|
use dioxus::html::SerializedHtmlEventConverter;
|
2022-11-27 07:06:04 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-11-27 14:38:40 +00:00
|
|
|
use std::rc::Rc;
|
2021-10-01 06:07:12 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2021-09-25 01:46:23 +00:00
|
|
|
|
2021-10-01 06:07:12 +00:00
|
|
|
type Shared<T> = Arc<Mutex<T>>;
|
2021-09-13 04:59:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn manual_diffing() {
|
2024-01-11 21:18:11 +00:00
|
|
|
#[derive(Clone)]
|
2021-09-13 04:59:08 +00:00
|
|
|
struct AppProps {
|
|
|
|
value: Shared<&'static str>,
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
fn app(cx: AppProps) -> Element {
|
2024-01-11 17:11:44 +00:00
|
|
|
let val = cx.value.lock().unwrap();
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! { div { "{val}" } }
|
2021-09-13 04:59:08 +00:00
|
|
|
};
|
|
|
|
|
2021-10-01 06:07:12 +00:00
|
|
|
let value = Arc::new(Mutex::new("Hello"));
|
2022-11-27 14:38:40 +00:00
|
|
|
let mut dom = VirtualDom::new_with_props(app, AppProps { value: value.clone() });
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2024-01-15 17:06:27 +00:00
|
|
|
dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2021-10-01 06:07:12 +00:00
|
|
|
*value.lock().unwrap() = "goodbye";
|
2021-09-13 04:59:08 +00:00
|
|
|
|
2022-11-27 14:38:40 +00:00
|
|
|
assert_eq!(
|
2024-01-11 01:21:15 +00:00
|
|
|
dom.rebuild_to_vec().santize().edits,
|
2022-11-27 14:38:40 +00:00
|
|
|
[
|
|
|
|
LoadTemplate { name: "template", index: 0, id: ElementId(3) },
|
2024-01-11 01:21:15 +00:00
|
|
|
HydrateText { path: &[0], value: "goodbye".to_string(), id: ElementId(4) },
|
2022-12-03 00:24:49 +00:00
|
|
|
AppendChildren { m: 1, id: ElementId(0) }
|
2022-11-27 14:38:40 +00:00
|
|
|
]
|
|
|
|
);
|
2021-09-13 04:59:08 +00:00
|
|
|
}
|
2021-11-11 16:49:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn events_generate() {
|
2023-09-05 00:17:43 +00:00
|
|
|
set_event_converter(Box::new(SerializedHtmlEventConverter));
|
2024-01-11 16:48:04 +00:00
|
|
|
fn app() -> Element {
|
2024-01-11 21:18:11 +00:00
|
|
|
let mut count = use_signal(|| 0);
|
2021-11-11 16:49:07 +00:00
|
|
|
|
2024-01-16 21:51:02 +00:00
|
|
|
match count() {
|
2024-01-16 19:18:46 +00:00
|
|
|
0 => rsx! {
|
2024-01-11 21:18:11 +00:00
|
|
|
div { onclick: move |_| count += 1,
|
2022-11-27 14:38:40 +00:00
|
|
|
div { "nested" }
|
|
|
|
"Click me!"
|
2021-11-11 16:49:07 +00:00
|
|
|
}
|
2024-01-11 01:21:15 +00:00
|
|
|
},
|
2024-01-11 03:33:34 +00:00
|
|
|
_ => None,
|
2022-11-27 14:38:40 +00:00
|
|
|
}
|
2021-11-11 16:49:07 +00:00
|
|
|
};
|
|
|
|
|
2022-01-30 23:34:24 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2024-01-15 17:06:27 +00:00
|
|
|
dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2022-11-27 14:38:40 +00:00
|
|
|
|
2023-09-01 20:38:55 +00:00
|
|
|
dom.handle_event(
|
|
|
|
"click",
|
2023-09-06 02:30:20 +00:00
|
|
|
Rc::new(PlatformEventData::new(Box::<SerializedMouseData>::default())),
|
2023-09-01 20:38:55 +00:00
|
|
|
ElementId(1),
|
|
|
|
true,
|
|
|
|
);
|
2021-11-11 16:49:07 +00:00
|
|
|
|
2023-08-25 13:31:23 +00:00
|
|
|
dom.mark_dirty(ScopeId::ROOT);
|
2024-01-11 01:21:15 +00:00
|
|
|
let edits = dom.render_immediate_to_vec();
|
2021-11-11 16:49:07 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-12-01 05:46:15 +00:00
|
|
|
edits.edits,
|
2021-11-11 16:49:07 +00:00
|
|
|
[
|
2022-11-27 14:38:40 +00:00
|
|
|
CreatePlaceholder { id: ElementId(2) },
|
|
|
|
ReplaceWith { id: ElementId(1), m: 1 }
|
2021-11-11 16:49:07 +00:00
|
|
|
]
|
2022-11-27 14:38:40 +00:00
|
|
|
)
|
2021-11-11 16:49:07 +00:00
|
|
|
}
|