dioxus/packages/core/tests/lifecycle.rs

169 lines
4.6 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::*;
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() {
struct AppProps {
value: Shared<&'static str>,
}
2022-11-27 14:38:40 +00:00
fn app(cx: Scope<AppProps>) -> Element {
2021-12-15 02:46:19 +00:00
let val = cx.props.value.lock().unwrap();
2022-11-27 14:38:40 +00:00
cx.render(rsx! { 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() });
let _ = dom.rebuild();
2021-10-01 06:07:12 +00:00
*value.lock().unwrap() = "goodbye";
2022-11-27 14:38:40 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.rebuild().santize().edits,
2022-11-27 14:38:40 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(3) },
HydrateText { path: &[0], value: "goodbye", 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() {
fn app(cx: Scope) -> Element {
let count = cx.use_hook(|| 0);
2021-11-11 16:49:07 +00:00
2022-11-27 14:38:40 +00:00
match *count {
0 => cx.render(rsx! {
2022-11-29 21:31:04 +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
}
2022-11-27 14:38:40 +00:00
}),
_ => cx.render(rsx!(())),
}
2021-11-11 16:49:07 +00:00
};
let mut dom = VirtualDom::new(app);
2022-11-27 14:38:40 +00:00
_ = dom.rebuild();
2022-11-29 21:31:04 +00:00
dom.handle_event("click", Rc::new(MouseData::default()), ElementId(1), true);
2021-11-11 16:49:07 +00:00
dom.mark_dirty(ScopeId::ROOT);
2022-11-27 14:38:40 +00:00
let edits = dom.render_immediate();
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(cx: Scope) -> Element {
// let render_phase = cx.use_hook(|| 0);
// *render_phase += 1;
// cx.render(match *render_phase {
// 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!(),
// })
// };
// fn Child(cx: Scope) -> Element {
// println!("Running child");
// cx.render(rsx_without_templates! {
// h1 {}
// })
// }
// let mut dom = VirtualDom::new(app);
// let edits = dom.rebuild();
// 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) },
// ]
// );
// }