dioxus/packages/core/tests/create_fragments.rs

102 lines
2.2 KiB
Rust
Raw Normal View History

2022-11-23 03:59:56 +00:00
//! Do we create fragments properly across complex boundaries?
2024-01-16 01:14:11 +00:00
use dioxus::dioxus_core::Mutation::*;
2022-11-23 03:59:56 +00:00
use dioxus::prelude::*;
use dioxus_core::ElementId;
#[test]
fn empty_fragment_creates_nothing() {
fn app() -> Element {
2024-02-05 07:03:52 +00:00
rsx!({})
2022-11-23 03:59:56 +00:00
}
let mut vdom = VirtualDom::new(app);
2024-01-11 01:21:15 +00:00
let edits = vdom.rebuild_to_vec();
2022-11-23 03:59:56 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
edits.edits,
2022-11-23 03:59:56 +00:00
[
2022-11-24 14:11:27 +00:00
CreatePlaceholder { id: ElementId(1) },
2022-12-03 00:24:49 +00:00
AppendChildren { id: ElementId(0), m: 1 }
2022-11-23 03:59:56 +00:00
]
);
}
#[test]
fn root_fragments_work() {
2024-01-11 18:40:36 +00:00
let mut vdom = VirtualDom::new(|| {
2024-01-16 19:18:46 +00:00
rsx!(
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-11 01:21:15 +00:00
)
2022-11-23 03:59:56 +00:00
});
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.rebuild_to_vec().edits.last().unwrap(),
2022-12-03 00:24:49 +00:00
&AppendChildren { id: ElementId(0), m: 2 }
2022-11-23 03:59:56 +00:00
);
}
#[test]
fn fragments_nested() {
2024-01-11 18:40:36 +00:00
let mut vdom = VirtualDom::new(|| {
2024-01-16 19:18:46 +00:00
rsx!(
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-16 19:18:46 +00:00
{rsx! {
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-16 19:18:46 +00:00
{rsx! {
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-16 19:18:46 +00:00
{rsx! {
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
}}
}}
}}
2024-01-14 21:21:19 +00:00
)
2022-11-23 03:59:56 +00:00
});
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.rebuild_to_vec().edits.last().unwrap(),
2022-12-03 00:24:49 +00:00
&AppendChildren { id: ElementId(0), m: 8 }
2022-11-23 03:59:56 +00:00
);
}
#[test]
fn fragments_across_components() {
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2022-11-23 03:59:56 +00:00
demo_child {}
demo_child {}
demo_child {}
demo_child {}
2024-01-11 01:21:15 +00:00
}
2022-11-23 03:59:56 +00:00
}
2024-01-11 17:11:44 +00:00
fn demo_child() -> Element {
2022-11-23 03:59:56 +00:00
let world = "world";
2024-01-16 19:18:46 +00:00
rsx! { "hellO!", {world} }
2022-11-23 03:59:56 +00:00
}
assert_eq!(
2024-01-11 01:21:15 +00:00
VirtualDom::new(app).rebuild_to_vec().edits.last().unwrap(),
2022-12-03 00:24:49 +00:00
&AppendChildren { id: ElementId(0), m: 8 }
2022-11-23 03:59:56 +00:00
);
}
#[test]
fn list_fragments() {
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx!(
2024-01-11 01:21:15 +00:00
h1 { "hello" }
2024-01-16 19:18:46 +00:00
{(0..6).map(|f| rsx!( span { "{f}" }))}
2024-01-11 01:21:15 +00:00
)
2022-11-23 03:59:56 +00:00
}
assert_eq!(
2024-01-11 01:21:15 +00:00
VirtualDom::new(app).rebuild_to_vec().edits.last().unwrap(),
2022-12-03 00:24:49 +00:00
&AppendChildren { id: ElementId(0), m: 7 }
2022-11-23 03:59:56 +00:00
);
}