dioxus/packages/desktop/headless_tests/rendering.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2023-05-04 16:04:06 +00:00
use dioxus::prelude::*;
2024-01-31 01:59:57 +00:00
use dioxus_desktop::DesktopContext;
2023-05-04 16:04:06 +00:00
#[path = "./utils.rs"]
mod utils;
2023-05-04 16:04:06 +00:00
fn main() {
#[cfg(not(windows))]
utils::check_app_exits(check_html_renders);
2023-05-04 16:04:06 +00:00
}
fn use_inner_html(id: &'static str) -> Option<String> {
let mut value = use_signal(|| None as Option<String>);
use_effect(move || {
spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let res = eval(&format!(
r#"let element = document.getElementById('{}');
return element.innerHTML"#,
id
))
.await
.unwrap();
if let Some(html) = res.as_str() {
println!("html: {}", html);
value.set(Some(html.to_string()));
}
});
2023-05-04 16:04:06 +00:00
});
2024-03-08 16:54:02 +00:00
value()
2023-05-04 16:04:06 +00:00
}
const EXPECTED_HTML: &str = r#"<div style="width: 100px; height: 100px; color: rgb(0, 0, 0);" id="5"><input type="checkbox"><h1>text</h1><div><p>hello world</p></div></div>"#;
2023-05-04 16:04:06 +00:00
fn check_html_renders() -> Element {
let inner_html = use_inner_html("main_div");
let desktop_context: DesktopContext = consume_context();
2023-05-04 16:04:06 +00:00
if let Some(raw_html) = inner_html {
println!("{}", raw_html);
2024-01-18 12:07:28 +00:00
let fragment = &raw_html;
let expected = EXPECTED_HTML;
assert_eq!(raw_html, EXPECTED_HTML);
2023-05-04 16:04:06 +00:00
if fragment == expected {
println!("html matches");
desktop_context.close();
}
}
let dyn_value = 0;
2024-04-17 22:08:38 +00:00
let dyn_element = rsx! { div { dangerous_inner_html: "<p>hello world</p>" } };
2023-05-04 16:04:06 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2024-04-17 22:08:38 +00:00
div { id: "main_div",
2023-05-04 16:04:06 +00:00
div {
width: "100px",
height: "100px",
color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
id: 5,
2024-04-17 22:08:38 +00:00
input { "type": "checkbox" }
h1 { "text" }
{dyn_element}
2023-05-04 16:04:06 +00:00
}
}
}
}