1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen_test::*;
3 use web_sys::{HtmlButtonElement, HtmlFormElement, Node};
4
5 #[wasm_bindgen(module = "/tests/wasm/element.js")]
6 extern "C" {
new_button() -> HtmlButtonElement7 fn new_button() -> HtmlButtonElement;
new_form() -> HtmlFormElement8 fn new_form() -> HtmlFormElement;
9 }
10
11 #[wasm_bindgen_test]
test_button_element()12 fn test_button_element() {
13 let element = new_button();
14 let location = web_sys::window().unwrap().location().href().unwrap();
15 assert!(!element.autofocus(), "Shouldn't have autofocus");
16 element.set_autofocus(true);
17 assert!(element.autofocus(), "Should have autofocus");
18
19 assert!(!element.disabled(), "Shouldn't be disabled");
20 element.set_disabled(true);
21 assert!(element.disabled(), "Should be disabled");
22
23 match element.form() {
24 None => assert!(true, "Shouldn't have a form"),
25 _ => assert!(false, "Shouldn't have a form"),
26 };
27
28 assert_eq!(
29 element.form_action(),
30 location,
31 "Should have the pages location"
32 );
33 element.set_form_action("http://boop.com/");
34 assert_eq!(
35 element.form_action(),
36 "http://boop.com/",
37 "Should have a form_action"
38 );
39
40 assert_eq!(element.form_enctype(), "", "Should have no enctype");
41 element.set_form_enctype("text/plain");
42 assert_eq!(
43 element.form_enctype(),
44 "text/plain",
45 "Should have a plain text enctype"
46 );
47
48 assert_eq!(element.form_method(), "", "Should have no method");
49 element.set_form_method("POST");
50 assert_eq!(element.form_method(), "post", "Should have a POST method");
51
52 assert!(!element.form_no_validate(), "Should validate");
53 element.set_form_no_validate(true);
54 assert!(element.form_no_validate(), "Should not validate");
55
56 assert_eq!(element.form_target(), "", "Should have no target");
57 element.set_form_target("_blank");
58 assert_eq!(
59 element.form_target(),
60 "_blank",
61 "Should have a _blank target"
62 );
63
64 assert_eq!(element.name(), "", "Shouldn't have a name");
65 element.set_name("button-name");
66 assert_eq!(element.name(), "button-name", "Should have a name");
67
68 assert_eq!(element.type_(), "submit", "Shouldn't have a type");
69 element.set_type("reset");
70 assert_eq!(element.type_(), "reset", "Should have a reset type");
71
72 assert_eq!(element.value(), "", "Shouldn't have a value");
73 element.set_value("value1");
74 assert_eq!(element.value(), "value1", "Should have a value");
75
76 assert_eq!(element.will_validate(), false, "Shouldn't validate");
77 assert_eq!(
78 element.validation_message().unwrap(),
79 "",
80 "Shouldn't have a value"
81 );
82 assert_eq!(element.check_validity(), true, "Should be valid");
83 assert_eq!(element.report_validity(), true, "Should be valid");
84 element.set_custom_validity("Boop"); // Method exists but doesn't impact validity
85 assert_eq!(element.check_validity(), true, "Should be valid");
86 assert_eq!(element.report_validity(), true, "Should be valid");
87
88 assert_eq!(
89 element.labels().length(),
90 0,
91 "Should return a node list with no elements"
92 );
93 }
94
95 #[wasm_bindgen_test]
test_button_element_in_form()96 fn test_button_element_in_form() {
97 let button = new_button();
98 button.set_type("reset");
99 let form = new_form();
100 form.set_name("test-form");
101
102 // TODO: implement `Clone` for types in `web_sys` to make this easier.
103 let button = JsValue::from(button);
104 let as_node = Node::from(button.clone());
105 Node::from(JsValue::from(form))
106 .append_child(&as_node)
107 .unwrap();
108
109 let element = HtmlButtonElement::from(button);
110 match element.form() {
111 None => assert!(false, "Should have a form"),
112 Some(form) => {
113 assert!(true, "Should have a form");
114 assert_eq!(
115 form.name(),
116 "test-form",
117 "Form should have a name of test-form"
118 );
119 }
120 };
121 assert_eq!(element.type_(), "reset", "Should have a type");
122 }
123