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?
use dioxus::core::Mutation::*;
use dioxus::prelude::*;
use dioxus_core::ElementId;
#[test]
fn empty_fragment_creates_nothing() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
render!(())
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() {
let mut vdom = VirtualDom::new(|cx| {
2024-01-11 01:21:15 +00:00
render!(
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() {
let mut vdom = VirtualDom::new(|cx| {
2024-01-11 01:21:15 +00:00
render!(
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-11 01:21:15 +00:00
render! {
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-11 01:21:15 +00:00
render! {
2022-11-23 03:59:56 +00:00
div { "hello" }
div { "goodbye" }
2024-01-11 01:21:15 +00:00
render! {
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: 8 }
2022-11-23 03:59:56 +00:00
);
}
#[test]
fn fragments_across_components() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
render! {
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-11 01:21:15 +00:00
render! { "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-11 01:21:15 +00:00
render!(
h1 { "hello" }
(0..6).map(|f| render!( span { "{f}" }))
)
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
);
}