dioxus/packages/core/tests/attr_cleanup.rs

100 lines
2.8 KiB
Rust
Raw Normal View History

2022-11-27 05:22:39 +00:00
//! dynamic attributes in dioxus necessitate an allocated node ID.
//!
//! This tests to ensure we clean it up
2022-12-09 22:18:37 +00:00
use bumpalo::Bump;
2022-11-27 05:22:39 +00:00
use dioxus::core::{ElementId, Mutation::*};
use dioxus::prelude::*;
use dioxus_core::{AttributeValue, BorrowedAttributeValue};
2022-11-27 05:22:39 +00:00
#[test]
fn attrs_cycle() {
let mut dom = VirtualDom::new(|cx| {
let id = cx.generation();
match cx.generation() % 2 {
0 => cx.render(rsx! {
div {}
}),
1 => cx.render(rsx! {
div {
h1 { class: "{id}", id: "{id}" }
}
}),
_ => unreachable!(),
}
});
2022-12-09 22:18:37 +00:00
let bump = Bump::new();
2022-11-27 05:22:39 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.rebuild().santize().edits,
2022-11-27 05:22:39 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
2022-12-03 00:24:49 +00:00
AppendChildren { m: 1, id: ElementId(0) },
2022-11-27 05:22:39 +00:00
]
);
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
2022-11-27 05:22:39 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.render_immediate().santize().edits,
2022-11-27 05:22:39 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
AssignId { path: &[0,], id: ElementId(3,) },
2022-12-09 22:18:37 +00:00
SetAttribute {
name: "class",
value: (&*bump.alloc("1".into_value(&bump))).into(),
id: ElementId(3,),
ns: None
},
SetAttribute {
name: "id",
value: (&*bump.alloc("1".into_value(&bump))).into(),
2022-12-09 22:18:37 +00:00
id: ElementId(3,),
ns: None
},
2022-11-27 05:22:39 +00:00
ReplaceWith { id: ElementId(1,), m: 1 },
]
);
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
2022-11-27 05:25:35 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.render_immediate().santize().edits,
2022-11-27 05:22:39 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
2022-11-27 05:25:35 +00:00
ReplaceWith { id: ElementId(2), m: 1 }
2022-11-27 05:22:39 +00:00
]
);
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
2022-11-27 05:22:39 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.render_immediate().santize().edits,
2022-11-27 05:22:39 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(2) },
AssignId { path: &[0], id: ElementId(3) },
2022-12-23 22:10:05 +00:00
SetAttribute {
name: "class",
value: BorrowedAttributeValue::Text("3"),
2022-12-23 22:10:05 +00:00
id: ElementId(3),
ns: None
},
SetAttribute {
name: "id",
value: BorrowedAttributeValue::Text("3"),
2022-12-23 22:10:05 +00:00
id: ElementId(3),
ns: None
},
ReplaceWith { id: ElementId(1), m: 1 }
2022-11-27 05:22:39 +00:00
]
);
2022-11-27 05:25:35 +00:00
// we take the node taken by attributes since we reused it
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId(0));
2022-11-27 05:25:35 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
dom.render_immediate().santize().edits,
2022-11-27 05:25:35 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1) },
ReplaceWith { id: ElementId(2), m: 1 }
]
);
2022-11-27 05:22:39 +00:00
}