dioxus/packages/core/tests/sharedstate.rs

40 lines
851 B
Rust
Raw Normal View History

2021-10-22 01:16:39 -04:00
#![allow(unused, non_upper_case_globals)]
use dioxus::{prelude::*, DomEdit, TestDom};
2021-09-16 13:20:04 -04:00
use dioxus_core as dioxus;
2021-09-24 21:46:23 -04:00
use dioxus_core_macro::*;
2021-09-16 13:20:04 -04:00
use dioxus_html as dioxus_elements;
2021-09-24 21:46:23 -04:00
2021-09-16 13:20:04 -04:00
use DomEdit::*;
mod test_logging;
#[test]
fn shared_state_test() {
struct MySharedState(&'static str);
2021-10-16 17:37:28 -04:00
static App: FC<()> = |(cx, props)| {
2021-10-11 15:35:20 -04:00
cx.provide_state(MySharedState("world!"));
rsx!(Child {})
2021-09-16 13:20:04 -04:00
};
2021-10-16 17:37:28 -04:00
static Child: FC<()> = |(cx, props)| {
2021-10-11 15:35:20 -04:00
let shared = cx.consume_state::<MySharedState>()?;
rsx!("Hello, {shared.0}")
2021-09-16 13:20:04 -04:00
};
let mut dom = VirtualDom::new(App);
let Mutations { edits, .. } = dom.rebuild();
assert_eq!(
edits,
[
CreateTextNode {
2021-10-05 03:37:15 -04:00
root: 0,
2021-09-16 13:20:04 -04:00
text: "Hello, world!"
},
AppendChildren { many: 1 },
]
);
}