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

159 lines
3.7 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-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-03-27 01:10:15 +00:00
use std::cell::Cell;
2022-04-12 12:51:57 +00:00
#[derive(State, Default, Clone)]
struct Empty {}
2022-03-27 01:10:15 +00:00
#[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-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 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!(dom.size(), 2);
assert!(&dom.contains_node(&VNode::Element(&root_div)));
assert_eq!(dom[1].height, 1);
assert_eq!(dom[2].height, 2);
2022-03-27 01:10:15 +00:00
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
rsx! {
div{
div{}
}
},
rsx! {
div{}
},
);
dom.apply_mutations(vec![mutations.1]);
2022-03-27 01:10:15 +00:00
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!(dom.size(), 1);
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(dom[1].height, 1);
2022-03-27 01:10:15 +00:00
}
#[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-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[1].height, 1);
2022-03-27 01:10:15 +00:00
let vdom = VirtualDom::new(Base);
let mutations = vdom.diff_lazynodes(
rsx! {
div{}
},
rsx! {
div{
p{}
}
},
);
dom.apply_mutations(vec![mutations.1]);
2022-03-27 01:10:15 +00:00
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!(dom.size(), 2);
assert!(&dom.contains_node(&VNode::Element(&new_root_div)));
assert_eq!(dom[1].height, 1);
assert_eq!(dom[2].height, 2);
2022-03-27 01:10:15 +00:00
}