use dioxus::prelude::*;
#[test]
fn root_ids() {
fn app() -> Element {
rsx! { div { width: "100px" } }
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"
"#
);
}
#[test]
fn dynamic_attributes() {
fn app() -> Element {
let dynamic = 123;
rsx! {
div { width: "100px", div { width: "{dynamic}px" } }
}
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#""#
);
}
#[test]
fn listeners() {
fn app() -> Element {
rsx! {
div { width: "100px", div { onclick: |_| {} } }
}
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#""#
);
fn app2() -> Element {
let dynamic = 123;
rsx! {
div { width: "100px", div { width: "{dynamic}px", onclick: |_| {} } }
}
}
let mut dom = VirtualDom::new(app2);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#""#
);
}
#[test]
fn text_nodes() {
fn app() -> Element {
let dynamic_text = "hello";
rsx! {
div { {dynamic_text} }
}
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"hello
"#
);
fn app2() -> Element {
let dynamic = 123;
rsx! {
div { "{dynamic}", "{1234}" }
}
}
let mut dom = VirtualDom::new(app2);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"1231234
"#
);
}
#[allow(non_snake_case)]
#[test]
fn components_hydrate() {
fn app() -> Element {
rsx! { Child {} }
}
fn Child() -> Element {
rsx! { div { "hello" } }
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"hello
"#
);
fn app2() -> Element {
rsx! { Child2 {} }
}
fn Child2() -> Element {
let dyn_text = "hello";
rsx! {
div { {dyn_text} }
}
}
let mut dom = VirtualDom::new(app2);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"hello
"#
);
fn app3() -> Element {
rsx! { Child3 {} }
}
fn Child3() -> Element {
rsx! { div { width: "{1}" } }
}
let mut dom = VirtualDom::new(app3);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#""#
);
fn app4() -> Element {
rsx! { Child4 {} }
}
fn Child4() -> Element {
rsx! {
for _ in 0..2 {
{rsx! { "{1}" }}
}
}
}
let mut dom = VirtualDom::new(app4);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"11"#
);
}
#[test]
fn hello_world_hydrates() {
use dioxus::hooks::use_signal;
fn app() -> Element {
let mut count = use_signal(|| 0);
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
}
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
assert_eq!(
dioxus_ssr::pre_render(&dom),
r#"High-Five counter: 0
"#
);
}