// #![allow(dead_code, unused_variables, unused_imports)] use wasm_bindgen::JsCast; use wasm_bindgen_test::wasm_bindgen_test_configure; use wasm_bindgen_test::*; use web_sys::{window, HtmlElement}; use weblog::console_log; use std::cell::Cell; use std::rc::Rc; use webru::*; wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] fn create_element_test() { const INNER_HTML: &str = "Testing time"; const ID: &str = "heading"; // Creating an html element `h1` with the `create_element()` function let h1 = create_element("h1"); // Setting some attributes h1.set_inner_html(INNER_HTML); h1.set_id(ID); // inserting the element inside the
body().append_child(&h1).unwrap(); // Now asserting the newly created element assert_eq!(h1.inner_html(), INNER_HTML); assert_eq!(h1.id(), ID); assert_ne!(h1.inner_html(), "Wrong heading"); assert_ne!(h1.id(), "wrong id"); } #[wasm_bindgen_test] fn get_element_by_id_test() { const PARAGRAPH_ID: &str = "little_para"; const PARAGRAPH_CLASS: &str = "para graph"; // Creating an html elementlet p = create_element("p"); // adding some attributes p.set_id(PARAGRAPH_ID); p.set_class_name(PARAGRAPH_CLASS); { // getting the element with the `get_element_by_id` without inserting it into the DOM let p = get_element_by_id(PARAGRAPH_ID); // at this moment, this should be None because the element hasn't been inserted in the
assert_eq!(p, None); } // inserting the element into the body().append_child(&p).unwrap(); // getting this element with the `get_element_by_id` function after inserting it into the DOM let p = get_element_by_id(PARAGRAPH_ID); // asserting assert_eq!(p.clone().unwrap().class_name(), PARAGRAPH_CLASS); assert_ne!(p, None); } #[wasm_bindgen_test] fn get_elements_by_classname_test() { /// classname of the 1st and 2ndconst PARAGRAPH_CLASS: &str = "sad fsda fasdf"; /// id of the 1st
const PARAGRAPH_1_ID: &str = "@%ASF"; /// id of the 2nd
const PARAGRAPH_2_ID: &str = "@%ASGAH"; // Creating 1st html element
let p1 = create_element("p"); // adding some attributes p1.set_class_name(PARAGRAPH_CLASS); p1.set_id(PARAGRAPH_1_ID); // Creating 2nd html element
let p2 = create_element("p"); // adding some attributes p2.set_class_name(PARAGRAPH_CLASS); p2.set_id(PARAGRAPH_2_ID); { // getting the element with the `get_elements_by_classname` function without inserting it into the DOM let p = get_elements_by_classname(PARAGRAPH_CLASS); assert_eq!(p.length(), 0); } // inserting the elements into the DOM body().append_child(&p1).unwrap(); body().append_child(&p2).unwrap(); // getting the element with the `get_elements_by_classname` function after inserting it into the DOM let p = get_elements_by_classname(PARAGRAPH_CLASS); // asserting assert_eq!(p.length(), 2); assert_eq!(p.item(0).unwrap().id(), PARAGRAPH_1_ID); assert_eq!(p.item(1).unwrap().id(), PARAGRAPH_2_ID); } #[wasm_bindgen_test] fn get_elements_by_classname_inside_vec_test() { /// classname of the 1st and 2nd
const PARAGRAPH_CLASS: &str = "as dfsadfasd ffa"; /// id of the 1st
const PARAGRAPH_1_ID: &str = "@%asdr4"; /// id of the 2nd
const PARAGRAPH_2_ID: &str = "@%@%^^GA"; // Creating 1st html element
let p1 = create_element("p"); // adding some attributes p1.set_class_name(PARAGRAPH_CLASS); p1.set_id(PARAGRAPH_1_ID); // Creating 2nd html element
let p2 = create_element("p"); // adding some attributes p2.set_class_name(PARAGRAPH_CLASS); p2.set_id(PARAGRAPH_2_ID); { // getting the element with the `get_elements_by_classname` function without inserting it into the DOM let p = get_elements_by_classname_inside_vec(PARAGRAPH_CLASS); assert_eq!(p.len(), 0); } // inserting the elements into the DOM body().append_child(&p1).unwrap(); body().append_child(&p2).unwrap(); // getting the element with the `get_elements_by_classname` function after inserting it into the DOM let p = get_elements_by_classname_inside_vec(PARAGRAPH_CLASS); // asserting assert_eq!(p.len(), 2); assert_eq!(p[0].id(), PARAGRAPH_1_ID); assert_eq!(p[1].id(), PARAGRAPH_2_ID); } #[wasm_bindgen_test] fn query_selector_test() { /// id of the
const PARAGRAPH_ID: &str = "peraid"; /// class of the
const PARAGRAPH_CLASS: &str = "perapera"; // Creating an html element
let p = create_element("p"); // adding some attributes p.set_id(PARAGRAPH_ID); p.set_class_name(PARAGRAPH_CLASS); { // getting the element with the `query_selector` without inserting it into the DOM let p = query_selector(&format!("#{}", PARAGRAPH_ID)); // at this moment, this should be None because the element hasn't been inserted in the
assert_eq!(p, None); } // inserting the element into the body().append_child(&p).unwrap(); // getting the right element with the `query_selector` function after inserting it into the DOM let p_right = query_selector(&format!("#{}", PARAGRAPH_ID)); // geeting the wrong element with the `query_selector` function after inserting it into the DOM let p_wrong = query_selector(&format!("#{}", "ewsdfs")); // asserting assert_eq!(p_right.clone().unwrap().class_name(), PARAGRAPH_CLASS); assert_ne!(p_right, p_wrong); assert_ne!(p_right, None); } #[wasm_bindgen_test] fn query_selector_all_test() { /// classname of the 1st and 2ndconst PARAGRAPH_CLASS: &str = "sadjfalsdf"; /// id of the 1st
const PARAGRAPH_1_ID: &str = "@***>>>"; /// id of the 2nd
const PARAGRAPH_2_ID: &str = "!!__<<"; // Creating 1st html element
let p1 = create_element("p"); // adding some attributes p1.set_class_name(PARAGRAPH_CLASS); p1.set_id(PARAGRAPH_1_ID); // Creating 2nd html element
let p2 = create_element("p"); // adding some attributes p2.set_class_name(PARAGRAPH_CLASS); p2.set_id(PARAGRAPH_2_ID); { // getting the element with the `get_elements_by_classname` function without inserting it into the DOM let p = query_selector_all(&format!(".{}", PARAGRAPH_CLASS)); assert_eq!(p.length(), 0); } // inserting the elements into the DOM body().append_child(&p1).unwrap(); body().append_child(&p2).unwrap(); // getting the element with the `get_elements_by_classname` function after inserting it into the DOM let p = query_selector_all(&format!(".{}", PARAGRAPH_CLASS)); // asserting assert_eq!(p.length(), 2); } #[wasm_bindgen_test] fn query_selector_all_inside_vec_test() { /// classname of the 1st and 2nd
const PARAGRAPH_CLASS: &str = "cleardldls"; /// id of the 1st
const PARAGRAPH_1_ID: &str = "@***>>>"; /// id of the 2nd
const PARAGRAPH_2_ID: &str = "!!__<<"; // Creating 1st html element
let p1 = create_element("p"); // adding some attributes p1.set_class_name(PARAGRAPH_CLASS); p1.set_id(PARAGRAPH_1_ID); // Creating 2nd html element
let p2 = create_element("p"); // adding some attributes p2.set_class_name(PARAGRAPH_CLASS); p2.set_id(PARAGRAPH_2_ID); { // getting the element with the `get_elements_by_classname` function without inserting it into the DOM let p = query_selector_all_inside_vec(&format!(".{}", PARAGRAPH_CLASS)); assert_eq!(p.len(), 0); } // inserting the elements into the DOM body().append_child(&p1).unwrap(); body().append_child(&p2).unwrap(); // getting the element with the `get_elements_by_classname` function after inserting it into the DOM let p = query_selector_all_inside_vec(&format!(".{}", PARAGRAPH_CLASS)); // asserting assert_eq!(p.len(), 2); } #[wasm_bindgen_test] fn document_test() { assert_eq!(document(), window().unwrap().document().unwrap()); } #[wasm_bindgen_test] fn location_test() { assert_eq!( location(), window().unwrap().document().unwrap().location().unwrap() ); } #[wasm_bindgen_test] fn url_test() { // For this test, my url is http://127.0.0.1:8000/. Your might be different. console_log!("The value of `url()` for your website is: ", url()); assert_eq!( url(), web_sys::window() .unwrap() .document() .unwrap() .url() .unwrap() ); } #[wasm_bindgen_test] fn body_test() { let body = body(); assert_eq!(body, window().unwrap().document().unwrap().body().unwrap()); assert_eq!(body.tag_name(), "BODY"); } #[wasm_bindgen_test] fn domain_name_test() { // In my case the domain name is 127.0.0.1 assert_eq!(domain_name(), "127.0.0.1"); console_log!( "The value of `domain_name()` for your website is: ", domain_name() ) } #[wasm_bindgen_test] fn path_name_test() { assert_eq!(path_name(), "/") } #[wasm_bindgen_test] fn reload_test() { // NOTE: You need to test it manually // creating a