dioxus/packages/core/tests/lifecycle.rs

80 lines
2 KiB
Rust
Raw Normal View History

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