use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use web_sys::Element;
#[wasm_bindgen(module = "/tests/wasm/element.js")]
extern "C" {
fn new_div() -> Element;
}
#[wasm_bindgen_test]
fn element() {
let element = new_div();
assert_eq!(element, element);
assert_eq!(element.prefix(), None, "Shouldn't have a prefix");
assert_eq!(element.local_name(), "div", "Should have a div local name");
assert_eq!(element.tag_name(), "DIV", "Should be a div tag");
assert!(!element.has_attribute("id"), "Shouldn't have an id");
element.set_id("beep");
assert_eq!(element.id(), "beep", "Should have an id of 'beep'");
assert_eq!(
element.set_attribute("id", "beep").unwrap(),
(),
"Should set id"
);
assert!(element.has_attribute("id"), "Should now have an id");
assert_eq!(
element.remove_attribute("id").unwrap(),
(),
"Should return nothing if removed"
);
assert_eq!(element.class_name(), "", "Shouldn't have a class name");
element.set_class_name("test thing");
assert_eq!(
element.class_name(),
"test thing",
"Should have a class name"
);
assert_eq!(
element.get_attribute("class").unwrap(),
"test thing",
"Should have a class name"
);
assert_eq!(
element.remove_attribute("class").unwrap(),
(),
"Should return nothing if removed"
);
assert!(!element.has_attribute("title"), "Should not have a title");
assert_eq!(
element.set_attribute("title", "boop").unwrap(),
(),
"Should return nothing if set correctly"
);
assert!(element.has_attribute("title"), "Should have a title");
assert_eq!(
element.remove_attribute("title").unwrap(),
(),
"Should return nothing if removed"
);
assert!(!element.has_attribute("title"), "Should not have a title");
assert!(!element.has_attributes(), "Should not have any attributes");
assert_eq!(
element.set_attribute("title", "boop").unwrap(),
(),
"Should return nothing if set correctly"
);
assert!(element.has_attributes(), "Should have attributes");
assert_eq!(
element.remove_attribute("title").unwrap(),
(),
"Should return nothing if removed"
);
assert_eq!(
element.matches(".this-is-a-thing").unwrap(),
false,
"Should not match selector"
);
assert_eq!(
element.webkit_matches_selector(".this-is-a-thing").unwrap(),
false,
"Should not match selector"
);
element.set_class_name("this-is-a-thing");
assert_eq!(
element.matches(".this-is-a-thing").unwrap(),
true,
"Should match selector"
);
assert_eq!(
element.webkit_matches_selector(".this-is-a-thing").unwrap(),
true,
"Should match selector"
);
assert_eq!(
element.remove_attribute("class").unwrap(),
(),
"Should return nothing if removed"
);
assert_eq!(element.inner_html(), "", "Should return no content");
element.set_inner_html("<strong>Hey!</strong><em>Web!</em>");
assert_eq!(
element.inner_html(),
"<strong>Hey!</strong><em>Web!</em>",
"Should return HTML conent"
);
assert_eq!(
element.query_selector_all("strong").unwrap().length(),
1,
"Should return one element"
);
assert!(
element.query_selector("strong").unwrap().is_some(),
"Should return an element"
);
element.set_inner_html("");
assert_eq!(element.inner_html(), "", "Should return no content");
assert!(
element.query_selector(".none-existant").unwrap().is_none(),
"Should return no results"
);
assert_eq!(
element
.query_selector_all(".none-existant")
.unwrap()
.length(),
0,
"Should return no results"
);
let child = new_div();
assert_eq!(
element.get_elements_by_tag_name("div").length(),
0,
"Element should not contain any div child"
);
element.append_child(&child).unwrap();
assert_eq!(
element.get_elements_by_tag_name("div").length(),
1,
"Element should contain one div child"
);
assert_eq!(
element.get_elements_by_class_name("foo").length(),
0,
"Element should not have childs with class foo"
);
child.class_list().add_1("foo").unwrap();
assert_eq!(
element.get_elements_by_class_name("foo").length(),
1,
"Element should have one child with class foo"
);
element.remove_child(&child).unwrap();
}