dioxus/packages/core/tests/diff_component.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2024-01-16 01:14:11 +00:00
use dioxus::dioxus_core::{ElementId, Mutation::*};
2022-11-29 21:31:04 +00:00
use dioxus::prelude::*;
/// When returning sets of components, we do a light diff of the contents to preserve some react-like functionality
///
/// This means that nav_bar should never get re-created and that we should only be swapping out
/// different pointers
#[test]
fn component_swap() {
fn app() -> Element {
2024-01-11 21:18:11 +00:00
let mut render_phase = use_signal(|| 0);
2022-11-29 21:31:04 +00:00
2024-01-11 21:18:11 +00:00
render_phase += 1;
2022-11-29 21:31:04 +00:00
2024-01-16 21:51:02 +00:00
match render_phase() {
2024-01-16 19:18:46 +00:00
0 => rsx! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-16 19:18:46 +00:00
1 => rsx! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_results {}
},
2024-01-16 19:18:46 +00:00
2 => rsx! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-16 19:18:46 +00:00
3 => rsx! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_results {}
},
2024-01-16 19:18:46 +00:00
4 => rsx! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-16 19:18:46 +00:00
_ => rsx!("blah"),
2024-01-11 01:21:15 +00:00
}
2022-11-29 21:31:04 +00:00
}
2024-01-11 17:11:44 +00:00
fn nav_bar() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
h1 { "NavBar", {(0..3).map(|_| rsx!(nav_link {}))} }
2024-01-11 01:21:15 +00:00
}
2022-11-29 21:31:04 +00:00
}
2024-01-11 17:11:44 +00:00
fn nav_link() -> Element {
2024-01-16 19:18:46 +00:00
rsx!( h1 { "nav_link" } )
2022-11-29 21:31:04 +00:00
}
2024-01-11 17:11:44 +00:00
fn dash_board() -> Element {
2024-01-16 19:18:46 +00:00
rsx!( div { "dashboard" } )
2022-11-29 21:31:04 +00:00
}
2024-01-11 17:11:44 +00:00
fn dash_results() -> Element {
2024-01-16 19:18:46 +00:00
rsx!( div { "results" } )
2022-11-29 21:31:04 +00:00
}
let mut dom = VirtualDom::new(app);
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) },
LoadTemplate { name: "template", index: 0, id: ElementId(2) },
LoadTemplate { name: "template", index: 0, id: ElementId(3) },
LoadTemplate { name: "template", index: 0, id: ElementId(4) },
ReplacePlaceholder { path: &[1], m: 3 },
LoadTemplate { name: "template", index: 0, id: ElementId(5) },
AppendChildren { m: 2, id: ElementId(0) }
]
);
}
2022-11-29 21:31:04 +00:00
dom.mark_dirty(ScopeId::ROOT);
2022-11-29 21:31:04 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-29 21:31:04 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(6) },
ReplaceWith { id: ElementId(5), m: 1 }
]
);
dom.mark_dirty(ScopeId::ROOT);
2022-11-29 21:31:04 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-29 21:31:04 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(5) },
ReplaceWith { id: ElementId(6), m: 1 }
]
);
dom.mark_dirty(ScopeId::ROOT);
2022-11-29 21:31:04 +00:00
assert_eq!(
2024-01-11 01:21:15 +00:00
dom.render_immediate_to_vec().santize().edits,
2022-11-29 21:31:04 +00:00
[
LoadTemplate { name: "template", index: 0, id: ElementId(6) },
ReplaceWith { id: ElementId(5), m: 1 }
]
);
}