Skip to content

Commit

Permalink
add tests for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jetli committed Jul 21, 2023
1 parent 522736e commit 25a864c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ jobs:
run: |
cargo fmt --all -- --check
cargo clippy -- --deny=warnings
- name: Run tests
- name: Run hooks tests
run: |
cd crates/yew-hooks && wasm-pack test --headless --chrome
- name: Run examples tests
run: |
cd examples/yew-app && wasm-pack test --headless --chrome
- name: Build examples
Expand Down
1 change: 1 addition & 0 deletions crates/yew-hooks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ features = [

[dev-dependencies]
log = "0.4.8"
wasm-bindgen-test = "0.3.14"
15 changes: 15 additions & 0 deletions crates/yew-hooks/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(dead_code)]

pub fn obtain_result() -> String {
gloo::utils::document()
.get_element_by_id("result")
.expect("No result found. Most likely, the application crashed and burned")
.inner_html()
}

pub fn obtain_result_by_id(id: &str) -> String {
gloo::utils::document()
.get_element_by_id(id)
.expect("No result found. Most likely, the application crashed and burned")
.inner_html()
}
38 changes: 38 additions & 0 deletions crates/yew-hooks/tests/use_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::time::Duration;
use wasm_bindgen_test::*;
use yew::platform::time::sleep;
use yew::prelude::*;

mod common;

use common::obtain_result;

wasm_bindgen_test_configure!(run_in_browser);

use yew_hooks::use_counter;

#[wasm_bindgen_test]
async fn use_counter_works() {
#[function_component]
fn TestComponent() -> Html {
let counter = use_counter(0);
if *counter < 5 {
counter.increase();
}
html! {
<div>
{"Test Output: "}
<div id="result">{*counter}</div>
{"\n"}
</div>
}
}

yew::Renderer::<TestComponent>::with_root(
gloo::utils::document().get_element_by_id("output").unwrap(),
)
.render();
sleep(Duration::ZERO).await;
let result = obtain_result();
assert_eq!(result.as_str(), "5");
}

0 comments on commit 25a864c

Please sign in to comment.