1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen_test::*;
3 use web_sys::HtmlSelectElement;
4
5 #[wasm_bindgen(module = "/tests/wasm/element.js")]
6 extern "C" {
new_select_with_food_opts() -> HtmlSelectElement7 fn new_select_with_food_opts() -> HtmlSelectElement;
8 }
9
10 #[wasm_bindgen_test]
test_select_element()11 fn test_select_element() {
12 // Creates a select with four options. Options are ["tomato", "potato", "orange", "apple"], where
13 // the string is the .value and .text of each option.
14 let select = new_select_with_food_opts();
15 select.set_autofocus(true);
16 assert_eq!(
17 select.autofocus(),
18 true,
19 "Select element should have a true autofocus property."
20 );
21
22 select.set_autofocus(false);
23 assert_eq!(
24 select.autofocus(),
25 false,
26 "Select element should have a false autofocus property."
27 );
28
29 // TODO: This test currently fails on Firefox, but not Chrome. In Firefox, even though we select.set_autocomplete(), select.autocomplete() yields an empty String.
30 // select.set_autocomplete("tomato");
31 // assert_eq!(select.autocomplete(), "tomato", "Select element should have a 'tomato' autocomplete property.");
32
33 select.set_disabled(true);
34 assert_eq!(
35 select.disabled(),
36 true,
37 "Select element should be disabled."
38 );
39
40 select.set_disabled(false);
41 assert_eq!(
42 select.disabled(),
43 false,
44 "Select element should not be disabled."
45 );
46
47 assert!(
48 select.form().is_none(),
49 "Select should not be associated with a form."
50 );
51
52 select.set_multiple(false);
53 assert_eq!(
54 select.multiple(),
55 false,
56 "Select element should have a false multiple property."
57 );
58
59 select.set_multiple(true);
60 assert_eq!(
61 select.multiple(),
62 true,
63 "Select element should have a true multiple property."
64 );
65
66 select.set_name("potato");
67 assert_eq!(
68 select.name(),
69 "potato",
70 "Select element should have a name property of 'potato'."
71 );
72
73 select.set_required(true);
74 assert_eq!(
75 select.required(),
76 true,
77 "Select element should be required."
78 );
79
80 select.set_required(false);
81 assert_eq!(
82 select.required(),
83 false,
84 "Select element should not be required."
85 );
86
87 select.set_size(432);
88 assert_eq!(
89 select.size(),
90 432,
91 "Select element should have a size property of 432."
92 );
93
94 // Default type seems to be "select-multiple" for the browsers I tested, but there's no guarantee
95 // on this, so let's just make sure we get back something here.
96 assert!(
97 select.type_().len() > 0,
98 "Select element should have some type."
99 );
100
101 assert!(
102 select.options().length() == 4,
103 "Select element should have four options."
104 );
105
106 select.set_length(12);
107 assert_eq!(
108 select.length(),
109 12,
110 "Select element should have a length of 12."
111 );
112 // Reset the length to four, as that's how many options we actually have.
113 select.set_length(4);
114
115 assert!(
116 select
117 .named_item("this should definitely find nothing")
118 .is_none(),
119 "Shouldn't be able to find a named item with the given string."
120 );
121
122 assert!(
123 select.selected_options().length() == 1,
124 "One option should be selected by default, just by way of having items."
125 );
126
127 select.set_selected_index(2);
128 assert_eq!(
129 select.selected_index(),
130 2,
131 "Select element should have a selected index of 2."
132 );
133
134 // Quote from docs: The value property sets or returns the value of the selected option in a drop-down list.
135 select.set_value("tomato"); // Select the "tomato" option
136 assert_eq!(
137 select.value(),
138 "tomato",
139 "Select element should have no selected value."
140 );
141
142 // This might be browser dependent, potentially rendering this test useless? Worked fine in Chrome and Firefox for now.
143 assert_eq!(
144 select.will_validate(),
145 true,
146 "Select element should not validate by default."
147 );
148
149 assert!(
150 select.validation_message().is_ok(),
151 "Select element should retrieve a validation message."
152 );
153
154 assert!(
155 select.validity().valid(),
156 "Our basic select should be valid."
157 );
158
159 assert!(
160 select.check_validity(),
161 "Our basic select should check out as valid."
162 );
163
164 assert!(
165 select.report_validity(),
166 "Our basic select should report valid."
167 );
168
169 select.set_custom_validity("Some custom validity error.");
170
171 assert!(
172 select.labels().length() == 0,
173 "There should be no labels associated with our select element."
174 );
175
176 // TODO: This test won't work until this bug is fixed: https://www.w3.org/Bugs/Public/show_bug.cgi?id=20720. Sometime in the future, either remove this test or uncomment after bug is fixed.
177 // assert!(select.named_item("tomato").is_some(), "Should be able to find the 'tomato' option before removing it.");
178 // select.remove(0);
179 // assert!(select.named_item("tomato").is_none(), "Shouldn't be able to find the 'tomato' option after removing it.")
180 // TODO: As a result, we are missing a test for the remove() method.
181 }
182