dioxus/packages/core/tests/create_dom.rs

198 lines
4.9 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.
//!
//! This methods all use "rebuild" which completely bypasses the scheduler.
//! Hard rebuilds don't consume any events from the event queue.
use dioxus::core::Mutation::*;
use dioxus::prelude::*;
use dioxus_core::ElementId;
#[test]
fn test_original_diff() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {
div {
"Hello, world!"
}
}
})
});
let edits = dom.rebuild().santize();
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() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {
div {
"Hello, world!"
div {
div {
Fragment {
"hello"
"world"
}
}
}
}
}
})
});
2022-12-03 00:24:49 +00:00
let _edits = dom.rebuild().santize();
// 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() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
(0..3).map(|f| rsx!( div { "hello" } ))
})
});
2022-12-03 00:24:49 +00:00
let _edits = dom.rebuild().santize();
// 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() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {}
div {}
div {}
div {}
})
});
let edits = dom.rebuild().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() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
Child { "abc1" }
Child { "abc2" }
Child { "abc3" }
})
});
#[derive(Props)]
struct ChildProps<'a> {
children: Element<'a>,
}
fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
cx.render(rsx! {
h1 {}
div { &cx.props.children }
p {}
})
}
2022-12-03 00:24:49 +00:00
let _edits = dom.rebuild().santize();
// todo: test this
2022-11-24 11:47:14 +00:00
}
#[test]
fn anchors() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
if true {
rsx!( div { "hello" } )
}
if false {
rsx!( div { "goodbye" } )
}
})
});
// note that the template under "false" doesn't show up since it's not loaded
let edits = dom.rebuild().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
]
)
}