dioxus/packages/native-core/tests/change_nodes.rs

154 lines
3.6 KiB
Rust
Raw Normal View History

2022-03-27 01:10:15 +00:00
use dioxus_core::VNode;
use dioxus_core::*;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
2022-03-31 01:45:41 +00:00
use dioxus_native_core::real_dom::RealDom;
2022-03-27 01:10:15 +00:00
use std::cell::Cell;
#[test]
2022-04-04 17:19:21 +00:00
fn remove_node() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{
div{}
}
});
2022-03-31 01:45:41 +00:00
let mut tree: RealDom<(), ()> = RealDom::new();
2022-03-27 01:10:15 +00:00
let _to_update = tree.apply_mutations(vec![mutations]);
let child_div = VElement {
id: Cell::new(Some(ElementId(2))),
key: None,
tag: "div",
namespace: None,
parent: Cell::new(Some(ElementId(1))),
listeners: &[],
attributes: &[],
children: &[],
};
let child_div_el = VNode::Element(&child_div);
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_el],
};
assert_eq!(tree.size(), 2);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
rsx! {
div{
div{}
}
},
rsx! {
div{}
},
);
tree.apply_mutations(vec![mutations.1]);
let new_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!(tree.size(), 1);
assert!(&tree.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(tree[1].height, 1);
}
#[test]
2022-04-04 17:19:21 +00:00
fn add_node() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{}
});
2022-03-31 01:45:41 +00:00
let mut tree: RealDom<(), ()> = RealDom::new();
2022-03-27 01:10:15 +00:00
let _to_update = tree.apply_mutations(vec![mutations]);
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!(tree.size(), 1);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
rsx! {
div{}
},
rsx! {
div{
p{}
}
},
);
tree.apply_mutations(vec![mutations.1]);
let child_div = VElement {
id: Cell::new(Some(ElementId(2))),
key: None,
tag: "p",
namespace: None,
parent: Cell::new(Some(ElementId(1))),
listeners: &[],
attributes: &[],
children: &[],
};
let child_div_el = VNode::Element(&child_div);
let new_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_el],
};
assert_eq!(tree.size(), 2);
assert!(&tree.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
}