dioxus/packages/core/tests/diff_component.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2022-11-29 21:31:04 +00:00
use dioxus::core::{ElementId, Mutation::*};
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 {
2022-11-29 21:31:04 +00:00
let render_phase = cx.use_hook(|| 0);
*render_phase += 1;
2024-01-11 01:21:15 +00:00
match *render_phase {
0 => render! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-11 01:21:15 +00:00
1 => render! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_results {}
},
2024-01-11 01:21:15 +00:00
2 => render! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-11 01:21:15 +00:00
3 => render! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_results {}
},
2024-01-11 01:21:15 +00:00
4 => render! {
2022-11-29 21:31:04 +00:00
nav_bar {}
dash_board {}
},
2024-01-11 01:21:15 +00:00
_ => render!("blah"),
}
2022-11-29 21:31:04 +00:00
}
fn nav_bar(cx: Scope) -> Element {
2024-01-11 01:21:15 +00:00
render! {
h1 { "NavBar", (0..3).map(|_| render!(nav_link {})) }
}
2022-11-29 21:31:04 +00:00
}
fn nav_link(cx: Scope) -> Element {
2024-01-11 01:21:15 +00:00
render!( h1 { "nav_link" } )
2022-11-29 21:31:04 +00:00
}
fn dash_board(cx: Scope) -> Element {
2024-01-11 01:21:15 +00:00
render!( div { "dashboard" } )
2022-11-29 21:31:04 +00:00
}
fn dash_results(cx: Scope) -> Element {
2024-01-11 01:21:15 +00:00
render!( 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 }
]
);
}