suspense.rs 755 B

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