wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use std::{collections::HashMap, convert::TryFrom}; use wasm_bindgen_test::*; use web_sys::{DomParser, SupportedType}; use web_sys_query::{self as query, query}; const HTML5_DOC: &str = r#"
This is a paragraph.
This is another paragraph.
"#; fn parse_document(source: &str) -> query::Document { console_log::init_with_level(log::Level::Info).ok(); let parser = DomParser::new().unwrap(); parser .parse_from_string(source, SupportedType::TextHtml) .unwrap() .into() } #[wasm_bindgen_test] fn test_by_id() { let document = parse_document(HTML5_DOC); let hero = document.find("#hero").unwrap().first().unwrap(); console_log!("by_id: {:?}", hero); assert_eq!("hero", hero.attr("id").unwrap()); assert_eq!("hero", hero.id()); } #[wasm_bindgen_test] fn test_by_selectors() { let document = parse_document(HTML5_DOC); let matching = document.find("body p, #hero").unwrap(); console_log!("by_selectors: {:?}", matching); assert_eq!(matching.len(), 3); } #[wasm_bindgen_test] fn test_order() { let document = parse_document(HTML5_DOC); let matching = document.find("*").unwrap(); console_log!("order: {:?}", matching); let zero = matching.get(0).unwrap(); assert_eq!(zero.local_name(), "html", "{:?}", matching); let five = matching.get(4).unwrap(); assert_eq!(five.local_name(), "h1", "{:?}", matching); } #[wasm_bindgen_test] fn test_document_root() { let document = parse_document(HTML5_DOC); let root = query::Element::try_from(&document).unwrap(); assert_eq!(root.local_name(), "html", "{:?}", document); } #[wasm_bindgen_test] fn test_query_document() { let document = parse_document(HTML5_DOC); let matching = query!(document, "title").unwrap(); console_log!("query_document: {:?}", matching); let title = matching.get(0).unwrap(); assert_eq!(title.text().unwrap(), "Page Title"); } #[wasm_bindgen_test] fn test_query() { let matching = query!("*").unwrap(); console_log!("query: {:?}", matching); assert!(matching.len() > 0); } #[wasm_bindgen_test] fn test_form() { let document = parse_document(HTML5_DOC); let form = query!(document, "form").unwrap().first().unwrap(); let kv: HashMap<_, String> = form.serialize_array().unwrap(); console_log!("form: {:?}", kv); assert_eq!(kv.len(), 6); assert_eq!(kv.get("a").unwrap(), "1"); } #[wasm_bindgen_test] fn test_form_element() { let document = parse_document(HTML5_DOC); let form = query!(document, "form").unwrap().first().unwrap(); let kv: query::FormData = form.serialize_array().unwrap(); console_log!("form_element: {:?}", kv); assert_eq!(kv.len(), 6); } #[wasm_bindgen_test] fn test_form_collection() { let document = parse_document(HTML5_DOC); let form = query!(document, "form").unwrap(); let single: query::FormData = form.serialize_array().unwrap(); let collection: query::FormData = form.first().unwrap().serialize_array().unwrap(); console_log!("form_collection: {:?}", collection); assert_eq!(single, collection); }