dioxus/packages/native-core-macro/tests/initial_build.rs

129 lines
3.4 KiB
Rust
Raw Normal View History

2022-03-27 01:10:15 +00:00
use std::cell::Cell;
2022-06-25 13:27:10 +00:00
use dioxus::core as dioxus_core;
use dioxus::core::{ElementId, VElement, VText};
use dioxus::prelude::*;
2022-03-31 01:45:41 +00:00
use dioxus_native_core::real_dom::RealDom;
2022-04-12 23:46:16 +00:00
use dioxus_native_core::state::State;
2022-04-12 12:51:57 +00:00
use dioxus_native_core_macro::State;
2022-04-16 17:23:31 +00:00
#[derive(Default, Clone, State)]
2022-04-12 12:51:57 +00:00
struct Empty {}
2022-03-27 01:10:15 +00:00
#[test]
2022-04-04 17:19:21 +00:00
fn initial_build_simple() {
2022-03-27 01:10:15 +00:00
use std::cell::Cell;
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
render!(div {})
2022-03-27 01:10:15 +00:00
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{}
});
2022-04-12 12:51:57 +00:00
let mut dom: RealDom<Empty> = RealDom::new();
2022-03-27 01:10:15 +00:00
let _to_update = dom.apply_mutations(vec![mutations]);
2022-03-27 01:10:15 +00:00
let root_div = VElement {
id: Cell::new(Some(ElementId(1))),
key: None,
tag: "div",
namespace: None,
parent: Cell::new(Some(ElementId(0))),
listeners: &[],
attributes: &[],
children: &[],
};
assert_eq!(dom.size(), 1);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[ElementId(1)].height, 1);
2022-03-27 01:10:15 +00:00
}
#[test]
2022-04-04 17:19:21 +00:00
fn initial_build_with_children() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
render!(div {})
2022-03-27 01:10:15 +00:00
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{
div{
"hello"
p{
"world"
}
"hello world"
}
}
});
2022-04-12 12:51:57 +00:00
let mut dom: RealDom<Empty> = RealDom::new();
2022-03-27 01:10:15 +00:00
let _to_update = dom.apply_mutations(vec![mutations]);
2022-03-27 01:10:15 +00:00
let first_text = VText {
id: Cell::new(Some(ElementId(3))),
text: "hello",
is_static: true,
};
let first_text_node = VNode::Text(&first_text);
let child_text = VText {
id: Cell::new(Some(ElementId(5))),
text: "world",
is_static: true,
};
let child_text_node = VNode::Text(&child_text);
let child_p_el = VElement {
id: Cell::new(Some(ElementId(4))),
key: None,
tag: "p",
namespace: None,
parent: Cell::new(Some(ElementId(2))),
listeners: &[],
attributes: &[],
children: &[child_text_node],
};
let child_p_node = VNode::Element(&child_p_el);
let second_text = VText {
id: Cell::new(Some(ElementId(6))),
text: "hello world",
is_static: true,
};
let second_text_node = VNode::Text(&second_text);
let child_div_el = VElement {
id: Cell::new(Some(ElementId(2))),
key: None,
tag: "div",
namespace: None,
parent: Cell::new(Some(ElementId(1))),
listeners: &[],
attributes: &[],
children: &[first_text_node, child_p_node, second_text_node],
};
let child_div_node = VNode::Element(&child_div_el);
let root_div = VElement {
id: Cell::new(Some(ElementId(1))),
key: None,
tag: "div",
namespace: None,
parent: Cell::new(Some(ElementId(0))),
listeners: &[],
attributes: &[],
children: &[child_div_node],
};
assert_eq!(dom.size(), 6);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[ElementId(1)].height, 1);
assert_eq!(dom[ElementId(2)].height, 2);
assert_eq!(dom[ElementId(3)].height, 3);
assert_eq!(dom[ElementId(4)].height, 3);
assert_eq!(dom[ElementId(5)].height, 4);
assert_eq!(dom[ElementId(6)].height, 3);
2022-03-27 01:10:15 +00:00
}