suspense.rs 861 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use dioxus::core::Mutation::*;
  2. use dioxus::prelude::*;
  3. use std::future::IntoFuture;
  4. use std::rc::Rc;
  5. use std::time::Duration;
  6. #[tokio::test]
  7. async fn it_works() {
  8. // wait just a moment, not enough time for the boundary to resolve
  9. let mut dom = VirtualDom::new(app);
  10. _ = dom.rebuild();
  11. dom.wait_for_suspense().await;
  12. let out = dioxus_ssr::pre_render(&dom);
  13. assert_eq!(out, "<div>Waiting for... child</div>");
  14. dbg!(out);
  15. }
  16. fn app(cx: Scope) -> Element {
  17. cx.render(rsx!(
  18. div {
  19. "Waiting for... "
  20. suspended_child {}
  21. }
  22. ))
  23. }
  24. fn suspended_child(cx: Scope) -> Element {
  25. let mut val = use_state(cx, || 0);
  26. if **val < 3 {
  27. let mut val = val.clone();
  28. cx.spawn(async move {
  29. val += 1;
  30. });
  31. return cx.suspend()?;
  32. }
  33. render!("child")
  34. }