Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza1311 committed Aug 8, 2022
1 parent 2177f8c commit 27efd0a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/yew/src/dom_bundle/btag/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,52 @@ impl Apply for Attributes {
}
}
}

#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use super::*;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use gloo::utils::document;
use js_sys::Reflect;

wasm_bindgen_test_configure!(run_in_browser);

fn create_element() -> (Element, BSubtree) {
let element = document().create_element("a").expect("failed to create element");
let btree = BSubtree::create_root(&element);
(element, btree)
}

#[test]
fn properties_are_set() {
let attrs = Attributes::Static(&[
["href", "https://example.com/"],
["alt", "somewhere"],
]);
let (element, btree) = create_element();
attrs.apply(&btree, &element);
assert_eq!(
Reflect::get(element.as_ref(), &JsValue::from_str("href")).expect("no href").as_string().expect("not a string"),
"https://example.com/",
"property `href` not set properly"
);
assert_eq!(
Reflect::get(element.as_ref(), &JsValue::from_str("alt")).expect("no alt").as_string().expect("not a string"),
"somewhere",
"property `alt` not set properly"
);
}

#[test]
fn class_id_are_attrs() {
let attrs = Attributes::Static(&[
["id", "foo"],
["class", "thing"],
]);
let (element, btree) = create_element();
attrs.apply(&btree, &element);
assert_eq!(element.get_attribute("id").unwrap(), "foo");
assert_eq!(element.get_attribute("class").unwrap(), "thing");
}
}

0 comments on commit 27efd0a

Please sign in to comment.