sharedstate.rs 959 B

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