dioxus/packages/core/tests/create_dom.rs

187 lines
4.7 KiB
Rust
Raw Normal View History

2022-11-24 11:47:14 +00:00
#![allow(unused, non_upper_case_globals, non_snake_case)]
//! Prove that the dom works normally through virtualdom methods.
//!
2024-01-11 01:21:15 +00:00
//! This methods all use "rebuild_to_vec" which completely bypasses the scheduler.
//! Hard rebuild_to_vecs don't consume any events from the event queue.
2022-11-24 11:47:14 +00:00
use dioxus::core::Mutation::*;
use dioxus::prelude::*;
use dioxus_core::ElementId;
#[test]
fn test_original_diff() {
2024-01-11 18:40:36 +00:00
let mut dom = VirtualDom::new(|| {
2024-01-11 01:21:15 +00:00
render! {
div { div { "Hello, world!" } }
}
2022-11-24 11:47:14 +00:00
});
2024-01-11 01:21:15 +00:00
let edits = dom.rebuild_to_vec().santize();
2022-11-24 11:47:14 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
edits.edits,
2022-11-24 11:47:14 +00:00
[
// add to root
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
2022-12-03 00:24:49 +00:00
AppendChildren { m: 1, id: ElementId(0) }
2022-11-24 11:47:14 +00:00
]
)
}
#[test]
fn create() {
2024-01-11 18:40:36 +00:00
let mut dom = VirtualDom::new(|| {
2024-01-11 01:21:15 +00:00
render! {
2022-11-24 11:47:14 +00:00
div {
div {
"Hello, world!"
div {
div {
2024-01-11 01:21:15 +00:00
Fragment { "hello""world" }
2022-11-24 11:47:14 +00:00
}
}
}
}
2024-01-11 01:21:15 +00:00
}
2022-11-24 11:47:14 +00:00
});
2024-01-11 01:21:15 +00:00
let _edits = dom.rebuild_to_vec().santize();
2022-12-03 00:24:49 +00:00
// todo: we don't test template mutations anymore since the templates are passed along
// assert_eq!(
// edits.templates,
// [
// // create template
// CreateElement { name: "div" },
// CreateElement { name: "div" },
// CreateStaticText { value: "Hello, world!" },
// CreateElement { name: "div" },
// CreateElement { name: "div" },
// CreateStaticPlaceholder {},
// AppendChildren { m: 1 },
// AppendChildren { m: 1 },
// AppendChildren { m: 2 },
// AppendChildren { m: 1 },
// SaveTemplate { name: "template", m: 1 },
// // The fragment child template
// CreateStaticText { value: "hello" },
// CreateStaticText { value: "world" },
// SaveTemplate { name: "template", m: 2 },
// ]
// );
2022-11-24 11:47:14 +00:00
}
#[test]
fn create_list() {
2024-01-11 21:18:11 +00:00
let mut dom = VirtualDom::new(|| render! {(0..3).map(|f| render!( div { "hello" } ))});
2022-11-24 11:47:14 +00:00
2024-01-11 01:21:15 +00:00
let _edits = dom.rebuild_to_vec().santize();
2022-12-03 00:24:49 +00:00
// note: we dont test template edits anymore
// assert_eq!(
// edits.templates,
// [
// // create template
// CreateElement { name: "div" },
// CreateStaticText { value: "hello" },
// AppendChildren { m: 1 },
// SaveTemplate { name: "template", m: 1 }
// ]
// );
2022-11-24 11:47:14 +00:00
}
#[test]
fn create_simple() {
2024-01-11 18:40:36 +00:00
let mut dom = VirtualDom::new(|| {
2024-01-11 01:21:15 +00:00
render! {
2022-11-24 11:47:14 +00:00
div {}
div {}
div {}
div {}
2024-01-11 01:21:15 +00:00
}
2022-11-24 11:47:14 +00:00
});
2024-01-11 01:21:15 +00:00
let edits = dom.rebuild_to_vec().santize();
2022-12-03 00:24:49 +00:00
// note: we dont test template edits anymore
// assert_eq!(
// edits.templates,
// [
// // create template
// CreateElement { name: "div" },
// CreateElement { name: "div" },
// CreateElement { name: "div" },
// CreateElement { name: "div" },
// // add to root
// SaveTemplate { name: "template", m: 4 }
// ]
// );
2022-11-24 11:47:14 +00:00
}
#[test]
fn create_components() {
2024-01-11 18:40:36 +00:00
let mut dom = VirtualDom::new(|| {
2024-01-11 01:21:15 +00:00
render! {
2022-11-24 11:47:14 +00:00
Child { "abc1" }
Child { "abc2" }
Child { "abc3" }
2024-01-11 01:21:15 +00:00
}
2022-11-24 11:47:14 +00:00
});
2024-01-11 21:18:11 +00:00
#[derive(Props, Clone, PartialEq)]
struct ChildProps {
2024-01-05 14:39:22 +00:00
children: Element,
2022-11-24 11:47:14 +00:00
}
2024-01-11 21:18:11 +00:00
fn Child(cx: ChildProps) -> Element {
2024-01-11 01:21:15 +00:00
render! {
2022-11-24 11:47:14 +00:00
h1 {}
2024-01-11 17:11:44 +00:00
div { &cx.children }
2022-11-24 11:47:14 +00:00
p {}
2024-01-11 01:21:15 +00:00
}
2022-11-24 11:47:14 +00:00
}
2024-01-11 01:21:15 +00:00
let _edits = dom.rebuild_to_vec().santize();
2022-12-03 00:24:49 +00:00
// todo: test this
2022-11-24 11:47:14 +00:00
}
#[test]
fn anchors() {
2024-01-11 18:40:36 +00:00
let mut dom = VirtualDom::new(|| {
2024-01-11 01:21:15 +00:00
render! {
2022-11-24 11:47:14 +00:00
if true {
2024-01-11 01:21:15 +00:00
render!( div { "hello" } )
2022-11-24 11:47:14 +00:00
}
if false {
2024-01-11 01:21:15 +00:00
render!( div { "goodbye" } )
2022-11-24 11:47:14 +00:00
}
2024-01-11 01:21:15 +00:00
}
2022-11-24 11:47:14 +00:00
});
// note that the template under "false" doesn't show up since it's not loaded
2024-01-11 01:21:15 +00:00
let edits = dom.rebuild_to_vec().santize();
2022-12-03 00:24:49 +00:00
// note: we dont test template edits anymore
// assert_eq!(
// edits.templates,
// [
// // create each template
// CreateElement { name: "div" },
// CreateStaticText { value: "hello" },
// AppendChildren { m: 1 },
// SaveTemplate { m: 1, name: "template" },
// ]
// );
2022-11-24 11:47:14 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
edits.edits,
2022-11-24 11:47:14 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
CreatePlaceholder { id: ElementId(2) },
2022-12-03 00:24:49 +00:00
AppendChildren { m: 2, id: ElementId(0) }
2022-11-24 11:47:14 +00:00
]
)
}