use axum_wire::{html, mount_component, mount_component_sync, wire_actions, Component};
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize, Component)]
struct AsyncChild;
#[wire_actions]
impl AsyncChild {
async fn mount() -> Self {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
Self
}
async fn render(&self) -> String {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
html! { <span id="child">"child async ok"</span> }
}
}
#[derive(Default, Serialize, Deserialize, Component)]
struct AsyncParent;
#[wire_actions]
impl AsyncParent {
async fn render(&self) -> String {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
html! {
<div id="parent">
<AsyncChild />
</div>
}
}
}
#[derive(Default, Serialize, Deserialize, Component)]
struct PropsChild {
pub label: String,
}
#[wire_actions]
impl PropsChild {
fn render(&self) -> String {
let label = &self.label;
html! { <p id="props-child">{label}</p> }
}
}
#[derive(Default, Serialize, Deserialize, Component)]
struct PropsParent;
#[wire_actions]
impl PropsParent {
async fn render(&self) -> String {
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
html! {
<section id="props-parent">
<PropsChild label=String::from("hello from parent") />
</section>
}
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn nested_async_component_renders_from_async_parent() {
let html = mount_component("async-parent")
.await
.expect("mount should succeed");
assert!(html.contains("id=\"parent\""));
assert!(html.contains("id=\"child\""));
assert!(html.contains("child async ok"));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sync_subcomponent_mount_inside_tokio_does_not_panic() {
let result = std::panic::catch_unwind(|| mount_component_sync("async-child"));
assert!(
result.is_ok(),
"mount_component_sync panicked inside tokio runtime"
);
let html = result.unwrap().expect("mount should succeed");
assert!(html.contains("child async ok"));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn nested_props_component_still_renders() {
let html = mount_component("props-parent")
.await
.expect("mount should succeed");
assert!(html.contains("id=\"props-parent\""));
assert!(html.contains("id=\"props-child\""));
assert!(html.contains("hello from parent"));
}