dioxus/packages/core/tests/borrowedstate.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

2022-11-24 11:47:14 +00:00
#![allow(non_snake_case)]
use dioxus::core::{ElementId, Mutation::*};
use dioxus::prelude::*;
#[test]
fn test_borrowed_state() {
let mut dom = VirtualDom::new(Parent);
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.rebuild().santize().edits,
2022-11-24 11:47:14 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
LoadTemplate { name: "template", index: 0, id: ElementId(3,) },
HydrateText { path: &[0,], value: "Hello w1!", id: ElementId(4,) },
ReplacePlaceholder { path: &[1,], m: 1 },
ReplacePlaceholder { path: &[0,], m: 1 },
2022-12-03 00:24:49 +00:00
AppendChildren { m: 1, id: ElementId(0) },
2022-11-24 11:47:14 +00:00
]
2022-12-09 22:18:37 +00:00
);
2022-11-24 11:47:14 +00:00
}
fn Parent(cx: Scope) -> Element {
let w1 = cx.use_hook(|| String::from("w1"));
cx.render(rsx! {
div {
Child { name: w1 }
}
})
}
#[derive(Props)]
struct ChildProps<'a> {
name: &'a str,
}
fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
cx.render(rsx! {
div {
h1 { "it's nested" }
Child2 { name: cx.props.name }
}
})
}
#[derive(Props)]
struct Grandchild<'a> {
name: &'a str,
}
fn Child2<'a>(cx: Scope<'a, Grandchild<'a>>) -> Element {
cx.render(rsx!(div { "Hello {cx.props.name}!" }))
}