|
| 1 | +use super::websys_project; |
| 2 | + |
| 3 | +#[test] |
| 4 | +fn element() { |
| 5 | + websys_project() |
| 6 | + .file( |
| 7 | + "src/lib.rs", |
| 8 | + r#" |
| 9 | + #![feature(use_extern_macros, wasm_custom_section)] |
| 10 | + extern crate wasm_bindgen; |
| 11 | + use wasm_bindgen::prelude::*; |
| 12 | + extern crate web_sys; |
| 13 | +
|
| 14 | + #[wasm_bindgen] |
| 15 | + pub fn test_element(element: &web_sys::Element) { |
| 16 | + assert_eq!(element.local_name(), "div", "Shouldn't have a div local name"); |
| 17 | + assert_eq!(element.tag_name(), "div", "Should be a div tag"); |
| 18 | + assert!(!element.has_attribute("id"), "Shouldn't have an id"); |
| 19 | + element.set_id("beep"); |
| 20 | + assert_eq!(element.id(), "beep", "Should have an id of 'beep'"); |
| 21 | +
|
| 22 | + // must_use is set on this result? |
| 23 | + assert_eq!(element.set_attribute("id", "beep").unwrap(), (), "Should set id"); |
| 24 | + assert!(element.has_attribute("id"), "Should now have an id"); |
| 25 | + assert_eq!(element.remove_attribute("id").unwrap(), (), "Should return nothing if removed"); |
| 26 | +
|
| 27 | + assert_eq!(element.class_name(), "", "Shouldn't have a class name"); |
| 28 | + element.set_class_name("test thing"); |
| 29 | + assert_eq!(element.class_name(), "test thing", "Should have a class name"); |
| 30 | + assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); |
| 31 | +
|
| 32 | +/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only) |
| 33 | + // TODO toggle_attribute should permit a single argument when optional arguments are supported |
| 34 | + assert!(!element.has_attribute("disabled"), "Should not be disabled"); |
| 35 | + assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set"); |
| 36 | + assert!(element.has_attribute("disabled"), "Should be disabled"); |
| 37 | + assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set"); |
| 38 | + assert!(!element.has_attribute("disabled"), "Should not be disabled"); |
| 39 | +*/ |
| 40 | +
|
| 41 | + assert!(!element.has_attribute("title"), "Should not have a title"); |
| 42 | + assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); |
| 43 | + assert!(element.has_attribute("title"), "Should have a title"); |
| 44 | + // TODO check get_attribute here when supported |
| 45 | + assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); |
| 46 | + assert!(!element.has_attribute("title"), "Should not have a title"); |
| 47 | +
|
| 48 | + assert!(!element.has_attributes(), "Should not have any attributes"); |
| 49 | + assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); |
| 50 | + assert!(element.has_attributes(), "Should have attributes"); |
| 51 | + assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); |
| 52 | +
|
| 53 | + assert_eq!(element.matches(".this-is-a-thing").unwrap(), false, "Should not match selector"); |
| 54 | + assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), false, "Should not match selector"); |
| 55 | + element.set_class_name("this-is-a-thing"); |
| 56 | + assert_eq!(element.matches(".this-is-a-thing").unwrap(), true, "Should match selector"); |
| 57 | + assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), true, "Should match selector"); |
| 58 | + assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); |
| 59 | +
|
| 60 | +
|
| 61 | +// TODO non standard moz_matches_selector should we even support? |
| 62 | +/* Tests needed for: |
| 63 | + insert_adjacent_text |
| 64 | + set_pointer_capture |
| 65 | + release_pointer_capture |
| 66 | + has_pointer_capture |
| 67 | + set_capture |
| 68 | + release_capture |
| 69 | +*/ |
| 70 | + } |
| 71 | + "#, |
| 72 | + ) |
| 73 | + .file( |
| 74 | + "test.js", |
| 75 | + r#" |
| 76 | + import * as assert from "assert"; |
| 77 | + import * as wasm from "./out"; |
| 78 | +
|
| 79 | + export function test() { |
| 80 | + let document = new Document(); |
| 81 | + let el = document.createElement("div"); |
| 82 | + wasm.test_element(el); |
| 83 | + } |
| 84 | + "#, |
| 85 | + ) |
| 86 | + .test(); |
| 87 | +} |
0 commit comments