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

423 lines
12 KiB
Rust
Raw Normal View History

2022-06-25 13:27:10 +00:00
use dioxus::prelude::*;
2022-03-31 01:45:41 +00:00
use dioxus_native_core::real_dom::*;
2022-04-16 17:23:31 +00:00
use dioxus_native_core::state::{ChildDepState, NodeDepState, ParentDepState, State};
use dioxus_native_core::tree::TreeView;
use dioxus_native_core::{node_ref::*, NodeId, SendAnyMap};
2022-04-12 12:51:57 +00:00
use dioxus_native_core_macro::State;
#[derive(Debug, Clone, Default, State)]
struct CallCounterState {
2022-04-16 17:23:31 +00:00
#[child_dep_state(child_counter)]
2022-04-12 12:51:57 +00:00
child_counter: ChildDepCallCounter,
2022-04-16 17:23:31 +00:00
#[parent_dep_state(parent_counter)]
2022-04-12 12:51:57 +00:00
parent_counter: ParentDepCallCounter,
#[node_dep_state()]
node_counter: NodeDepCallCounter,
}
2022-03-27 01:10:15 +00:00
2022-04-12 12:51:57 +00:00
#[derive(Debug, Clone, Default)]
struct ChildDepCallCounter(u32);
impl ChildDepState for ChildDepCallCounter {
2022-03-27 01:10:15 +00:00
type Ctx = ();
type DepState = (Self,);
2022-04-12 12:51:57 +00:00
const NODE_MASK: NodeMask = NodeMask::ALL;
2022-04-12 23:46:16 +00:00
fn reduce<'a>(
2022-04-12 12:51:57 +00:00
&mut self,
_: NodeView,
_: impl Iterator<Item = (&'a Self,)>,
2022-04-12 23:46:16 +00:00
_: &Self::Ctx,
) -> bool
where
Self::DepState: 'a,
{
2022-03-27 01:10:15 +00:00
self.0 += 1;
2022-04-12 12:51:57 +00:00
true
2022-03-27 01:10:15 +00:00
}
}
2022-04-12 12:51:57 +00:00
#[derive(Debug, Clone, Default)]
struct ParentDepCallCounter(u32);
impl ParentDepState for ParentDepCallCounter {
2022-03-27 01:10:15 +00:00
type Ctx = ();
type DepState = (Self,);
2022-04-12 12:51:57 +00:00
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(&mut self, _node: NodeView, _parent: Option<(&Self,)>, _ctx: &Self::Ctx) -> bool {
2022-04-12 12:51:57 +00:00
self.0 += 1;
println!("ParentDepCallCounter::reduce on {:?}\n{}", _node, self.0);
2022-04-12 12:51:57 +00:00
true
}
}
2022-03-27 01:10:15 +00:00
2022-04-12 12:51:57 +00:00
#[derive(Debug, Clone, Default)]
struct NodeDepCallCounter(u32);
impl NodeDepState for NodeDepCallCounter {
2022-04-12 12:51:57 +00:00
type Ctx = ();
type DepState = ();
2022-04-12 12:51:57 +00:00
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(&mut self, _node: NodeView, _sibling: (), _ctx: &Self::Ctx) -> bool {
2022-03-27 01:10:15 +00:00
self.0 += 1;
2022-04-12 12:51:57 +00:00
true
2022-03-27 01:10:15 +00:00
}
}
#[allow(clippy::vec_box)]
2022-03-27 01:10:15 +00:00
#[derive(Debug, Clone, PartialEq, Default)]
2022-04-12 12:51:57 +00:00
struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
impl ChildDepState for BubbledUpStateTester {
2022-03-27 01:10:15 +00:00
type Ctx = u32;
type DepState = (Self,);
2022-04-16 22:22:03 +00:00
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
2022-04-12 23:46:16 +00:00
fn reduce<'a>(
&mut self,
node: NodeView,
children: impl Iterator<Item = (&'a Self,)>,
2022-04-12 23:46:16 +00:00
ctx: &Self::Ctx,
) -> bool
where
Self::DepState: 'a,
{
2022-03-27 01:10:15 +00:00
assert_eq!(*ctx, 42);
*self = BubbledUpStateTester(
2022-04-12 12:51:57 +00:00
node.tag().map(|s| s.to_string()),
children
.into_iter()
.map(|(c,)| Box::new(c.clone()))
.collect(),
2022-03-27 01:10:15 +00:00
);
2022-04-12 12:51:57 +00:00
true
2022-03-27 01:10:15 +00:00
}
}
#[derive(Debug, Clone, PartialEq, Default)]
2022-04-12 12:51:57 +00:00
struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>);
impl ParentDepState for PushedDownStateTester {
2022-03-27 01:10:15 +00:00
type Ctx = u32;
type DepState = (Self,);
2022-04-16 22:22:03 +00:00
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
fn reduce(&mut self, node: NodeView, parent: Option<(&Self,)>, ctx: &Self::Ctx) -> bool {
2022-03-27 01:10:15 +00:00
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
2022-04-12 12:51:57 +00:00
node.tag().map(|s| s.to_string()),
parent.map(|(c,)| Box::new(c.clone())),
2022-03-27 01:10:15 +00:00
);
2022-04-12 12:51:57 +00:00
true
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct NodeStateTester(Option<String>, Vec<(String, String)>);
impl NodeDepState for NodeStateTester {
2022-04-12 12:51:57 +00:00
type Ctx = u32;
type DepState = ();
2022-04-16 22:22:03 +00:00
const NODE_MASK: NodeMask = NodeMask::new_with_attrs(AttributeMask::All).with_tag();
fn reduce(&mut self, node: NodeView, _sibling: (), ctx: &Self::Ctx) -> bool {
2022-04-12 12:51:57 +00:00
assert_eq!(*ctx, 42);
*self = NodeStateTester(
node.tag().map(|s| s.to_string()),
node.attributes()
.map(|iter| {
iter.map(|a| {
(
a.attribute.name.to_string(),
2022-10-02 21:12:24 +00:00
a.value.as_text().unwrap().to_string(),
)
})
.collect()
})
.unwrap_or_default(),
2022-04-12 12:51:57 +00:00
);
true
2022-03-27 01:10:15 +00:00
}
}
2022-04-16 17:23:31 +00:00
#[derive(State, Clone, Default, Debug)]
struct StateTester {
#[child_dep_state(bubbled, u32)]
bubbled: BubbledUpStateTester,
#[parent_dep_state(pushed, u32)]
pushed: PushedDownStateTester,
#[node_dep_state(NONE, u32)]
node: NodeStateTester,
}
2022-03-27 01:10:15 +00:00
#[test]
2022-04-04 17:19:21 +00:00
fn state_initial() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
render! {
div {
p{
color: "red"
}
h1{}
}
}
2022-03-27 01:10:15 +00:00
}
let mut vdom = VirtualDom::new(Base);
2022-03-27 01:10:15 +00:00
let mutations = vdom.rebuild();
2022-03-27 01:10:15 +00:00
2022-04-12 12:51:57 +00:00
let mut dom: RealDom<StateTester> = RealDom::new();
2022-03-27 01:10:15 +00:00
let (nodes_updated, _) = dom.apply_mutations(mutations);
let mut ctx = SendAnyMap::new();
2022-04-12 12:51:57 +00:00
ctx.insert(42u32);
let _to_rerender = dom.update_state(nodes_updated, ctx);
2022-03-27 01:10:15 +00:00
let root_div_id = dom.children_ids(NodeId(0)).unwrap()[0];
let root_div = &dom.get(root_div_id).unwrap();
2022-04-12 12:51:57 +00:00
assert_eq!(root_div.state.bubbled.0, Some("div".to_string()));
2022-03-27 01:10:15 +00:00
assert_eq!(
2022-04-12 12:51:57 +00:00
root_div.state.bubbled.1,
2022-03-27 01:10:15 +00:00
vec![
2022-04-12 12:51:57 +00:00
Box::new(BubbledUpStateTester(Some("p".to_string()), Vec::new())),
Box::new(BubbledUpStateTester(Some("h1".to_string()), Vec::new()))
2022-03-27 01:10:15 +00:00
]
);
2022-04-12 12:51:57 +00:00
assert_eq!(root_div.state.pushed.0, Some("div".to_string()));
2022-04-16 17:23:31 +00:00
assert_eq!(
root_div.state.pushed.1,
Some(Box::new(PushedDownStateTester(
Some("Root".to_string()),
None
)))
2022-04-16 17:23:31 +00:00
);
2022-04-12 12:51:57 +00:00
assert_eq!(root_div.state.node.0, Some("div".to_string()));
assert_eq!(root_div.state.node.1, vec![]);
2022-03-27 01:10:15 +00:00
let child_p_id = dom.children_ids(root_div_id).unwrap()[0];
let child_p = &dom[child_p_id];
2022-04-12 12:51:57 +00:00
assert_eq!(child_p.state.bubbled.0, Some("p".to_string()));
assert_eq!(child_p.state.bubbled.1, Vec::new());
assert_eq!(child_p.state.pushed.0, Some("p".to_string()));
2022-03-27 01:10:15 +00:00
assert_eq!(
2022-04-12 12:51:57 +00:00
child_p.state.pushed.1,
Some(Box::new(PushedDownStateTester(
Some("div".to_string()),
Some(Box::new(PushedDownStateTester(
Some("Root".to_string()),
None
)))
2022-04-12 12:51:57 +00:00
)))
);
assert_eq!(child_p.state.node.0, Some("p".to_string()));
assert_eq!(
child_p.state.node.1,
vec![("color".to_string(), "red".to_string())]
2022-03-27 01:10:15 +00:00
);
let child_h1_id = dom.children_ids(root_div_id).unwrap()[1];
let child_h1 = &dom[child_h1_id];
2022-04-12 12:51:57 +00:00
assert_eq!(child_h1.state.bubbled.0, Some("h1".to_string()));
assert_eq!(child_h1.state.bubbled.1, Vec::new());
assert_eq!(child_h1.state.pushed.0, Some("h1".to_string()));
2022-03-27 01:10:15 +00:00
assert_eq!(
2022-04-12 12:51:57 +00:00
child_h1.state.pushed.1,
Some(Box::new(PushedDownStateTester(
Some("div".to_string()),
Some(Box::new(PushedDownStateTester(
Some("Root".to_string()),
None
)))
2022-04-12 12:51:57 +00:00
)))
2022-03-27 01:10:15 +00:00
);
2022-04-12 12:51:57 +00:00
assert_eq!(child_h1.state.node.0, Some("h1".to_string()));
assert_eq!(child_h1.state.node.1, vec![]);
2022-03-27 01:10:15 +00:00
}
#[test]
2022-04-16 17:23:31 +00:00
fn state_reduce_parent_called_minimally_on_update() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
let width = if cx.generation() == 0 { "100%" } else { "99%" };
cx.render(rsx! {
div {
width: "{width}",
2022-03-27 01:10:15 +00:00
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
2022-03-27 01:10:15 +00:00
}
}
})
}
let mut vdom = VirtualDom::new(Base);
2022-03-27 01:10:15 +00:00
2022-04-12 12:51:57 +00:00
let mut dom: RealDom<CallCounterState> = RealDom::new();
2022-03-27 01:10:15 +00:00
let (nodes_updated, _) = dom.apply_mutations(vdom.rebuild());
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
vdom.mark_dirty(ScopeId(0));
let (nodes_updated, _) = dom.apply_mutations(vdom.render_immediate());
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
2022-03-27 01:10:15 +00:00
let mut is_root = true;
dom.traverse_depth_first(|n| {
if is_root {
is_root = false;
assert_eq!(n.state.parent_counter.0, 1);
} else {
assert_eq!(n.state.parent_counter.0, 2);
}
2022-03-27 01:10:15 +00:00
});
}
#[test]
2022-04-16 17:23:31 +00:00
fn state_reduce_child_called_minimally_on_update() {
2022-03-27 01:10:15 +00:00
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
let width = if cx.generation() == 0 { "100%" } else { "99%" };
cx.render(rsx! {
// updated: 2
div {
// updated: 2
2022-03-27 01:10:15 +00:00
div{
// updated: 2
div{
// updated: 2
p{
width: "{width}",
}
}
// updated: 1
2022-04-16 17:23:31 +00:00
p{
// updated: 1
"hello"
2022-04-16 17:23:31 +00:00
}
// updated: 1
div{
// updated: 1
h1{}
}
// updated: 1
2022-04-16 17:23:31 +00:00
p{
// updated: 1
"world"
2022-04-16 17:23:31 +00:00
}
2022-03-27 01:10:15 +00:00
}
}
})
}
let mut vdom = VirtualDom::new(Base);
2022-03-27 01:10:15 +00:00
2022-04-12 12:51:57 +00:00
let mut dom: RealDom<CallCounterState> = RealDom::new();
2022-03-27 01:10:15 +00:00
let (nodes_updated, _) = dom.apply_mutations(vdom.rebuild());
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
vdom.mark_dirty(ScopeId(0));
let (nodes_updated, _) = dom.apply_mutations(vdom.render_immediate());
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
2022-03-27 01:10:15 +00:00
let mut traverse_count = 0;
dom.traverse_depth_first(|n| {
assert_eq!(n.state.child_counter.0, {
if traverse_count > 4 {
1
} else {
2
}
});
traverse_count += 1;
2022-03-27 01:10:15 +00:00
});
}
2022-04-16 17:23:31 +00:00
#[derive(Debug, Clone, Default, State)]
struct UnorderedDependanciesState {
#[node_dep_state(c)]
b: BDepCallCounter,
#[node_dep_state()]
c: CDepCallCounter,
#[node_dep_state(b)]
a: ADepCallCounter,
}
#[derive(Debug, Clone, Default, PartialEq)]
struct ADepCallCounter(usize, BDepCallCounter);
impl NodeDepState for ADepCallCounter {
2022-04-16 17:23:31 +00:00
type Ctx = ();
type DepState = (BDepCallCounter,);
2022-04-16 17:23:31 +00:00
const NODE_MASK: NodeMask = NodeMask::NONE;
fn reduce(
&mut self,
_node: NodeView,
(sibling,): (&BDepCallCounter,),
_ctx: &Self::Ctx,
) -> bool {
2022-04-16 17:23:31 +00:00
self.0 += 1;
self.1 = sibling.clone();
true
}
}
#[derive(Debug, Clone, Default, PartialEq)]
struct BDepCallCounter(usize, CDepCallCounter);
impl NodeDepState for BDepCallCounter {
2022-04-16 17:23:31 +00:00
type Ctx = ();
type DepState = (CDepCallCounter,);
2022-04-16 17:23:31 +00:00
const NODE_MASK: NodeMask = NodeMask::NONE;
fn reduce(
&mut self,
_node: NodeView,
(sibling,): (&CDepCallCounter,),
_ctx: &Self::Ctx,
) -> bool {
2022-04-16 17:23:31 +00:00
self.0 += 1;
self.1 = sibling.clone();
true
}
}
#[derive(Debug, Clone, Default, PartialEq)]
struct CDepCallCounter(usize);
impl NodeDepState for CDepCallCounter {
2022-04-16 17:23:31 +00:00
type Ctx = ();
type DepState = ();
2022-04-16 17:23:31 +00:00
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(&mut self, _node: NodeView, _sibling: (), _ctx: &Self::Ctx) -> bool {
2022-04-16 17:23:31 +00:00
self.0 += 1;
true
}
}
#[test]
fn dependancies_order_independant() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
render!(div {
2022-04-16 17:23:31 +00:00
width: "100%",
p{
"hello"
}
})
}
let mut vdom = VirtualDom::new(Base);
2022-04-16 17:23:31 +00:00
let mut dom: RealDom<UnorderedDependanciesState> = RealDom::new();
let mutations = vdom.rebuild();
let (nodes_updated, _) = dom.apply_mutations(mutations);
let _to_rerender = dom.update_state(nodes_updated, SendAnyMap::new());
2022-04-16 17:23:31 +00:00
let c = CDepCallCounter(1);
let b = BDepCallCounter(1, c.clone());
let a = ADepCallCounter(1, b.clone());
dom.traverse_depth_first(|n| {
assert_eq!(&n.state.a, &a);
assert_eq!(&n.state.b, &b);
assert_eq!(&n.state.c, &c);
});
}