Skip to content

Commit

Permalink
Add more pretty DOM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Sep 9, 2024
1 parent 2ede850 commit 7fa029f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/dom/src/pretty_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ fn filter_comments_and_default_ignore_tags_tags(node: &Node) -> bool {
}

pub fn pretty_dom(dom: Option<DocumentOrElement>, max_length: Option<usize>) -> String {
let dom = dom.unwrap_or_else(|| get_document().into());
let dom = dom.unwrap_or_else(|| {
get_document()
.body()
.expect("Body should exist.")
.unchecked_into::<Element>()
.into()
});
let max_length = max_length.unwrap_or(7000);

if max_length == 0 {
Expand Down
50 changes: 50 additions & 0 deletions packages/dom/tests/pretty_dom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod helpers;

use helpers::test_utils::render_into_document;
use indoc::indoc;
use testing_library_dom::pretty_dom;
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
Expand All @@ -22,3 +23,52 @@ fn pretty_dom_prints_out_the_given_dom_element_tree() {
pretty_dom(Some(container.into()), None)
);
}

#[wasm_bindgen_test]
fn pretty_dom_supports_truncating_the_output_length() {
let RenderReturn { container, .. } = render("<div>Hello World!</div>", None);

assert_eq!(
"<div>...",
pretty_dom(Some(container.clone().into()), Some(5))
);
assert_eq!("", pretty_dom(Some(container.clone().into()), Some(0)));
assert_eq!(
indoc! {"
<div>
<div>
Hello World!
</div>
</div>"},
pretty_dom(Some(container.into()), Some(usize::MAX))
);
}

#[ignore = "`wasm-pack test` crashes when its `pre` elements are overwritten."]
#[wasm_bindgen_test]
fn pretty_dom_defaults_to_document_body() {
render_into_document("<div>Hello World!</div>");

assert_eq!(
indoc! {"
<body>
<div>
Hello World!
</div>
</body>"},
pretty_dom(None, None)
);
}

#[ignore = "`wasm-pack test` crashes when its `pre` elements are overwritten."]
#[wasm_bindgen_test]
fn pretty_dom_supports_receiving_the_document_element() {
assert_eq!(
indoc! {"
<html>
<head />
<body />
</html>"},
pretty_dom(None, None)
);
}

0 comments on commit 7fa029f

Please sign in to comment.