dioxus/packages/core/tests/diff_element.rs

85 lines
2.1 KiB
Rust
Raw Normal View History

2022-11-23 04:16:14 +00:00
use dioxus::core::Mutation::*;
use dioxus::prelude::*;
use dioxus_core::ElementId;
#[test]
fn text_diff() {
fn app(cx: Scope) -> Element {
let gen = cx.generation();
cx.render(rsx!( h1 { "hello {gen}" } ))
}
let mut vdom = VirtualDom::new(app);
2022-11-29 21:31:04 +00:00
_ = vdom.rebuild();
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().edits,
2022-11-23 04:16:14 +00:00
[SetText { value: "hello 1", id: ElementId(2) }]
);
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().edits,
2022-11-23 04:16:14 +00:00
[SetText { value: "hello 2", id: ElementId(2) }]
);
vdom.mark_dirty(ScopeId::ROOT);
2022-11-23 04:16:14 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().edits,
2022-11-23 04:16:14 +00:00
[SetText { value: "hello 3", id: ElementId(2) }]
);
}
#[test]
fn element_swap() {
fn app(cx: Scope) -> Element {
let gen = cx.generation();
match gen % 2 {
0 => cx.render(rsx!( h1 { "hello 1" } )),
1 => cx.render(rsx!( h2 { "hello 2" } )),
_ => unreachable!(),
}
}
let mut vdom = VirtualDom::new(app);
2022-11-29 21:31:04 +00:00
_ = vdom.rebuild();
2022-11-23 04:16:14 +00:00
vdom.mark_dirty(ScopeId::ROOT);
2022-11-24 14:11:27 +00:00
assert_eq!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().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!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().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!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().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!(
2022-12-01 05:46:15 +00:00
vdom.render_immediate().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
}