Skip to content

Commit a334ce4

Browse files
Fix TODOs in web-sys tests (#3449)
1 parent 4e6dcbe commit a334ce4

File tree

8 files changed

+84
-59
lines changed

8 files changed

+84
-59
lines changed

crates/web-sys/tests/wasm/button_element.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use wasm_bindgen::prelude::*;
22
use wasm_bindgen_test::*;
3-
use web_sys::{HtmlButtonElement, HtmlFormElement, Node};
3+
use web_sys::{HtmlButtonElement, HtmlFormElement};
44

55
#[wasm_bindgen(module = "/tests/wasm/element.js")]
66
extern "C" {
@@ -99,15 +99,9 @@ fn test_button_element_in_form() {
9999
let form = new_form();
100100
form.set_name("test-form");
101101

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();
102+
form.append_child(&button).unwrap();
108103

109-
let element = HtmlButtonElement::from(button);
110-
match element.form() {
104+
match button.form() {
111105
None => assert!(false, "Should have a form"),
112106
Some(form) => {
113107
assert!(true, "Should have a form");
@@ -118,5 +112,5 @@ fn test_button_element_in_form() {
118112
);
119113
}
120114
};
121-
assert_eq!(element.type_(), "reset", "Should have a type");
115+
assert_eq!(button.type_(), "reset", "Should have a type");
122116
}

crates/web-sys/tests/wasm/element.js

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export function new_select_with_food_opts() {
116116

117117
for(let i = 0; i < opts.length; i++) {
118118
let opt = document.createElement("option");
119+
opt.id = opts[i];
119120
opt.value = opts[i];
120121
opt.text = opts[i];
121122
select.appendChild(opt);

crates/web-sys/tests/wasm/element.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,26 @@ fn element() {
5757
get_attribute_ns
5858
*/
5959

60-
/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only)
61-
// TODO toggle_attribute should permit a single argument when optional arguments are supported
62-
assert!(!element.has_attribute("disabled"), "Should not be disabled");
63-
assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set");
64-
assert!(element.has_attribute("disabled"), "Should be disabled");
65-
assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set");
66-
assert!(!element.has_attribute("disabled"), "Should not be disabled");
67-
*/
60+
assert!(!element.has_attribute("disabled"), "Should not be disabled");
61+
assert!(
62+
element.toggle_attribute("disabled").unwrap(),
63+
"Should return true when attribute is set"
64+
);
65+
assert!(element.has_attribute("disabled"), "Should be disabled");
66+
assert!(
67+
element
68+
.toggle_attribute_with_force("disabled", true)
69+
.unwrap(),
70+
"Should return true when attribute is set"
71+
);
72+
assert!(element.has_attribute("disabled"), "Should be disabled");
73+
assert!(
74+
!element
75+
.toggle_attribute_with_force("disabled", false)
76+
.unwrap(),
77+
"Should return false when attribute is not set"
78+
);
79+
assert!(!element.has_attribute("disabled"), "Should not be disabled");
6880

6981
assert!(!element.has_attribute("title"), "Should not have a title");
7082
assert_eq!(
@@ -73,7 +85,11 @@ fn element() {
7385
"Should return nothing if set correctly"
7486
);
7587
assert!(element.has_attribute("title"), "Should have a title");
76-
// TODO check get_attribute here when supported
88+
assert_eq!(
89+
element.get_attribute("title").unwrap(),
90+
"boop",
91+
"Title should be 'boop'"
92+
);
7793
assert_eq!(
7894
element.remove_attribute("title").unwrap(),
7995
(),
@@ -129,8 +145,6 @@ fn element() {
129145
"Should return nothing if removed"
130146
);
131147

132-
// TODO non standard moz_matches_selector should we even support?
133-
134148
/* Tests needed for:
135149
insert_adjacent_element
136150
insert_adjacent_text

crates/web-sys/tests/wasm/html_element.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ fn test_html_element() {
121121
);
122122
assert!(element.is_content_editable(), "Should be content_editable");
123123

124-
// TODO: This test is also broken in Chrome (but not Firefox).
125-
// assert!(!element.spellcheck(), "Shouldn't be spellchecked");
124+
element.set_spellcheck(false);
125+
assert!(!element.spellcheck(), "Shouldn't be spellchecked");
126126
element.set_spellcheck(true);
127-
assert!(element.spellcheck(), "Should be dragspellcheckedgable");
127+
assert!(element.spellcheck(), "Should be spellchecked");
128128

129129
// TODO verify case where we have an offset_parent
130130
match element.offset_parent() {

crates/web-sys/tests/wasm/input_element.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ fn test_input_element() {
4040
element.set_default_checked(true);
4141
assert!(element.default_checked(), "Should have an default_checked");
4242

43-
/*TODO fix
44-
assert!(!element.checked(), "Shouldn't be checked");
45-
element.set_checked(true);
46-
assert!(element.checked(), "Should be checked");
47-
*/
43+
assert!(element.checked(), "Should be checked");
44+
element.set_checked(false);
45+
assert!(!element.checked(), "Shouldn't be checked");
4846

4947
assert!(!element.disabled(), "Shouldn't be disabled");
5048
element.set_disabled(true);
@@ -93,14 +91,18 @@ fn test_input_element() {
9391

9492
assert_eq!(element.height(), 0, "Should have no height");
9593
element.set_height(12);
96-
assert_eq!(element.height(), 0, "Should have no height"); // Doesn't change, TODO check with get_attribute("height")=="12"
94+
assert_eq!(element.height(), 0, "Should have no height");
95+
assert_eq!(
96+
element.get_attribute("height").unwrap(),
97+
"12",
98+
"The height attribute should be 12"
99+
);
97100

98-
/*TODO fails in chrome
99101
element.set_type("checkbox");
102+
element.set_indeterminate(true);
100103
assert!(element.indeterminate(), "Should be indeterminate");
101-
element.set_checked(true);
104+
element.set_indeterminate(false);
102105
assert!(!element.indeterminate(), "Shouldn't be indeterminate");
103-
*/
104106
/*TODO add tests
105107
pub fn indeterminate(&self) -> bool
106108
pub fn set_indeterminate(&self, indeterminate: bool)
@@ -144,12 +146,11 @@ fn test_input_element() {
144146
pub fn size(&self) -> u32
145147
pub fn set_size(&self, size: u32)
146148
*/
147-
/*TODO fails in chrome
148-
element.set_type("image");
149-
assert_eq!(element.src(), "", "Should have no src");
150-
element.set_value("hey.png");
151-
assert_eq!(element.src(), "hey.png", "Should have a src");
152-
*/
149+
element.set_type("image");
150+
assert_eq!(element.src(), "", "Should have no src");
151+
const EMPTY_IMAGE: &str = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E";
152+
element.set_src(EMPTY_IMAGE);
153+
assert_eq!(element.src(), EMPTY_IMAGE, "Should have a src");
153154
/*TODO add tests
154155
pub fn src(&self) -> String
155156
pub fn set_src(&self, src: &str)
@@ -160,11 +161,9 @@ fn test_input_element() {
160161
pub fn default_value(&self) -> String
161162
pub fn set_default_value(&self, default_value: &str)
162163
*/
163-
/*TODO fails in chrome
164-
assert_eq!(element.value(), "", "Should have no value");
165-
element.set_value("hey!");
166-
assert_eq!(element.value(), "hey!", "Should have a value");
167-
*/
164+
assert_eq!(element.value(), "", "Should have no value");
165+
element.set_value("hey!");
166+
assert_eq!(element.value(), "hey!", "Should have a value");
168167
element.set_type("number");
169168
element.set_value("1");
170169
assert_eq!(element.value_as_number(), 1.0, "Should have value 1");
@@ -173,7 +172,12 @@ fn test_input_element() {
173172

174173
assert_eq!(element.width(), 0, "Should have no width");
175174
element.set_width(12);
176-
assert_eq!(element.width(), 0, "Should have no width"); // Doesn't change, TODO check with get_attribute("width")=="12"
175+
assert_eq!(element.width(), 0, "Should have no width");
176+
assert_eq!(
177+
element.get_attribute("width").unwrap(),
178+
"12",
179+
"The width attribute should be 12"
180+
);
177181

178182
assert_eq!(element.will_validate(), false, "Shouldn't validate");
179183
assert_eq!(

crates/web-sys/tests/wasm/output_element.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ fn test_output_element() {
4242
output.set_value("49");
4343
assert_eq!(output.value(), "49", "Output value should be '49'.");
4444

45-
// TODO: Fails in Chrome, but not in Firefox.
46-
//assert!(output.will_validate(), "Output should validate by default (maybe browser dependent?)");
45+
assert!(
46+
!output.will_validate(),
47+
"Output is not a submittable element, so willValidate must be false"
48+
);
4749

4850
assert!(
4951
output.validity().valid(),

crates/web-sys/tests/wasm/select_element.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ fn test_select_element() {
2626
"Select element should have a false autofocus property."
2727
);
2828

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.");
29+
select.set_autocomplete("country");
30+
assert_eq!(
31+
select.autocomplete(),
32+
"country",
33+
"Select element should have a 'country' autocomplete property."
34+
);
3235

3336
select.set_disabled(true);
3437
assert_eq!(
@@ -173,9 +176,13 @@ fn test_select_element() {
173176
"There should be no labels associated with our select element."
174177
);
175178

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.
179+
assert!(
180+
select.named_item("tomato").is_some(),
181+
"Should be able to find the 'tomato' option before removing it."
182+
);
183+
select.remove_with_index(0);
184+
assert!(
185+
select.named_item("tomato").is_none(),
186+
"Shouldn't be able to find the 'tomato' option after removing it."
187+
);
181188
}

crates/web-sys/tests/wasm/slot_element.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ extern "C" {
99

1010
#[wasm_bindgen_test]
1111
fn test_slot_element() {
12-
let _slot = new_slot();
13-
// TODO: Test fails in Firefox, but not in Chrome. Error in Firefox is 'ReferenceError: HTMLSlotElement is not defined'. https://w3c-test.org/shadow-dom/HTMLSlotElement-interface.html
14-
// slot.set_name("root_separator");
15-
// assert_eq!(slot.name(), "root_separator", "Slot name should 'root_separator'.");
12+
let slot = new_slot();
13+
slot.set_name("root_separator");
14+
assert_eq!(
15+
slot.name(),
16+
"root_separator",
17+
"Slot name should 'root_separator'."
18+
);
1619
}

0 commit comments

Comments
 (0)