dioxus/packages/core/tests/diff_element.rs

209 lines
5.8 KiB
Rust
Raw Normal View History

2024-01-16 01:14:11 +00:00
use dioxus::dioxus_core::Mutation::*;
2024-01-31 02:17:45 +00:00
use dioxus::dioxus_core::{AttributeValue, ElementId, NoOpMutations};
2022-11-23 04:16:14 +00:00
use dioxus::prelude::*;
#[test]
fn text_diff() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
let gen = generation();
2024-01-16 19:18:46 +00:00
rsx!( 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-16 19:18:46 +00:00
0 => rsx!( h1 { "hello 1" } ),
1 => rsx!( 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
}
2024-01-21 15:29:29 +00:00
#[test]
fn attribute_diff() {
2024-01-31 02:17:45 +00:00
fn app() -> Element {
let gen = generation();
// attributes have to be sorted by name
let attrs = match gen % 5 {
0 => vec![Attribute::new(
"a",
AttributeValue::Text("hello".into()),
None,
false,
)],
1 => vec![
Attribute::new("a", AttributeValue::Text("hello".into()), None, false),
Attribute::new("b", AttributeValue::Text("hello".into()), None, false),
Attribute::new("c", AttributeValue::Text("hello".into()), None, false),
],
2 => vec![
Attribute::new("c", AttributeValue::Text("hello".into()), None, false),
Attribute::new("d", AttributeValue::Text("hello".into()), None, false),
Attribute::new("e", AttributeValue::Text("hello".into()), None, false),
],
3 => vec![Attribute::new(
"d",
AttributeValue::Text("world".into()),
None,
false,
)],
_ => unreachable!(),
};
rsx!(
div {
..attrs,
"hello"
}
)
}
let mut vdom = VirtualDom::new(app);
vdom.rebuild(&mut NoOpMutations);
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate_to_vec().santize().edits,
[
SetAttribute {
name: "b",
value: (AttributeValue::Text("hello".into())),
id: ElementId(1,),
ns: None,
},
SetAttribute {
name: "c",
value: (AttributeValue::Text("hello".into())),
id: ElementId(1,),
ns: None,
},
]
);
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate_to_vec().santize().edits,
[
SetAttribute { name: "a", value: AttributeValue::None, id: ElementId(1,), ns: None },
SetAttribute { name: "b", value: AttributeValue::None, id: ElementId(1,), ns: None },
SetAttribute {
name: "d",
value: AttributeValue::Text("hello".into()),
id: ElementId(1,),
ns: None,
},
SetAttribute {
name: "e",
value: AttributeValue::Text("hello".into()),
id: ElementId(1,),
ns: None,
},
]
);
vdom.mark_dirty(ScopeId::ROOT);
assert_eq!(
vdom.render_immediate_to_vec().santize().edits,
[
SetAttribute { name: "c", value: AttributeValue::None, id: ElementId(1,), ns: None },
SetAttribute {
name: "d",
value: AttributeValue::Text("world".into()),
id: ElementId(1,),
ns: None,
},
SetAttribute { name: "e", value: AttributeValue::None, id: ElementId(1,), ns: None },
]
);
2024-01-21 15:29:29 +00:00
}
#[test]
fn diff_empty() {
fn app() -> Element {
match generation() % 2 {
0 => rsx! { div { "hello" } },
1 => rsx! {},
_ => unreachable!(),
}
}
let mut vdom = VirtualDom::new(app);
vdom.rebuild(&mut NoOpMutations);
vdom.mark_dirty(ScopeId::ROOT);
let edits = vdom.render_immediate_to_vec().santize().edits;
assert_eq!(
edits,
[
CreatePlaceholder { id: ElementId(2,) },
ReplaceWith { id: ElementId(1,), m: 1 },
]
)
}