dioxus/packages/core/tests/lifecycle.rs

177 lines
4.8 KiB
Rust
Raw Normal View History

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-10-22 05:16:39 +00:00
//! Tests for the lifecycle of components.
2022-11-27 07:06:04 +00:00
use dioxus::core::{ElementId, Mutation::*};
use dioxus::prelude::*;
2023-09-05 00:17:43 +00:00
use dioxus_html::SerializedHtmlEventConverter;
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>>;
#[test]
fn manual_diffing() {
2024-01-11 21:18:11 +00:00
#[derive(Clone)]
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-11 01:21:15 +00:00
render! { div { "{val}" } }
};
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() });
2024-01-15 17:06:27 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2021-10-01 06:07:12 +00:00
*value.lock().unwrap() = "goodbye";
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-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));
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-11 21:18:11 +00:00
match *count() {
2024-01-11 01:21:15 +00:00
0 => render! {
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
},
_ => None,
2022-11-27 14:38:40 +00:00
}
2021-11-11 16:49:07 +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
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
}
2021-11-11 21:36:51 +00:00
2022-11-27 14:38:40 +00:00
// #[test]
// fn components_generate() {
// fn app() -> Element {
2024-01-15 15:05:46 +00:00
// let render_phase = use_hook(|| 0);
2022-11-27 14:38:40 +00:00
// *render_phase += 1;
2024-01-11 01:21:15 +00:00
// match *render_phase {
2022-11-27 14:38:40 +00:00
// 1 => rsx_without_templates!("Text0"),
// 2 => rsx_without_templates!(div {}),
// 3 => rsx_without_templates!("Text2"),
// 4 => rsx_without_templates!(Child {}),
// 5 => rsx_without_templates!({ None as Option<()> }),
// 6 => rsx_without_templates!("text 3"),
// 7 => rsx_without_templates!({ (0..2).map(|f| rsx_without_templates!("text {f}")) }),
// 8 => rsx_without_templates!(Child {}),
// _ => todo!(),
// })
// };
2024-01-11 17:11:44 +00:00
// fn Child() -> Element {
2022-11-27 14:38:40 +00:00
// println!("Running child");
2024-01-11 01:21:15 +00:00
// render_without_templates! {
2022-11-27 14:38:40 +00:00
// h1 {}
// })
// }
// let mut dom = VirtualDom::new(app);
2024-01-11 01:21:15 +00:00
// let edits = dom.rebuild_to_vec();
2022-11-27 14:38:40 +00:00
// assert_eq!(
// edits.edits,
// [
// CreateTextNode { root: Some(1), text: "Text0" },
// AppendChildren { root: Some(0), children: vec![1] }
// ]
// );
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateElement { root: Some(2), tag: "div", children: 0 },
// ReplaceWith { root: Some(1), nodes: vec![2] }
// ]
// );
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateTextNode { root: Some(1), text: "Text2" },
// ReplaceWith { root: Some(2), nodes: vec![1] }
// ]
// );
// // child {}
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateElement { root: Some(2), tag: "h1", children: 0 },
// ReplaceWith { root: Some(1), nodes: vec![2] }
// ]
// );
// // placeholder
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreatePlaceholder { root: Some(1) },
// ReplaceWith { root: Some(2), nodes: vec![1] }
// ]
// );
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateTextNode { root: Some(2), text: "text 3" },
// ReplaceWith { root: Some(1), nodes: vec![2] }
// ]
// );
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateTextNode { text: "text 0", root: Some(1) },
// CreateTextNode { text: "text 1", root: Some(3) },
// ReplaceWith { root: Some(2), nodes: vec![1, 3] },
// ]
// );
// assert_eq!(
// dom.hard_diff(ScopeId::ROOT).edits,
2022-11-27 14:38:40 +00:00
// [
// CreateElement { tag: "h1", root: Some(2), children: 0 },
// ReplaceWith { root: Some(1), nodes: vec![2] },
// Remove { root: Some(3) },
// ]
// );
// }