1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-06-26 03:01:33 +00:00
Files
.cargo
crates
backend
cli
cli-support
futures
js-sys
macro
macro-support
shared
test
test-macro
typescript
wasm-interpreter
web-sys
src
tests
wasm
anchor_element.rs
body_element.rs
br_element.rs
button_element.rs
console.rs
div_element.rs
dom_point.rs
element.js
element.rs
event.js
event.rs
head_element.rs
headers.js
headers.rs
heading_element.rs
history.rs
hr_element.rs
html_element.rs
html_html_element.rs
input_element.rs
location.rs
main.rs
menu_element.rs
menu_item_element.rs
meta_element.rs
meter_element.rs
mod_elements.rs
olist_element.rs
optgroup_element.rs
option_element.rs
options_collection.rs
output_element.rs
paragraph_element.rs
param_element.rs
performance.rs
pre_element.rs
progress_element.rs
quote_element.rs
response.js
response.rs
script_element.rs
select_element.rs
slot_element.rs
span_element.rs
style_element.rs
table_element.rs
title_element.rs
xpath_result.rs
webidls
Cargo.toml
README.md
build.rs
webidl
webidl-tests
examples
guide
releases
src
tests
.appveyor.yml
.eslintignore
.eslintrc
.gitattributes
.gitignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md
build.rs
package.json
publish.rs
wasm-bindgen/crates/web-sys/tests/wasm/button_element.rs

105 lines
3.9 KiB
Rust
Raw Normal View History

use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*;
use web_sys::{HtmlButtonElement, HtmlFormElement, Node};
#[wasm_bindgen(module = "./tests/wasm/element.js")]
extern {
fn new_button() -> HtmlButtonElement;
fn new_form() -> HtmlFormElement;
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = location, js_namespace = document)]
static LOCATION: Location;
type Location;
// FIXME: `href` should be structural in `web_sys`
#[wasm_bindgen(getter, method, structural)]
fn href(this: &Location) -> String;
}
#[wasm_bindgen_test]
fn test_button_element() {
let element = new_button();
let location = LOCATION.href();
assert!(!element.autofocus(), "Shouldn't have autofocus");
element.set_autofocus(true);
assert!(element.autofocus(), "Should have autofocus");
assert!(!element.disabled(), "Shouldn't be disabled");
element.set_disabled(true);
assert!(element.disabled(), "Should be disabled");
match element.form() {
None => assert!(true, "Shouldn't have a form"),
_ => assert!(false, "Shouldn't have a form"),
};
assert_eq!(element.form_action(), location, "Should have the pages location");
element.set_form_action("http://boop.com/");
assert_eq!(element.form_action(), "http://boop.com/", "Should have a form_action");
assert_eq!(element.form_enctype(), "", "Should have no enctype");
element.set_form_enctype("text/plain");
assert_eq!(element.form_enctype(), "text/plain", "Should have a plain text enctype");
assert_eq!(element.form_method(), "", "Should have no method");
element.set_form_method("POST");
assert_eq!(element.form_method(), "post", "Should have a POST method");
assert!(!element.form_no_validate(), "Should validate");
element.set_form_no_validate(true);
assert!(element.form_no_validate(), "Should not validate");
assert_eq!(element.form_target(), "", "Should have no target");
element.set_form_target("_blank");
assert_eq!(element.form_target(), "_blank", "Should have a _blank target");
assert_eq!(element.name(), "", "Shouldn't have a name");
element.set_name("button-name");
assert_eq!(element.name(), "button-name", "Should have a name");
assert_eq!(element.type_(), "submit", "Shouldn't have a type");
element.set_type("reset");
assert_eq!(element.type_(), "reset", "Should have a reset type");
assert_eq!(element.value(), "", "Shouldn't have a value");
element.set_value("value1");
assert_eq!(element.value(), "value1", "Should have a value");
assert_eq!(element.will_validate(), false, "Shouldn't validate");
assert_eq!(element.validation_message().unwrap(), "", "Shouldn't have a value");
assert_eq!(element.check_validity(), true, "Should be valid");
assert_eq!(element.report_validity(), true, "Should be valid");
element.set_custom_validity("Boop"); // Method exists but doesn't impact validity
assert_eq!(element.check_validity(), true, "Should be valid");
assert_eq!(element.report_validity(), true, "Should be valid");
assert_eq!(element.labels().length(), 0, "Should return a node list with no elements");
}
#[wasm_bindgen_test]
fn test_button_element_in_form() {
let button = new_button();
button.set_type("reset");
let form = new_form();
form.set_name("test-form");
// TODO: implement `Clone` for types in `web_sys` to make this easier.
let button = JsValue::from(button);
let as_node = Node::from(button.clone());
Node::from(JsValue::from(form)).append_child(&as_node).unwrap();
let element = HtmlButtonElement::from(button);
match element.form() {
None => assert!(false, "Should have a form"),
Some(form) => {
assert!(true, "Should have a form");
assert_eq!(form.name(), "test-form", "Form should have a name of test-form");
},
};
assert_eq!(element.type_(), "reset", "Should have a type");
}