dioxus/packages/core/tests/diff_element.rs

85 lines
2.2 KiB
Rust
Raw Normal View History

2022-11-23 04:16:14 +00:00
use dioxus::core::Mutation::*;
use dioxus::prelude::*;
2024-01-15 16:21:44 +00:00
use dioxus_core::{ElementId, NoOpMutations};
2022-11-23 04:16:14 +00:00
#[test]
fn text_diff() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
let gen = generation();
render!( h1 { "hello {gen}" } )
2022-11-23 04:16:14 +00:00
}
let mut vdom = VirtualDom::new(app);
2024-01-15 16:21:44 +00:00
vdom.rebuild(&mut NoOpMutations);
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().edits,
[SetText { value: "hello 1".to_string(), id: ElementId(2) }]
2022-11-23 04:16:14 +00:00
);
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().edits,
[SetText { value: "hello 2".to_string(), id: ElementId(2) }]
2022-11-23 04:16:14 +00:00
);
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().edits,
[SetText { value: "hello 3".to_string(), id: ElementId(2) }]
2022-11-23 04:16:14 +00:00
);
}
#[test]
fn element_swap() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
let gen = generation();
2022-11-23 04:16:14 +00:00
match gen % 2 {
2024-01-11 01:21:15 +00:00
0 => render!( h1 { "hello 1" } ),
1 => render!( h2 { "hello 2" } ),
2022-11-23 04:16:14 +00:00
_ => unreachable!(),
}
}
let mut vdom = VirtualDom::new(app);
2024-01-15 16:21:44 +00:00
vdom.rebuild(&mut NoOpMutations);
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.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 },
]
);
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
2022-11-25 07:12:29 +00:00
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
2022-11-24 14:11:27 +00:00
ReplaceWith { id: ElementId(2,), m: 1 },
]
);
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
2022-11-25 07:12:29 +00:00
LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
ReplaceWith { id: ElementId(1,), m: 1 },
2022-11-24 14:11:27 +00:00
]
);
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
vdom.render_immediate_to_vec().santize().edits,
2022-11-24 14:11:27 +00:00
[
2022-11-25 07:12:29 +00:00
LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
ReplaceWith { id: ElementId(2,), m: 1 },
2022-11-24 14:11:27 +00:00
]
);
2022-11-23 04:16:14 +00:00
}