Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Feb 7, 2024
1 parent cbb2979 commit b3dbb29
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/js/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,51 @@ mod test {
assert_eq!(res, "p,p");
}

#[tokio::test]
async fn test_node_parent() {
let res = run_js(
r#"
const dom = DOM.parse_fragment("<div><p>hello</p></div>");
const [p] = dom.select('p');
p.parent().tag_name()
"#,
)
.await;

assert_eq!(res, "div");
}

#[tokio::test]
async fn test_node_siblings() {
let res = run_js(
r#"
const dom = DOM.parse_fragment("<div>1</div><p>2</p><br><span>3</span>");
const [p] = dom.select('p');
const prev = p.previous_sibling().tag_name();
const next = p.next_sibling().tag_name();
`${prev},${next}`
"#,
)
.await;

assert_eq!(res, "div,br");
}

#[tokio::test]
async fn test_node_remove() {
let res = run_js(
r#"
const dom = DOM.parse_fragment("<div><p>hello</p></div>");
const [p] = dom.select('p');
p.remove();
dom.to_html()
"#,
)
.await;

assert_eq!(res, "<div></div>");
}

async fn run_js(code: &str) -> String {
let rt = Runtime::new().await.unwrap();
rt.eval(code).await.unwrap()
Expand Down

0 comments on commit b3dbb29

Please sign in to comment.