dioxus/packages/core/tests/suspense.rs

68 lines
1.6 KiB
Rust
Raw Normal View History

2022-11-24 07:15:01 +00:00
use dioxus::prelude::*;
2024-03-06 00:24:01 +00:00
use std::future::poll_fn;
use std::task::Poll;
2023-07-15 00:13:49 +00:00
#[test]
2024-01-15 17:06:27 +00:00
fn suspense_resolves() {
2022-11-24 07:15:01 +00:00
// wait just a moment, not enough time for the boundary to resolve
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(async {
let mut dom = VirtualDom::new(app);
2024-01-15 17:06:27 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
dom.wait_for_suspense().await;
2023-12-27 16:23:56 +00:00
let out = dioxus_ssr::render(&dom);
2023-01-09 21:50:33 +00:00
assert_eq!(out, "<div>Waiting for... child</div>");
2023-01-09 21:50:33 +00:00
dbg!(out);
});
2022-11-06 22:28:41 +00:00
}
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx!(
2022-11-24 07:15:01 +00:00
div {
"Waiting for... "
suspended_child {}
2022-11-24 07:15:01 +00:00
}
2024-01-11 01:21:15 +00:00
)
2022-11-09 03:39:37 +00:00
}
2024-01-11 17:11:44 +00:00
fn suspended_child() -> Element {
let mut val = use_signal(|| 0);
2023-07-14 23:15:20 +00:00
2024-03-06 00:24:01 +00:00
// Tasks that are not suspended should never be polled
spawn(async move {
panic!("Non-suspended task was polled");
});
2024-03-07 17:49:51 +00:00
// Memos should still work like normal
let memo = use_memo(move || val * 2);
assert_eq!(memo, val * 2);
2024-03-06 00:24:01 +00:00
2024-01-16 21:51:02 +00:00
if val() < 3 {
2024-03-06 00:24:01 +00:00
let task = spawn(async move {
// Poll each task 3 times
let mut count = 0;
poll_fn(|cx| {
println!("polling... {}", count);
if count < 3 {
count += 1;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
})
.await;
println!("waiting... {}", val);
val += 1;
});
2024-03-06 00:24:01 +00:00
suspend(task)?;
}
2023-07-14 23:15:20 +00:00
2024-01-16 19:18:46 +00:00
rsx!("child")
2022-11-06 22:28:41 +00:00
}