dioxus/packages/core/tests/cycle.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2022-11-24 14:11:27 +00:00
use dioxus::core::{ElementId, Mutation::*};
use dioxus::prelude::*;
/// As we clean up old templates, the ID for the node should cycle
#[test]
fn cycling_elements() {
2024-01-11 21:18:11 +00:00
let mut dom = VirtualDom::new(|| match generation() % 2 {
2024-01-11 01:21:15 +00:00
0 => render! { div { "wasd" } },
1 => render! { div { "abcd" } },
_ => unreachable!(),
2022-11-24 14:11:27 +00:00
});
2022-12-09 22:18:37 +00:00
{
2024-01-11 01:21:15 +00:00
let edits = dom.rebuild_to_vec().santize();
2022-12-09 22:18:37 +00:00
assert_eq!(
edits.edits,
[
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
AppendChildren { m: 1, id: ElementId(0) },
]
);
}
2022-11-24 14:11:27 +00:00
dom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
ReplaceWith { id: ElementId(1,), m: 1 },
]
);
// notice that the IDs cycle back to ElementId(1), preserving a minimal memory footprint
dom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
ReplaceWith { id: ElementId(2,), m: 1 },
]
);
dom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
ReplaceWith { id: ElementId(1,), m: 1 },
]
);
}