sharedstate.rs 981 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use dioxus::{nodes::VSuspended, prelude::*, DomEdit, TestDom};
  2. use dioxus_core as dioxus;
  3. use dioxus_core_macro::*;
  4. use dioxus_html as dioxus_elements;
  5. use DomEdit::*;
  6. mod test_logging;
  7. fn new_dom() -> TestDom {
  8. const IS_LOGGING_ENABLED: bool = false;
  9. test_logging::set_up_logging(IS_LOGGING_ENABLED);
  10. TestDom::new()
  11. }
  12. #[test]
  13. fn shared_state_test() {
  14. struct MySharedState(&'static str);
  15. static App: FC<()> = |(cx, props)| {
  16. cx.provide_state(MySharedState("world!"));
  17. rsx!(cx, Child {})
  18. };
  19. static Child: FC<()> = |(cx, props)| {
  20. let shared = cx.consume_state::<MySharedState>()?;
  21. rsx!(cx, "Hello, {shared.0}")
  22. };
  23. let mut dom = VirtualDom::new(App);
  24. let Mutations { edits, .. } = dom.rebuild();
  25. assert_eq!(
  26. edits,
  27. [
  28. CreateTextNode {
  29. root: 0,
  30. text: "Hello, world!"
  31. },
  32. AppendChildren { many: 1 },
  33. ]
  34. );
  35. }