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

534 lines
14 KiB
Rust
Raw Normal View History

2022-04-12 12:51:57 +00:00
use anymap::AnyMap;
2022-06-25 13:27:10 +00:00
use dioxus::core as dioxus_core;
2022-06-25 13:48:03 +00:00
use dioxus::core::{AttributeValue, DomEdit, Mutations};
2022-06-25 13:27:10 +00:00
use dioxus::prelude::*;
2022-04-16 17:23:31 +00:00
use dioxus_native_core::node_ref::*;
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};
2022-04-12 12:51:57 +00:00
use dioxus_native_core_macro::State;
2022-04-20 01:12:57 +00:00
#[derive(Debug, Clone, Default, State)]
struct CallCounterStatePart1 {
#[child_dep_state(child_counter)]
child_counter: ChildDepCallCounter,
}
#[derive(Debug, Clone, Default, State)]
struct CallCounterStatePart2 {
#[parent_dep_state(parent_counter)]
parent_counter: ParentDepCallCounter,
}
#[derive(Debug, Clone, Default, State)]
struct CallCounterStatePart3 {
#[node_dep_state()]
node_counter: NodeDepCallCounter,
}
2022-04-12 12:51:57 +00:00
#[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-20 01:12:57 +00:00
#[state]
part2: CallCounterStatePart2,
2022-04-16 17:23:31 +00:00
#[parent_dep_state(parent_counter)]
2022-04-12 12:51:57 +00:00
parent_counter: ParentDepCallCounter,
2022-04-20 01:12:57 +00:00
#[state]
part1: CallCounterStatePart1,
#[state]
part3: CallCounterStatePart3,
2022-04-12 12:51:57 +00:00
#[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 = ();
2022-04-12 12:51:57 +00:00
type DepState = Self;
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,
2022-04-12 23:46:16 +00:00
_: NodeView,
_: impl Iterator<Item = &'a Self::DepState>,
_: &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 = ();
2022-04-12 12:51:57 +00:00
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(
&mut self,
_node: NodeView,
_parent: Option<&Self::DepState>,
_ctx: &Self::Ctx,
) -> bool {
self.0 += 1;
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 {
type Ctx = ();
2022-04-16 17:23:31 +00:00
type DepState = ();
2022-04-12 12:51:57 +00:00
const NODE_MASK: NodeMask = NodeMask::ALL;
2022-04-16 17:23:31 +00:00
fn reduce(&mut self, _node: NodeView, _sibling: &Self::DepState, _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;
2022-04-12 12:51:57 +00:00
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::DepState>,
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;
2022-04-12 12:51:57 +00:00
type DepState = Self;
2022-04-16 22:22:03 +00:00
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
2022-04-12 12:51:57 +00:00
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, 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()),
2022-03-27 01:10:15 +00:00
parent.map(|c| Box::new(c.clone())),
);
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 {
type Ctx = u32;
2022-04-16 17:23:31 +00:00
type DepState = ();
2022-04-16 22:22:03 +00:00
const NODE_MASK: NodeMask = NodeMask::new_with_attrs(AttributeMask::All).with_tag();
2022-04-16 17:23:31 +00:00
fn reduce(&mut self, node: NodeView, _sibling: &Self::DepState, 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(|a| (a.name.to_string(), a.value.to_string()))
.collect(),
);
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 {
rsx!(cx, div {
p{}
h1{}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
2022-04-12 12:51:57 +00:00
p{
color: "red"
}
2022-03-27 01:10:15 +00:00
h1{}
}
});
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(vec![mutations]);
2022-04-12 12:51:57 +00:00
let mut ctx = AnyMap::new();
ctx.insert(42u32);
let _to_rerender = dom.update_state(&vdom, nodes_updated, ctx);
2022-03-27 01:10:15 +00:00
let root_div = &dom[1];
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(None, None)))
);
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 = &dom[2];
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()),
2022-04-16 17:23:31 +00:00
Some(Box::new(PushedDownStateTester(None, 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 = &dom[3];
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()),
2022-04-16 17:23:31 +00:00
Some(Box::new(PushedDownStateTester(None, 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-04 17:19:21 +00:00
fn state_reduce_initally_called_minimally() {
2022-03-27 01:10:15 +00:00
#[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"
}
}
}
});
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(vec![mutations]);
2022-04-12 12:51:57 +00:00
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
2022-03-27 01:10:15 +00:00
dom.traverse_depth_first(|n| {
2022-04-20 01:12:57 +00:00
assert_eq!(n.state.part1.child_counter.0, 1);
2022-04-12 12:51:57 +00:00
assert_eq!(n.state.child_counter.0, 1);
2022-04-20 01:12:57 +00:00
assert_eq!(n.state.part2.parent_counter.0, 1);
2022-04-12 12:51:57 +00:00
assert_eq!(n.state.parent_counter.0, 1);
2022-04-20 01:12:57 +00:00
assert_eq!(n.state.part3.node_counter.0, 1);
2022-04-12 12:51:57 +00:00
assert_eq!(n.state.node_counter.0, 1);
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 {
rsx!(cx, div {
2022-04-12 12:51:57 +00:00
width: "100%",
2022-03-27 01:10:15 +00:00
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"
}
}
}
});
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(vec![mutations]);
2022-04-12 12:51:57 +00:00
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
let nodes_updated = dom.apply_mutations(vec![Mutations {
2022-03-27 01:10:15 +00:00
edits: vec![DomEdit::SetAttribute {
root: 1,
field: "width",
value: AttributeValue::Text("99%"),
2022-03-27 01:10:15 +00:00
ns: Some("style"),
}],
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
2022-04-12 12:51:57 +00:00
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
2022-03-27 01:10:15 +00:00
dom.traverse_depth_first(|n| {
2022-04-20 01:12:57 +00:00
assert_eq!(n.state.part2.parent_counter.0, 2);
2022-04-12 23:46:16 +00:00
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 {
rsx!(cx, div {
div{
div{
2022-04-16 17:23:31 +00:00
p{
width: "100%",
}
2022-03-27 01:10:15 +00:00
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
div{
div{
2022-04-16 17:23:31 +00:00
p{
width: "100%",
}
2022-03-27 01:10:15 +00:00
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
}
});
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(vec![mutations]);
2022-04-12 12:51:57 +00:00
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
let nodes_updated = dom.apply_mutations(vec![Mutations {
2022-03-27 01:10:15 +00:00
edits: vec![DomEdit::SetAttribute {
root: 4,
field: "width",
value: AttributeValue::Text("99%"),
2022-03-27 01:10:15 +00:00
ns: Some("style"),
}],
dirty_scopes: fxhash::FxHashSet::default(),
refs: Vec::new(),
}]);
2022-04-12 12:51:57 +00:00
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
2022-03-27 01:10:15 +00:00
dom.traverse_depth_first(|n| {
2022-04-16 17:23:31 +00:00
println!("{:?}", n);
2022-04-20 01:12:57 +00:00
assert_eq!(
n.state.part1.child_counter.0,
if n.id.0 > 4 { 1 } else { 2 }
);
2022-04-12 23:46:16 +00:00
assert_eq!(n.state.child_counter.0, if n.id.0 > 4 { 1 } else { 2 });
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 {
type Ctx = ();
type DepState = BDepCallCounter;
const NODE_MASK: NodeMask = NodeMask::NONE;
fn reduce(&mut self, _node: NodeView, sibling: &Self::DepState, _ctx: &Self::Ctx) -> bool {
self.0 += 1;
self.1 = sibling.clone();
true
}
}
#[derive(Debug, Clone, Default, PartialEq)]
struct BDepCallCounter(usize, CDepCallCounter);
impl NodeDepState for BDepCallCounter {
type Ctx = ();
type DepState = CDepCallCounter;
const NODE_MASK: NodeMask = NodeMask::NONE;
fn reduce(&mut self, _node: NodeView, sibling: &Self::DepState, _ctx: &Self::Ctx) -> bool {
self.0 += 1;
self.1 = sibling.clone();
true
}
}
#[derive(Debug, Clone, Default, PartialEq)]
struct CDepCallCounter(usize);
impl NodeDepState for CDepCallCounter {
type Ctx = ();
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(&mut self, _node: NodeView, _sibling: &Self::DepState, _ctx: &Self::Ctx) -> bool {
self.0 += 1;
true
}
}
#[test]
fn dependancies_order_independant() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
rsx!(cx, div {
width: "100%",
p{
"hello"
}
})
}
let vdom = VirtualDom::new(Base);
let mutations = vdom.create_vnodes(rsx! {
div {
width: "100%",
p{
"hello"
}
}
});
let mut dom: RealDom<UnorderedDependanciesState> = RealDom::new();
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(&vdom, nodes_updated, AnyMap::new());
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);
});
}
2022-04-20 01:12:57 +00:00
#[derive(Clone, Default, State)]
struct DependanciesStateTest {
#[node_dep_state(c)]
b: BDepCallCounter,
#[node_dep_state()]
c: CDepCallCounter,
#[node_dep_state(b)]
a: ADepCallCounter,
#[state]
child: UnorderedDependanciesState,
}