dioxus/packages/desktop/headless_tests/rendering.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

2023-05-04 11:04:06 -05:00
use dioxus::prelude::*;
2024-01-18 04:07:28 -08:00
use dioxus_core::Element;
2024-01-30 17:59:57 -08:00
use dioxus_desktop::DesktopContext;
2023-05-04 11:04:06 -05:00
#[path = "./utils.rs"]
mod utils;
2023-05-04 11:04:06 -05:00
fn main() {
utils::check_app_exits(check_html_renders);
2023-05-04 11:04:06 -05: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() {
// serde_json::Value::String(html)
println!("html: {}", html);
value.set(Some(html.to_string()));
}
});
2023-05-04 11:04:06 -05:00
});
2023-05-04 11:04:06 -05:00
value.read().clone()
}
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 11:04:06 -05:00
fn check_html_renders() -> Element {
let inner_html = use_inner_html("main_div");
let desktop_context: DesktopContext = consume_context();
2023-05-04 11:04:06 -05:00
if let Some(raw_html) = inner_html {
println!("{}", raw_html);
2024-01-18 04:07:28 -08:00
let fragment = &raw_html;
let expected = EXPECTED_HTML;
assert_eq!(raw_html, EXPECTED_HTML);
2023-05-04 11:04:06 -05:00
if fragment == expected {
println!("html matches");
desktop_context.close();
}
}
let dyn_value = 0;
let dyn_element = rsx! {
div {
dangerous_inner_html: "<p>hello world</p>",
}
};
2024-01-16 13:18:46 -06:00
rsx! {
2023-05-04 11:04:06 -05:00
div {
id: "main_div",
div {
width: "100px",
height: "100px",
color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
id: 5,
input {
"type": "checkbox",
},
h1 {
"text"
}
{dyn_element}
2023-05-04 11:04:06 -05:00
}
}
}
}