dioxus/packages/core/tests/context_api.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2022-11-27 05:38:40 +00:00
use dioxus::core::{ElementId, Mutation::*};
use dioxus::prelude::*;
#[test]
fn state_shares() {
fn app(cx: Scope) -> Element {
cx.provide_context(cx.generation() as i32);
cx.render(rsx!(child_1 {}))
}
fn child_1(cx: Scope) -> Element {
cx.render(rsx!(child_2 {}))
}
fn child_2(cx: Scope) -> Element {
let value = cx.consume_context::<i32>().unwrap();
cx.render(rsx!("Value is {value}"))
}
let mut dom = VirtualDom::new(app);
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.rebuild().santize().edits,
2022-11-27 05:38:40 +00:00
[
CreateTextNode { value: "Value is 0", id: ElementId(1,) },
2022-12-03 00:24:49 +00:00
AppendChildren { m: 1, id: ElementId(0) },
2022-11-27 05:38:40 +00:00
]
);
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
_ = dom.render_immediate();
2022-12-07 01:41:47 +00:00
assert_eq!(dom.base_scope().consume_context::<i32>().unwrap(), 1);
2022-11-27 05:38:40 +00:00
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
_ = dom.render_immediate();
2022-12-07 01:41:47 +00:00
assert_eq!(dom.base_scope().consume_context::<i32>().unwrap(), 2);
2022-11-27 05:38:40 +00:00
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(2));
2022-11-27 05:38:40 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.render_immediate().santize().edits,
2022-11-27 05:38:40 +00:00
[SetText { value: "Value is 2", id: ElementId(1,) },]
);
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
dom.mark_dirty(ScopeId(2));
2022-11-27 05:38:40 +00:00
let edits = dom.render_immediate();
assert_eq!(
2022-12-01 05:46:15 +00:00
edits.santize().edits,
2022-11-27 05:38:40 +00:00
[SetText { value: "Value is 3", id: ElementId(1,) },]
);
}