dioxus/packages/ssr/tests/hydration.rs

198 lines
4.6 KiB
Rust
Raw Normal View History

use dioxus::prelude::*;
#[test]
fn root_ids() {
2024-01-15 17:12:24 +00:00
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { div { width: "100px" } }
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div style="width:100px;" data-node-hydration="0"></div>"#
);
}
#[test]
fn dynamic_attributes() {
2024-01-15 17:12:24 +00:00
fn app() -> Element {
let dynamic = 123;
2024-01-16 19:18:46 +00:00
rsx! {
div { width: "100px", div { width: "{dynamic}px" } }
}
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div style="width:100px;" data-node-hydration="0"><div style="width:123px;" data-node-hydration="1"></div></div>"#
);
}
#[test]
fn listeners() {
2024-01-15 17:12:24 +00:00
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
div { width: "100px", div { onclick: |_| {} } }
}
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div style="width:100px;" data-node-hydration="0"><div data-node-hydration="1,click:1"></div></div>"#
);
2024-01-15 17:12:24 +00:00
fn app2() -> Element {
let dynamic = 123;
2024-01-16 19:18:46 +00:00
rsx! {
div { width: "100px", div { width: "{dynamic}px", onclick: |_| {} } }
}
}
let mut dom = VirtualDom::new(app2);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div style="width:100px;" data-node-hydration="0"><div style="width:123px;" data-node-hydration="1,click:1"></div></div>"#
);
}
#[test]
fn text_nodes() {
2024-01-15 17:12:24 +00:00
fn app() -> Element {
let dynamic_text = "hello";
2024-01-16 19:18:46 +00:00
rsx! {
div { {dynamic_text} }
}
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div data-node-hydration="0"><!--node-id1-->hello<!--#--></div>"#
);
2024-01-15 17:12:24 +00:00
fn app2() -> Element {
let dynamic = 123;
2024-01-16 19:18:46 +00:00
rsx! {
div { "{dynamic}", "{1234}" }
}
}
let mut dom = VirtualDom::new(app2);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<div data-node-hydration="0"><!--node-id1-->123<!--#--><!--node-id2-->1234<!--#--></div>"#
);
}
2023-12-27 16:23:56 +00:00
#[allow(non_snake_case)]
2023-12-27 16:17:18 +00:00
#[test]
fn components_hydrate() {
2024-01-15 17:12:24 +00:00
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { Child {} }
2023-12-27 16:17:18 +00:00
}
2024-01-15 17:12:24 +00:00
fn Child() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { div { "hello" } }
2023-12-27 16:17:18 +00:00
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2023-12-27 16:17:18 +00:00
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"<div data-node-hydration="0">hello</div>"#
);
2024-01-15 17:12:24 +00:00
fn app2() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { Child2 {} }
2023-12-27 16:17:18 +00:00
}
2024-01-15 17:12:24 +00:00
fn Child2() -> Element {
2023-12-27 16:17:18 +00:00
let dyn_text = "hello";
2024-01-16 19:18:46 +00:00
rsx! {
div { {dyn_text} }
2023-12-27 16:17:18 +00:00
}
}
let mut dom = VirtualDom::new(app2);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2023-12-27 16:17:18 +00:00
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"<div data-node-hydration="0"><!--node-id1-->hello<!--#--></div>"#
);
2024-01-15 17:12:24 +00:00
fn app3() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { Child3 {} }
2023-12-27 16:17:18 +00:00
}
2024-01-15 17:12:24 +00:00
fn Child3() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { div { width: "{1}" } }
2023-12-27 16:17:18 +00:00
}
let mut dom = VirtualDom::new(app3);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2023-12-27 16:17:18 +00:00
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"<div style="width:1;" data-node-hydration="0"></div>"#
);
2024-01-15 17:12:24 +00:00
fn app4() -> Element {
2024-01-16 19:18:46 +00:00
rsx! { Child4 {} }
2023-12-27 16:17:18 +00:00
}
2024-01-15 17:12:24 +00:00
fn Child4() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2023-12-27 16:17:18 +00:00
for _ in 0..2 {
2024-01-16 19:18:46 +00:00
{rsx! { "{1}" }}
2023-12-27 16:17:18 +00:00
}
}
}
let mut dom = VirtualDom::new(app4);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2023-12-27 16:17:18 +00:00
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"<!--node-id0-->1<!--#--><!--node-id1-->1<!--#-->"#
);
}
#[test]
fn hello_world_hydrates() {
use dioxus::hooks::use_signal;
2024-01-15 17:12:24 +00:00
fn app() -> Element {
2024-01-10 23:57:15 +00:00
let mut count = use_signal(|| 0);
2024-01-16 19:18:46 +00:00
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
2024-01-10 23:57:15 +00:00
}
}
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
2023-12-27 16:17:18 +00:00
r#"<h1 data-node-hydration="0"><!--node-id1-->High-Five counter: 0<!--#--></h1><button data-node-hydration="2,click:1">Up high!</button><button data-node-hydration="3,click:1">Down low!</button>"#
);
}