dioxus/packages/ssr/tests/simple.rs

77 lines
1.6 KiB
Rust
Raw Normal View History

2022-11-16 00:05:22 +00:00
use dioxus::prelude::*;
#[test]
fn simple() {
fn app(cx: Scope) -> Element {
render! { div { "hello!" } }
}
let mut dom = VirtualDom::new(app);
2022-12-07 01:41:47 +00:00
_ = dom.rebuild();
assert_eq!(dioxus_ssr::render(&dom), "<div>hello!</div>");
2022-11-16 00:05:22 +00:00
assert_eq!(
2022-12-07 01:41:47 +00:00
dioxus_ssr::render_lazy(rsx!( div {"hello!"} )),
2022-11-16 00:05:22 +00:00
"<div>hello!</div>"
);
}
#[test]
fn lists() {
2022-12-07 01:41:47 +00:00
assert_eq!(
dioxus_ssr::render_lazy(rsx! {
2022-11-16 00:05:22 +00:00
ul {
(0..5).map(|i| rsx! {
li { "item {i}" }
})
}
2022-12-07 01:41:47 +00:00
}),
2022-11-16 00:05:22 +00:00
"<ul><li>item 0</li><li>item 1</li><li>item 2</li><li>item 3</li><li>item 4</li></ul>"
);
}
#[test]
fn dynamic() {
2022-12-07 01:41:47 +00:00
let dynamic = 123;
2022-11-16 00:05:22 +00:00
assert_eq!(
2022-12-07 01:41:47 +00:00
dioxus_ssr::render_lazy(rsx! {
div { "Hello world 1 -->" "{dynamic}" "<-- Hello world 2" }
}),
2022-11-16 00:05:22 +00:00
"<div>Hello world 1 -->123<-- Hello world 2</div>"
);
}
#[test]
fn components() {
#[inline_props]
fn my_component(cx: Scope, name: i32) -> Element {
render! {
div { "component {name}" }
}
}
assert_eq!(
2022-12-07 01:41:47 +00:00
dioxus_ssr::render_lazy(rsx! {
div {
(0..5).map(|name| rsx! {
my_component { name: name }
})
}
}),
2022-11-16 00:05:22 +00:00
"<div><div>component 0</div><div>component 1</div><div>component 2</div><div>component 3</div><div>component 4</div></div>"
);
}
2022-12-07 01:41:47 +00:00
#[test]
fn fragments() {
assert_eq!(
dioxus_ssr::render_lazy(rsx! {
div {
(0..5).map(|_| rsx! (()))
}
}),
"<div></div>"
);
}