use silkenweb::{document::Document, dom::DefaultDom, task::render_now};
use silkenweb_base::document;
use wasm_bindgen::{JsCast, UnwrapThrowExt};
#[must_use]
pub struct BrowserTest;
impl BrowserTest {
pub async fn new(mount_point_id: &str) -> Self {
render_now().await;
if try_html_element(APP_CONTAINER_ID).is_some() {
panic!("`Test` cannot be nested")
}
DefaultDom::unmount_all();
let app_container = document::create_element("div");
app_container.set_id(APP_CONTAINER_ID);
let mount_point = document::create_element("div");
mount_point.set_id(mount_point_id);
app_container
.append_child(&mount_point)
.expect_throw("Couldn't add mount point to app container");
let body = document::body().expect_throw("Couldn't get document body");
body.append_child(&app_container)
.expect_throw("Couldn't add app container to body");
Self
}
pub fn html(&self) -> String {
html_element(APP_CONTAINER_ID).inner_html()
}
}
impl Drop for BrowserTest {
fn drop(&mut self) {
DefaultDom::unmount_all();
html_element(APP_CONTAINER_ID).remove();
}
}
pub fn html_element(id: &str) -> web_sys::HtmlElement {
try_html_element(id).expect_throw("Element not found")
}
pub fn try_html_element(id: &str) -> Option<web_sys::HtmlElement> {
document::query_selector(&format!("#{id}"))
.expect_throw("Error searching for element")
.map(|elem| {
elem.dyn_into()
.expect_throw("Element was not an `HTMLElement")
})
}
const APP_CONTAINER_ID: &str = "silkenweb-test-mount-point";