-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3e4f70
commit 7d274c5
Showing
8 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use wasm_bindgen::JsCast; | ||
use web_sys::{Element, HtmlInputElement, Node}; | ||
|
||
use crate::util::node_list_to_vec; | ||
|
||
pub fn get_node_text(node: &Node) -> String { | ||
if node | ||
.dyn_ref::<Element>() | ||
.map(|element| { | ||
element | ||
.matches("input[type=submit], input[type=button], input[type=reset]") | ||
.is_ok_and(|value| value) | ||
}) | ||
.unwrap_or(false) | ||
{ | ||
node.unchecked_ref::<HtmlInputElement>().value() | ||
} else { | ||
node_list_to_vec::<Node>(node.child_nodes()) | ||
.into_iter() | ||
.filter_map(|child| { | ||
(child.node_type() == Node::TEXT_NODE) | ||
.then(|| child.text_content()) | ||
.flatten() | ||
}) | ||
.collect::<Vec<_>>() | ||
.join("") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
mod helpers; | ||
|
||
use helpers::test_utils::RenderReturn; | ||
use testing_library_dom::get_node_text; | ||
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; | ||
|
||
use self::helpers::test_utils::render; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[wasm_bindgen_test] | ||
fn it_prints_out_the_text_content_of_a_container() { | ||
let RenderReturn { container, .. } = render("Hello <!--skipped-->World!", None); | ||
|
||
assert_eq!("Hello World!", get_node_text(&container)); | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn it_prints_out_the_value_for_submit_and_button_inputs() { | ||
let RenderReturn { container, .. } = render( | ||
"<input type=\"submit\" value=\"save\"><input type=\"button\" value=\"reset\">", | ||
None, | ||
); | ||
|
||
assert_eq!( | ||
"save", | ||
get_node_text(&container.first_child().expect("First child should exist.")) | ||
); | ||
|
||
assert_eq!( | ||
"reset", | ||
get_node_text(&container.last_child().expect("Last child should exist.")) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod test_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use web_sys::{window, Document, Element}; | ||
|
||
pub fn document() -> Document { | ||
window() | ||
.expect("Window should exist.") | ||
.document() | ||
.expect("Document should exist.") | ||
} | ||
|
||
pub struct RenderReturn { | ||
pub container: Element, | ||
pub rerender: Box<dyn Fn(&str) -> RenderReturn>, | ||
} | ||
|
||
pub fn render(html: &str, container: Option<Element>) -> RenderReturn { | ||
let container = container.unwrap_or_else(|| { | ||
document() | ||
.create_element("div") | ||
.expect("Element should be created.") | ||
}); | ||
|
||
container.set_inner_html(html); | ||
|
||
// TODO: container_queries | ||
|
||
RenderReturn { | ||
container: container.clone(), | ||
rerender: Box::new(move |new_html| render(new_html, Some(container.clone()))), | ||
} | ||
} | ||
|
||
pub fn render_into_document(html: &str) -> RenderReturn { | ||
render( | ||
html, | ||
Some(document().body().expect("Body should exist.").into()), | ||
) | ||
} | ||
|
||
pub fn cleanup() { | ||
document() | ||
.body() | ||
.expect("Body should exist.") | ||
.set_inner_html(""); | ||
} |