Skip to content

Commit

Permalink
Restore test.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Apr 21, 2022
1 parent b20b266 commit 2cb70db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
29 changes: 28 additions & 1 deletion packages/yew/src/dom_bundle/bcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod feat_hydration {
#[cfg(test)]
mod tests {
use super::*;
use crate::dom_bundle::{Reconcilable, ReconcileTarget};
use crate::dom_bundle::{Bundle, Reconcilable, ReconcileTarget};
use crate::scheduler;
use crate::{
html,
Expand Down Expand Up @@ -442,6 +442,33 @@ mod tests {
scheduler::start_now();
assert!(node_ref.get().is_none());
}

#[test]
fn reset_ancestors_node_ref() {
let (root, scope, parent) = setup_parent();

let mut bundle = Bundle::new();
let node_ref_a = NodeRef::default();
let node_ref_b = NodeRef::default();
let elem = html! { <Comp ref={node_ref_a.clone()}></Comp> };
let node_a = bundle.reconcile(&root, &scope, &parent, NodeRef::default(), elem);
scheduler::start_now();
let node_a = node_a.get().unwrap();

assert!(node_ref_a.get().is_some(), "node_ref_a should be bound");

let elem = html! { <Comp ref={node_ref_b.clone()}></Comp> };
let node_b = bundle.reconcile(&root, &scope, &parent, NodeRef::default(), elem);
scheduler::start_now();
let node_b = node_b.get().unwrap();

assert_eq!(node_a, node_b, "Comp should have reused the element");
assert!(node_ref_b.get().is_some(), "node_ref_b should be bound");
assert!(
node_ref_a.get().is_none(),
"node_ref_a should have been reset when the element was reused."
);
}
}

#[cfg(feature = "wasm_test")]
Expand Down
10 changes: 8 additions & 2 deletions packages/yew/src/html/component/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ impl Runnable for PropsUpdateRunner {
..
} => {
// When components are updated, a new node ref could have been passed in
*node_ref = next_node_ref;
if *node_ref != next_node_ref {
node_ref.set(None);
*node_ref = next_node_ref;
}
// When components are updated, their siblings were likely also updated
*current_next_sibling = next_sibling;
// Only trigger changed if props were changed
Expand All @@ -343,7 +346,10 @@ impl Runnable for PropsUpdateRunner {
..
} => {
// When components are updated, a new node ref could have been passed in
*node_ref = next_node_ref;
if *node_ref != next_node_ref {
node_ref.set(None);
*node_ref = next_node_ref;
}
// When components are updated, their siblings were likely also updated
*current_next_sibling = next_sibling;
// Only trigger changed if props were changed
Expand Down

0 comments on commit 2cb70db

Please sign in to comment.