2022-11-24 07:15:01 +00:00
|
|
|
use dioxus::core::ElementId;
|
|
|
|
use dioxus::core::{Mutation::*, SuspenseBoundary};
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
use dioxus_core::SuspenseContext;
|
|
|
|
use std::{rc::Rc, time::Duration};
|
2022-11-06 22:28:41 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn it_works() {
|
|
|
|
let mut dom = VirtualDom::new(app);
|
|
|
|
|
2022-11-24 07:15:01 +00:00
|
|
|
let mutations = dom.rebuild().santize();
|
2022-11-06 22:28:41 +00:00
|
|
|
|
2022-11-24 07:15:01 +00:00
|
|
|
// We should at least get the top-level template in
|
|
|
|
assert_eq!(
|
|
|
|
mutations.template_mutations,
|
|
|
|
[
|
|
|
|
CreateElement { name: "div", namespace: None, id: ElementId(0) },
|
|
|
|
CreateStaticText { value: "Waiting for child..." },
|
|
|
|
CreatePlaceholder { id: ElementId(0) },
|
|
|
|
AppendChildren { m: 2 },
|
|
|
|
SaveTemplate { name: "template", m: 1 }
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// And we should load it in and assign the placeholder properly
|
|
|
|
assert_eq!(
|
|
|
|
mutations.edits,
|
|
|
|
[
|
|
|
|
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
|
|
|
|
// hmmmmmmmmm.... with suspense how do we guarantee that IDs increase linearly?
|
|
|
|
// can we even?
|
|
|
|
AssignId { path: &[1], id: ElementId(3) },
|
|
|
|
AppendChildren { m: 1 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// wait just a moment, not enough time for the boundary to resolve
|
2022-11-06 22:28:41 +00:00
|
|
|
|
|
|
|
dom.wait_for_work().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
2022-11-24 07:15:01 +00:00
|
|
|
cx.render(rsx!(
|
|
|
|
div {
|
|
|
|
"Waiting for child..."
|
|
|
|
suspense_boundary {}
|
|
|
|
}
|
|
|
|
))
|
2022-11-09 03:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn suspense_boundary(cx: Scope) -> Element {
|
2022-11-24 07:15:01 +00:00
|
|
|
cx.use_hook(|| cx.provide_context(Rc::new(SuspenseBoundary::new(cx.scope_id()))));
|
2022-11-09 03:39:37 +00:00
|
|
|
|
2022-11-24 07:15:01 +00:00
|
|
|
// Ensure the right types are found
|
|
|
|
cx.has_context::<SuspenseContext>().unwrap();
|
2022-11-09 03:39:37 +00:00
|
|
|
|
2022-11-24 07:15:01 +00:00
|
|
|
cx.render(rsx!(async_child {}))
|
2022-11-06 22:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn async_child(cx: Scope<'_>) -> Element {
|
2022-11-24 07:15:01 +00:00
|
|
|
use_future!(cx, || tokio::time::sleep(Duration::from_millis(10))).await;
|
|
|
|
cx.render(rsx!(async_text {}))
|
2022-11-09 03:39:37 +00:00
|
|
|
}
|
2022-11-08 06:55:22 +00:00
|
|
|
|
2022-11-09 03:39:37 +00:00
|
|
|
async fn async_text(cx: Scope<'_>) -> Element {
|
2022-11-24 07:15:01 +00:00
|
|
|
cx.render(rsx!("async_text"))
|
2022-11-06 22:28:41 +00:00
|
|
|
}
|