1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen::JsCast;
3 use wasm_bindgen_test::*;
4 use web_sys::HtmlElement;
5 
6 #[wasm_bindgen(module = "/tests/wasm/element.js")]
7 extern "C" {
new_html() -> HtmlElement8     fn new_html() -> HtmlElement;
9 }
10 
11 #[wasm_bindgen_test]
test_html_element()12 fn test_html_element() {
13     let element = new_html();
14     assert!(element.is_instance_of::<HtmlElement>());
15 
16     assert_eq!(element.title(), "", "Shouldn't have a title");
17     element.set_title("boop");
18     assert_eq!(element.title(), "boop", "Should have a title");
19 
20     assert_eq!(element.lang(), "", "Shouldn't have a lang");
21     element.set_lang("en-us");
22     assert_eq!(element.lang(), "en-us", "Should have a lang");
23 
24     assert_eq!(element.dir(), "", "Shouldn't have a dir");
25     element.set_dir("ltr");
26     assert_eq!(element.dir(), "ltr", "Should have a dir");
27 
28     assert_eq!(element.inner_text(), "", "Shouldn't have inner_text");
29     element.set_inner_text("hey");
30     assert_eq!(element.inner_text(), "hey", "Should have inner_text");
31 
32     assert!(!element.hidden(), "Shouldn't be hidden");
33     element.set_hidden(true);
34     assert!(element.hidden(), "Should be hidden");
35 
36     assert_eq!(
37         element.class_list().get(0),
38         None,
39         "Shouldn't have class at index 0"
40     );
41     element.class_list().add_2("a", "b").unwrap();
42     assert_eq!(
43         element.class_list().get(0).unwrap(),
44         "a",
45         "Should have class at index 0"
46     );
47     assert_eq!(
48         element.class_list().get(1).unwrap(),
49         "b",
50         "Should have class at index 1"
51     );
52     assert_eq!(
53         element.class_list().get(2),
54         None,
55         "Shouldn't have class at index 2"
56     );
57 
58     assert_eq!(element.dataset().get("id"), None, "Shouldn't have data-id");
59     element.dataset().set("id", "123").unwrap();
60     assert_eq!(
61         element.dataset().get("id").unwrap(),
62         "123",
63         "Should have data-id"
64     );
65 
66     assert_eq!(
67         element.style().get(0),
68         None,
69         "Shouldn't have style property name at index 0"
70     );
71     element
72         .style()
73         .set_property("background-color", "red")
74         .unwrap();
75     assert_eq!(
76         element.style().get(0).unwrap(),
77         "background-color",
78         "Should have style property at index 0"
79     );
80     assert_eq!(
81         element
82             .style()
83             .get_property_value("background-color")
84             .unwrap(),
85         "red",
86         "Should have style property"
87     );
88 
89     // TODO add a click handler here
90     element.click();
91 
92     assert_eq!(element.tab_index(), -1, "Shouldn't be tab_index");
93     element.set_tab_index(1);
94     assert_eq!(element.tab_index(), 1, "Should be tab_index");
95 
96     // TODO add a focus handler here
97     assert_eq!(element.focus().unwrap(), (), "No result");
98 
99     // TODO add a blur handler here
100     assert_eq!(element.blur().unwrap(), (), "No result");
101 
102     assert_eq!(element.access_key(), "", "Shouldn't have a access_key");
103     element.set_access_key("a");
104     assert_eq!(element.access_key(), "a", "Should have a access_key");
105 
106     // TODO add test for access_key_label
107 
108     assert!(!element.draggable(), "Shouldn't be draggable");
109     element.set_draggable(true);
110     assert!(element.draggable(), "Should be draggable");
111 
112     assert_eq!(
113         element.content_editable(),
114         "inherit",
115         "Shouldn't have a content_editable"
116     );
117     element.set_content_editable("true");
118     assert_eq!(
119         element.content_editable(),
120         "true",
121         "Should be content_editable"
122     );
123     assert!(element.is_content_editable(), "Should be content_editable");
124 
125     /*TODO doesn't work in Chrome
126         // TODO verify case where menu is passed
127         match element.context_menu() {
128             None => assert!(true, "Shouldn't have a custom menu set"),
129             _ => assert!(false, "Shouldn't have a custom menu set")
130         };
131     */
132 
133     // TODO: This test is also broken in Chrome (but not Firefox).
134     // assert!(!element.spellcheck(), "Shouldn't be spellchecked");
135     element.set_spellcheck(true);
136     assert!(element.spellcheck(), "Should be dragspellcheckedgable");
137 
138     // TODO verify case where we have an offset_parent
139     match element.offset_parent() {
140         None => assert!(true, "Shouldn't have an offset_parent set"),
141         _ => assert!(false, "Shouldn't have a offset_parent set"),
142     };
143 
144     // TODO verify when we have offsets
145     assert_eq!(element.offset_top(), 0, "Shouldn't have an offset_top yet");
146     assert_eq!(
147         element.offset_left(),
148         0,
149         "Shouldn't have an offset_left yet"
150     );
151     assert_eq!(
152         element.offset_width(),
153         0,
154         "Shouldn't have an offset_width yet"
155     );
156     assert_eq!(
157         element.offset_height(),
158         0,
159         "Shouldn't have an offset_height yet"
160     );
161 }
162