mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* Make a use_hook hook with the new Hook trait. * Implement Lifetime. * Rewrites function signature. * Only apply lifetime if there're other lifetimes. * Cleanup signature rewrite logic. * Rewrite hook body. * Port some built-in hooks. * Finish porting all built-in hooks. * Port tests. * Fix tests. * Migrate to macro-based hooks. * Fix HookContext, add tests on non-possible locations. * Fix stderr for trybuild. * Add 1 more test case. * Adjust doc location. * Pretty print hook signature. * Fix Items & std::ops::Fn*. * Add use_memo. * Optimise Implementation of hooks. * Use Box to capture function value only. * Detect whether needs boxing. * Add args if boxing not needed. * Enforce hook number. * Deduplicate use_effect. * Optimise Implementation. * Update documentation. * Fix website test. Strip BoxedHook implementation from it. * Allow doc string. * Workaround doc tests. * Optimise codebase & documentation. * Fix website test. * Reduce implementation complexity. * Destructor is no more. * Documentation and macros. * Reduce heap allocation and hook complexity. * Remove Queue as well. * Prefer Generics. * Fix typo. * Remove more allocations. * Add comments. * Remove outdated comment. * Bare Function Pointer for better code size.
101 lines
2.6 KiB
Rust
101 lines
2.6 KiB
Rust
mod common;
|
|
|
|
use common::obtain_result;
|
|
use wasm_bindgen_test::*;
|
|
use yew::prelude::*;
|
|
|
|
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
#[wasm_bindgen_test]
|
|
fn use_state_works() {
|
|
#[function_component(UseComponent)]
|
|
fn use_state_comp() -> Html {
|
|
let counter = use_state(|| 0);
|
|
if *counter < 5 {
|
|
counter.set(*counter + 1)
|
|
}
|
|
html! {
|
|
<div>
|
|
{"Test Output: "}
|
|
<div id="result">{*counter}</div>
|
|
{"\n"}
|
|
</div>
|
|
}
|
|
}
|
|
|
|
yew::start_app_in_element::<UseComponent>(
|
|
gloo_utils::document().get_element_by_id("output").unwrap(),
|
|
);
|
|
let result = obtain_result();
|
|
assert_eq!(result.as_str(), "5");
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn multiple_use_state_setters() {
|
|
#[function_component(UseComponent)]
|
|
fn use_state_comp() -> Html {
|
|
let counter = use_state(|| 0);
|
|
let counter_clone = counter.clone();
|
|
use_effect_with_deps(
|
|
move |_| {
|
|
// 1st location
|
|
counter_clone.set(*counter_clone + 1);
|
|
|| {}
|
|
},
|
|
(),
|
|
);
|
|
let another_scope = {
|
|
let counter = counter.clone();
|
|
move || {
|
|
if *counter < 11 {
|
|
// 2nd location
|
|
counter.set(*counter + 10)
|
|
}
|
|
}
|
|
};
|
|
another_scope();
|
|
html! {
|
|
<div>
|
|
{ "Test Output: " }
|
|
// expected output
|
|
<div id="result">{ *counter }</div>
|
|
{ "\n" }
|
|
</div>
|
|
}
|
|
}
|
|
|
|
yew::start_app_in_element::<UseComponent>(
|
|
gloo_utils::document().get_element_by_id("output").unwrap(),
|
|
);
|
|
let result = obtain_result();
|
|
assert_eq!(result.as_str(), "11");
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn use_state_eq_works() {
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
static RENDER_COUNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
#[function_component(UseComponent)]
|
|
fn use_state_comp() -> Html {
|
|
RENDER_COUNT.fetch_add(1, Ordering::Relaxed);
|
|
let counter = use_state_eq(|| 0);
|
|
counter.set(1);
|
|
|
|
html! {
|
|
<div>
|
|
{"Test Output: "}
|
|
<div id="result">{*counter}</div>
|
|
{"\n"}
|
|
</div>
|
|
}
|
|
}
|
|
|
|
yew::start_app_in_element::<UseComponent>(
|
|
gloo_utils::document().get_element_by_id("output").unwrap(),
|
|
);
|
|
let result = obtain_result();
|
|
assert_eq!(result.as_str(), "1");
|
|
assert_eq!(RENDER_COUNT.load(Ordering::Relaxed), 2);
|
|
}
|