bugfixes, testing and refactoring

This commit is contained in:
Evan Almloff committed 2022-03-26 20:32:49 -05:00
1 parent 6adfa8805c
commit 7f4e257757
20 files changed
+3888 -739

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
use dioxus_core::VNode;
use dioxus_core::*;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
use dioxus_native_core::client_tree::ClientTree;
use std::cell::Cell;
#[test]
fn tree_remove_node() {
#[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{}
}
});
let mut tree: ClientTree<(), ()> = ClientTree::new();
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]
fn tree_add_node() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{}
});
let mut tree: ClientTree<(), ()> = ClientTree::new();
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);
}
+124
View File
@@ -0,0 +1,124 @@
use std::cell::Cell;
use dioxus_core::VNode;
use dioxus_core::*;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
use dioxus_native_core::client_tree::ClientTree;
#[test]
fn tree_initial_build_simple() {
use std::cell::Cell;
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div{}
});
let mut tree: ClientTree<(), ()> = ClientTree::new();
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);
}
#[test]
fn tree_initial_build_with_children() {
#[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{
"hello"
p{
"world"
}
"hello world"
}
}
});
let mut tree: ClientTree<(), ()> = ClientTree::new();
let _to_update = tree.apply_mutations(vec![mutations]);
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!(tree.size(), 6);
assert!(&tree.contains_node(&VNode::Element(&root_div)));
assert_eq!(tree[1].height, 1);
assert_eq!(tree[2].height, 2);
assert_eq!(tree[3].height, 3);
assert_eq!(tree[4].height, 3);
assert_eq!(tree[5].height, 4);
assert_eq!(tree[6].height, 3);
}
+320
View File
@@ -0,0 +1,320 @@
use dioxus_core::VNode;
use dioxus_core::*;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
use dioxus_native_core::client_tree::*;
#[derive(Debug, Clone, PartialEq, Default)]
struct CallCounter(u32);
impl BubbledUpState for CallCounter {
type Ctx = ();
fn reduce<'a, I>(&mut self, _children: I, _vnode: &VNode, _ctx: &mut Self::Ctx)
where
I: Iterator<Item = &'a Self>,
Self: 'a,
{
self.0 += 1;
}
}
impl PushedDownState for CallCounter {
type Ctx = ();
fn reduce(&mut self, _parent: Option<&Self>, _vnode: &VNode, _ctx: &mut Self::Ctx) {
self.0 += 1;
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct BubbledUpStateTester(String, Vec<Box<BubbledUpStateTester>>);
impl BubbledUpState for BubbledUpStateTester {
type Ctx = u32;
fn reduce<'a, I>(&mut self, children: I, vnode: &VNode, ctx: &mut Self::Ctx)
where
I: Iterator<Item = &'a Self>,
Self: 'a,
{
assert_eq!(*ctx, 42);
*self = BubbledUpStateTester(
vnode.mounted_id().to_string(),
children.map(|c| Box::new(c.clone())).collect(),
);
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct PushedDownStateTester(String, Option<Box<PushedDownStateTester>>);
impl PushedDownState for PushedDownStateTester {
type Ctx = u32;
fn reduce(&mut self, parent: Option<&Self>, vnode: &VNode, ctx: &mut Self::Ctx) {
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
vnode.mounted_id().to_string(),
parent.map(|c| Box::new(c.clone())),
);
}
}
#[test]
fn tree_state_initial() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {
p{}
h1{}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
p{}
h1{}
}
});
let mut tree: ClientTree<BubbledUpStateTester, PushedDownStateTester> = ClientTree::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut 42, &mut 42);
let root_div = &tree[1];
assert_eq!(root_div.up_state.0, "1");
assert_eq!(
root_div.up_state.1,
vec![
Box::new(BubbledUpStateTester("2".to_string(), Vec::new())),
Box::new(BubbledUpStateTester("3".to_string(), Vec::new()))
]
);
assert_eq!(root_div.down_state.0, "1");
assert_eq!(root_div.down_state.1, None);
let child_p = &tree[2];
assert_eq!(child_p.up_state.0, "2");
assert_eq!(child_p.up_state.1, Vec::new());
assert_eq!(child_p.down_state.0, "2");
assert_eq!(
child_p.down_state.1,
Some(Box::new(PushedDownStateTester("1".to_string(), None)))
);
let child_h1 = &tree[3];
assert_eq!(child_h1.up_state.0, "3");
assert_eq!(child_h1.up_state.1, Vec::new());
assert_eq!(child_h1.down_state.0, "3");
assert_eq!(
child_h1.down_state.1,
Some(Box::new(PushedDownStateTester("1".to_string(), None)))
);
}
#[test]
fn tree_state_reduce_initally_called_minimally() {
#[derive(Debug, Clone, PartialEq, Default)]
struct CallCounter(u32);
impl BubbledUpState for CallCounter {
type Ctx = ();
fn reduce<'a, I>(&mut self, _children: I, _vnode: &VNode, _ctx: &mut Self::Ctx)
where
I: Iterator<Item = &'a Self>,
Self: 'a,
{
self.0 += 1;
}
}
impl PushedDownState for CallCounter {
type Ctx = ();
fn reduce(&mut self, _parent: Option<&Self>, _vnode: &VNode, _ctx: &mut Self::Ctx) {
self.0 += 1;
}
}
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
}
});
let mut tree: ClientTree<CallCounter, CallCounter> = ClientTree::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse(|n| {
assert_eq!(n.up_state.0, 1);
assert_eq!(n.down_state.0, 1);
});
}
#[test]
fn tree_state_reduce_down_called_minimally_on_update() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
width: "100%",
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
}
});
let mut tree: ClientTree<CallCounter, CallCounter> = ClientTree::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = tree.apply_mutations(vec![Mutations {
edits: vec![DomEdit::SetAttribute {
root: 1,
field: "width",
value: "99%",
ns: Some("style"),
}],
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse(|n| {
assert_eq!(n.down_state.0, 2);
});
}
#[test]
fn tree_state_reduce_up_called_minimally_on_update() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
width: "100%",
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
}
});
let mut tree: ClientTree<CallCounter, CallCounter> = ClientTree::new();
let nodes_updated = tree.apply_mutations(vec![mutations]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
let nodes_updated = tree.apply_mutations(vec![Mutations {
edits: vec![DomEdit::SetAttribute {
root: 4,
field: "width",
value: "99%",
ns: Some("style"),
}],
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
let _to_rerender = tree.update_state(&vdom, nodes_updated, &mut (), &mut ());
tree.traverse(|n| {
assert_eq!(n.up_state.0, if n.id.0 > 4 { 1 } else { 2 });
});
}