dioxus/packages/core/tests/attr_cleanup.rs
Jonathan Kelley 0de3bf7aeb
Fix nested rsx expansion by not using template titles (#2799)
* Fix nested rsx expansion by not using template titles

* fix writers with nameless templates

* fix clippy

* dont commit vscode fix

* fix release mode, pull out __template_name

* fix axum_desktop

* Fix core tests

* Make most fields of HotReloadedTemplate public for testing

* wip: formatting, compare all diff cases

* slightly smarter diffing for dynamic nodes

* add a comment about generic node diffing

* clean up mutations a bit

* fix load template

* simplify maybe_rebuild

* Restore write mutations flag in web

* write_mutations -> skip_mutations

---------

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
2024-08-13 01:59:04 +00:00

83 lines
2.4 KiB
Rust

//! dynamic attributes in dioxus necessitate an allocated node ID.
//!
//! This tests to ensure we clean it up
use dioxus::dioxus_core::{ElementId, Mutation::*};
use dioxus::prelude::*;
#[test]
fn attrs_cycle() {
tracing_subscriber::fmt::init();
let mut dom = VirtualDom::new(|| {
let id = generation();
match id % 2 {
0 => rsx! { div {} },
1 => rsx! {
div { h1 { class: "{id}", id: "{id}" } }
},
_ => unreachable!(),
}
});
assert_eq!(
dom.rebuild_to_vec().edits,
[
LoadTemplate { index: 0, id: ElementId(1,) },
AppendChildren { m: 1, id: ElementId(0) },
]
);
dom.mark_dirty(ScopeId::APP);
assert_eq!(
dom.render_immediate_to_vec().edits,
[
LoadTemplate { index: 0, id: ElementId(2,) },
AssignId { path: &[0,], id: ElementId(3,) },
SetAttribute { name: "class", value: "1".into_value(), id: ElementId(3,), ns: None },
SetAttribute { name: "id", value: "1".into_value(), id: ElementId(3,), ns: None },
ReplaceWith { id: ElementId(1,), m: 1 },
]
);
dom.mark_dirty(ScopeId::APP);
assert_eq!(
dom.render_immediate_to_vec().edits,
[
LoadTemplate { index: 0, id: ElementId(1) },
ReplaceWith { id: ElementId(2), m: 1 }
]
);
dom.mark_dirty(ScopeId::APP);
assert_eq!(
dom.render_immediate_to_vec().edits,
[
LoadTemplate { index: 0, id: ElementId(2) },
AssignId { path: &[0], id: ElementId(3) },
SetAttribute {
name: "class",
value: dioxus_core::AttributeValue::Text("3".to_string()),
id: ElementId(3),
ns: None
},
SetAttribute {
name: "id",
value: dioxus_core::AttributeValue::Text("3".to_string()),
id: ElementId(3),
ns: None
},
ReplaceWith { id: ElementId(1), m: 1 }
]
);
// we take the node taken by attributes since we reused it
dom.mark_dirty(ScopeId::APP);
assert_eq!(
dom.render_immediate_to_vec().edits,
[
LoadTemplate { index: 0, id: ElementId(1) },
ReplaceWith { id: ElementId(2), m: 1 }
]
);
}