pub mod keyed; pub mod non_keyed; pub mod router; use hirola::dom::*; use hirola::prelude::signal::Mutable; use hirola::prelude::*; use hirola::signal::SignalExt; use hirola_dom::dom_test_utils::{next_tick, next_tick_with}; use hirola_dom::node_ref::NodeRef; use wasm_bindgen_test::*; use web_sys::{Document, HtmlElement, Node, Window}; wasm_bindgen_test_configure!(run_in_browser); fn window() -> Window { web_sys::window().unwrap() } fn document() -> Document { window().document().unwrap() } /// Returns a [`Node`] referencing the test container with the contents cleared. fn test_div() -> Node { if document() .query_selector("div#test-container") .unwrap() .is_none() { document() .body() .unwrap() .insert_adjacent_html("beforeend", r#"
"#) .unwrap(); } let container = document() .query_selector("div#test-container") .unwrap() .unwrap(); container.set_inner_html(""); // erase contents from previous test runs container.into() } #[wasm_bindgen_test] fn hello_world() { let node = html! {"Hello World!"
}; let _ = render_to(node, &test_div()); assert_eq!( &document() .query_selector("p") .unwrap() .unwrap() .outer_html(), "Hello World!
" ); } #[wasm_bindgen_test] fn hello_world_noderef() { let p_ref = NodeRef::new(); let node = html! {"Hello World!"
}; let _ = render_to(node, &test_div()); assert_eq!( &p_ref.get().unchecked_into::Hello World!
" ); } #[wasm_bindgen_test] fn interpolation() { let text = "Hello Hirola!"; let node = html! {{text}
}; let _ = render_to(node, &test_div()); assert_eq!( document() .query_selector("p") .unwrap() .unwrap() .text_content() .unwrap(), "Hello Hirola!" ); } #[wasm_bindgen_test] fn reactive_text() { let count = Mutable::new(0); let node = html! {{count.clone()}
}; let _ = render_to(node, &test_div()); let p = document().query_selector("p").unwrap().unwrap(); assert_eq!(p.text_content().unwrap(), "0"); count.set(1); next_tick_with(&p, |p| { assert_eq!(p.text_content().unwrap(), "1"); }); } #[wasm_bindgen_test] fn reactive_attribute() { let count = Mutable::new(0i32); let node = html! { }; let _ = render_to(node, &test_div()); let span = document().query_selector("span").unwrap().unwrap(); // assert_eq!(span.get_attribute("attribute").unwrap(), "0"); count.set(1); next_tick(move || { assert_eq!(span.get_attribute("attribute").unwrap(), "1"); }); } #[wasm_bindgen_test] fn noderefs() { let noderef = NodeRef::new(); let node = html! {