summaryrefslogtreecommitdiffstats
path: root/vendor/web-sys/tests/wasm/button_element.rs
blob: d94160aace7c1ef285793f02de24b6c43f5bf08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use web_sys::{HtmlButtonElement, HtmlFormElement, Node};

#[wasm_bindgen(module = "/tests/wasm/element.js")]
extern "C" {
    fn new_button() -> HtmlButtonElement;
    fn new_form() -> HtmlFormElement;
}

#[wasm_bindgen_test]
fn test_button_element() {
    let element = new_button();
    let location = web_sys::window().unwrap().location().href().unwrap();
    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");
}