2021-10-22 05:16:39 +00:00
|
|
|
#![allow(unused, non_upper_case_globals)]
|
2021-07-30 14:35:47 +00:00
|
|
|
//! Diffing Tests
|
2021-09-02 04:10:09 +00:00
|
|
|
//!
|
|
|
|
//! These tests only verify that the diffing algorithm works properly for single components.
|
|
|
|
//!
|
|
|
|
//! It does not validated that component lifecycles work properly. This is done in another test file.
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-11-28 21:25:42 +00:00
|
|
|
use dioxus::{prelude::*, DomEdit};
|
2021-07-29 01:46:53 +00:00
|
|
|
use dioxus_core as dioxus;
|
2021-09-25 01:46:23 +00:00
|
|
|
use dioxus_core_macro::*;
|
2021-07-29 01:46:53 +00:00
|
|
|
use dioxus_html as dioxus_elements;
|
2021-09-02 04:10:09 +00:00
|
|
|
|
2021-08-24 14:01:01 +00:00
|
|
|
mod test_logging;
|
2021-08-24 15:03:19 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
fn new_dom() -> VirtualDom {
|
2021-09-10 00:58:48 +00:00
|
|
|
const IS_LOGGING_ENABLED: bool = false;
|
2021-09-02 04:10:09 +00:00
|
|
|
test_logging::set_up_logging(IS_LOGGING_ENABLED);
|
2021-12-21 03:33:13 +00:00
|
|
|
VirtualDom::new(|cx| rsx!(cx, "hi"))
|
2021-09-02 04:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
use DomEdit::*;
|
2021-07-29 01:46:53 +00:00
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
/// Should push the text node onto the stack and modify it
|
2021-07-29 01:46:53 +00:00
|
|
|
#[test]
|
|
|
|
fn html_and_rsx_generate_the_same_output() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(
|
2021-07-29 22:04:09 +00:00
|
|
|
rsx! ( div { "Hello world" } ),
|
|
|
|
rsx! ( div { "Goodbye world" } ),
|
|
|
|
);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2,
|
2021-08-24 14:01:01 +00:00
|
|
|
text: "Hello world"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
2021-10-11 19:35:20 +00:00
|
|
|
[SetText {
|
|
|
|
text: "Goodbye world",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2
|
2021-10-11 19:35:20 +00:00
|
|
|
},]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:47 +00:00
|
|
|
/// Should result in 3 elements on the stack
|
2021-07-29 22:04:09 +00:00
|
|
|
#[test]
|
|
|
|
fn fragments_create_properly() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let create = dom.create_vnodes(rsx! {
|
2021-07-29 22:04:09 +00:00
|
|
|
div { "Hello a" }
|
|
|
|
div { "Hello b" }
|
|
|
|
div { "Hello c" }
|
|
|
|
});
|
2021-07-29 01:46:53 +00:00
|
|
|
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2,
|
2021-08-24 14:01:01 +00:00
|
|
|
text: "Hello a"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 3,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 4,
|
2021-08-24 14:01:01 +00:00
|
|
|
text: "Hello b"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-08-24 14:01:01 +00:00
|
|
|
text: "Hello c"
|
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 3 },
|
|
|
|
]
|
|
|
|
);
|
2021-07-29 01:46:53 +00:00
|
|
|
}
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
/// Should result in the creation of an anchor (placeholder) and then a replacewith
|
|
|
|
#[test]
|
|
|
|
fn empty_fragments_create_anchors() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let left = rsx!({ (0..0).map(|_f| rsx! { div {}}) });
|
|
|
|
let right = rsx!({ (0..1).map(|_f| rsx! { div {}}) });
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[CreatePlaceholder { root: 1 }, AppendChildren { many: 1 }]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
ReplaceWith { m: 1, root: 1 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in the creation of an anchor (placeholder) and then a replacewith m=5
|
|
|
|
#[test]
|
|
|
|
fn empty_fragments_create_many_anchors() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let left = rsx!({ (0..0).map(|_f| rsx! { div {}}) });
|
|
|
|
let right = rsx!({ (0..5).map(|_f| rsx! { div {}}) });
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[CreatePlaceholder { root: 1 }, AppendChildren { many: 1 }]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 2,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 3,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 4,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 5,
|
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 6,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
ReplaceWith { m: 5, root: 1 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in the creation of an anchor (placeholder) and then a replacewith
|
|
|
|
/// Includes child nodes inside the fragment
|
|
|
|
#[test]
|
|
|
|
fn empty_fragments_create_anchors_with_many_children() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-09-10 00:58:48 +00:00
|
|
|
let left = rsx!({ (0..0).map(|_| rsx! { div {} }) });
|
2021-07-30 14:35:47 +00:00
|
|
|
let right = rsx!({
|
2021-08-24 14:01:01 +00:00
|
|
|
(0..3).map(|f| {
|
|
|
|
rsx! { div { "hello: {f}" }}
|
2021-07-30 14:35:47 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[CreatePlaceholder { root: 1 }, AppendChildren { many: 1 }]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
2021-12-18 20:49:30 +00:00
|
|
|
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-12-18 20:49:30 +00:00
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
|
|
|
text: "hello: 0",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 3
|
2021-08-24 14:01:01 +00:00
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-12-18 20:49:30 +00:00
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 4,
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
|
|
|
text: "hello: 1",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5
|
2021-08-24 14:01:01 +00:00
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-12-18 20:49:30 +00:00
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
|
|
|
text: "hello: 2",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 7
|
2021-08-24 14:01:01 +00:00
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-12-18 20:49:30 +00:00
|
|
|
ReplaceWith { root: 1, m: 3 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in every node being pushed and then replaced with an anchor
|
|
|
|
#[test]
|
|
|
|
fn many_items_become_fragment() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
2021-09-10 00:58:48 +00:00
|
|
|
(0..2).map(|_| {
|
2021-07-30 14:35:47 +00:00
|
|
|
rsx! { div { "hello" }}
|
|
|
|
})
|
|
|
|
});
|
2021-09-10 00:58:48 +00:00
|
|
|
let right = rsx!({ (0..0).map(|_| rsx! { div {} }) });
|
2021-07-30 14:35:47 +00:00
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
|
|
|
text: "hello",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2
|
2021-08-24 14:01:01 +00:00
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 3,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-08-24 14:01:01 +00:00
|
|
|
CreateTextNode {
|
|
|
|
text: "hello",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 4
|
2021-08-24 14:01:01 +00:00
|
|
|
},
|
|
|
|
AppendChildren { many: 1 },
|
|
|
|
AppendChildren { many: 2 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-12-18 20:49:30 +00:00
|
|
|
CreatePlaceholder { root: 5 },
|
2021-11-12 02:50:08 +00:00
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-12-18 20:49:30 +00:00
|
|
|
Remove { root: 3 },
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in no edits
|
|
|
|
#[test]
|
|
|
|
fn two_equal_fragments_are_equal() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
2021-09-10 00:58:48 +00:00
|
|
|
(0..2).map(|_| {
|
2021-07-30 14:35:47 +00:00
|
|
|
rsx! { div { "hello" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let right = rsx!({
|
2021-09-10 00:58:48 +00:00
|
|
|
(0..2).map(|_| {
|
2021-07-30 14:35:47 +00:00
|
|
|
rsx! { div { "hello" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert!(change.edits.is_empty());
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result the creation of more nodes appended after the old last node
|
|
|
|
#[test]
|
|
|
|
fn two_fragments_with_differrent_elements_are_differet() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!(
|
2021-08-24 16:43:46 +00:00
|
|
|
{ (0..2).map(|_| rsx! { div { }} ) }
|
2021-07-30 14:35:47 +00:00
|
|
|
p {}
|
|
|
|
);
|
|
|
|
let right = rsx!(
|
2021-08-24 16:43:46 +00:00
|
|
|
{ (0..5).map(|_| rsx! (h1 { }) ) }
|
2021-07-30 14:35:47 +00:00
|
|
|
p {}
|
|
|
|
);
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_create, changes) = dom.diff_lazynodes(left, right);
|
2021-08-24 16:43:46 +00:00
|
|
|
log::debug!("{:#?}", &changes);
|
|
|
|
assert_eq!(
|
|
|
|
changes.edits,
|
|
|
|
[
|
|
|
|
// create the new h1s
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement { tag: "h1", root: 4 },
|
|
|
|
CreateElement { tag: "h1", root: 5 },
|
|
|
|
CreateElement { tag: "h1", root: 6 },
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertAfter { root: 2, n: 3 },
|
|
|
|
// replace the divs with new h1s
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement { tag: "h1", root: 7 },
|
2021-08-24 16:43:46 +00:00
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-11-12 02:50:08 +00:00
|
|
|
CreateElement { tag: "h1", root: 8 },
|
|
|
|
ReplaceWith { root: 2, m: 1 },
|
2021-08-24 16:43:46 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in multiple nodes destroyed - with changes to the first nodes
|
|
|
|
#[test]
|
|
|
|
fn two_fragments_with_differrent_elements_are_differet_shorter() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!(
|
|
|
|
{(0..5).map(|f| {rsx! { div { }}})}
|
|
|
|
p {}
|
|
|
|
);
|
|
|
|
let right = rsx!(
|
|
|
|
{(0..2).map(|f| {rsx! { h1 { }}})}
|
|
|
|
p {}
|
|
|
|
);
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 1,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 2,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 3,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
root: 4,
|
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
CreateElement {
|
|
|
|
root: 5,
|
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement { root: 6, tag: "p" },
|
2021-08-24 14:01:01 +00:00
|
|
|
AppendChildren { many: 6 },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
|
|
|
Remove { root: 3 },
|
|
|
|
Remove { root: 4 },
|
2021-11-12 02:50:08 +00:00
|
|
|
Remove { root: 5 },
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement { root: 7, tag: "h1" },
|
2021-08-24 14:01:01 +00:00
|
|
|
ReplaceWith { root: 1, m: 1 },
|
2021-11-12 02:50:08 +00:00
|
|
|
CreateElement { root: 8, tag: "h1" },
|
|
|
|
ReplaceWith { root: 2, m: 1 },
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in multiple nodes destroyed - with no changes
|
|
|
|
#[test]
|
|
|
|
fn two_fragments_with_same_elements_are_differet() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!(
|
|
|
|
{(0..2).map(|f| {rsx! { div { }}})}
|
|
|
|
p {}
|
|
|
|
);
|
|
|
|
let right = rsx!(
|
|
|
|
{(0..5).map(|f| {rsx! { div { }}})}
|
|
|
|
p {}
|
|
|
|
);
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
create.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 1,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 2,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
CreateElement { root: 3, tag: "p" },
|
2021-08-24 14:01:01 +00:00
|
|
|
AppendChildren { many: 3 },
|
|
|
|
]
|
2021-07-30 14:35:47 +00:00
|
|
|
);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 4,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertAfter { root: 2, n: 3 },
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
2021-07-30 14:35:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// should result in the removal of elements
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_order() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 14:35:47 +00:00
|
|
|
|
|
|
|
let left = rsx!(
|
2021-11-10 22:09:52 +00:00
|
|
|
{(0..5).map(|f| {rsx! { div { key: "{f}" }}})}
|
2021-07-30 14:35:47 +00:00
|
|
|
p {"e"}
|
|
|
|
);
|
|
|
|
let right = rsx!(
|
2021-11-01 06:22:08 +00:00
|
|
|
{(0..2).map(|f| rsx! { div { key: "{f}" }})}
|
2021-07-30 14:35:47 +00:00
|
|
|
p {"e"}
|
|
|
|
);
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (create, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[Remove { root: 3 }, Remove { root: 4 }, Remove { root: 5 },]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in moves, but not removals or additions
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 20:07:42 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
2021-08-24 14:01:01 +00:00
|
|
|
[0, 1, 2, 3, /**/ 4, 5, 6, /**/ 7, 8, 9].iter().map(|f| {
|
2021-07-30 20:07:42 +00:00
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
2021-08-24 14:01:01 +00:00
|
|
|
[0, 1, 2, 3, /**/ 6, 4, 5, /**/ 7, 8, 9].iter().map(|f| {
|
2021-07-30 20:07:42 +00:00
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, changes) = dom.diff_lazynodes(left, right);
|
2021-08-24 16:43:46 +00:00
|
|
|
log::debug!("{:?}", &changes);
|
|
|
|
assert_eq!(
|
|
|
|
changes.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[PushRoot { root: 7 }, InsertBefore { root: 5, n: 1 }]
|
2021-08-24 16:43:46 +00:00
|
|
|
);
|
2021-08-24 14:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in moves only
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order_adds() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 8, 7, 4, 5, 6 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 5 },
|
2021-10-05 07:37:15 +00:00
|
|
|
PushRoot { root: 4 },
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 2, root: 1 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/// Should result in moves onl
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order_adds_2() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 7, 8, 4, 5, 6 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
PushRoot { root: 4 },
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 5 },
|
|
|
|
InsertBefore { n: 2, root: 1 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in moves onl
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order_adds_3() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 4, 8, 7, 5, 6 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 5 },
|
2021-10-05 07:37:15 +00:00
|
|
|
PushRoot { root: 4 },
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 2, root: 2 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in moves onl
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order_adds_4() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 4, 5, 8, 7, 6 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 5 },
|
2021-10-05 07:37:15 +00:00
|
|
|
PushRoot { root: 4 },
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 2, root: 3 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Should result in moves onl
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_out_of_order_adds_5() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 8, 7 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
2021-11-12 02:50:08 +00:00
|
|
|
[PushRoot { root: 5 }, InsertBefore { n: 1, root: 4 }]
|
2021-08-24 14:01:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_additions() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7, 8, 9, 10 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 14:01:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 7,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertAfter { n: 2, root: 5 }
|
2021-08-24 14:01:01 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_additions_and_moves_on_ends() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 7, 4, 5, 6, 11, 12 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 15:03:19 +00:00
|
|
|
log::debug!("{:?}", change);
|
2021-08-24 16:43:46 +00:00
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
|
|
|
// create 11, 12
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertAfter { root: 3, n: 2 },
|
2021-08-24 16:43:46 +00:00
|
|
|
// move 7 to the front
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 4 },
|
|
|
|
InsertBefore { root: 1, n: 1 }
|
2021-08-24 16:43:46 +00:00
|
|
|
]
|
|
|
|
);
|
2021-08-24 14:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keyed_diffing_additions_and_moves_in_middle() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 14:01:01 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[/**/ 4, 5, 6, 7 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[/**/ 7, 4, 13, 17, 5, 11, 12, 6 /**/].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-08-24 16:43:46 +00:00
|
|
|
// LIS: 4, 5, 6
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, change) = dom.diff_lazynodes(left, right);
|
2021-08-24 16:43:46 +00:00
|
|
|
log::debug!("{:#?}", change);
|
|
|
|
assert_eq!(
|
|
|
|
change.edits,
|
|
|
|
[
|
|
|
|
// create 13, 17
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { root: 2, n: 2 },
|
2021-08-24 16:43:46 +00:00
|
|
|
// create 11, 12
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 7
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
|
|
|
CreateElement {
|
|
|
|
tag: "div",
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 8
|
2021-10-05 07:37:15 +00:00
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { root: 3, n: 2 },
|
2021-08-24 16:43:46 +00:00
|
|
|
// move 7
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 4 },
|
|
|
|
InsertBefore { root: 1, n: 1 }
|
2021-08-24 16:43:46 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 20:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn controlled_keyed_diffing_out_of_order() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-07-30 20:07:42 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
2021-08-24 15:03:19 +00:00
|
|
|
[4, 5, 6, 7].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
2021-07-30 20:07:42 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
2021-08-24 15:03:19 +00:00
|
|
|
[0, 5, 9, 6, 4].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
2021-07-30 20:07:42 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-08-24 15:03:19 +00:00
|
|
|
// LIS: 5, 6
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, changes) = dom.diff_lazynodes(left, right);
|
2021-08-24 15:03:19 +00:00
|
|
|
log::debug!("{:#?}", &changes);
|
|
|
|
assert_eq!(
|
|
|
|
changes.edits,
|
|
|
|
[
|
|
|
|
// move 4 to after 6
|
2021-11-12 02:50:08 +00:00
|
|
|
PushRoot { root: 1 },
|
|
|
|
InsertAfter { n: 1, root: 3 },
|
2021-08-24 15:03:19 +00:00
|
|
|
// remove 7
|
|
|
|
|
|
|
|
// create 9 and insert before 6
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 5,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 1, root: 3 },
|
2021-08-24 15:03:19 +00:00
|
|
|
// create 0 and insert before 5
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 1, root: 2 },
|
2021-08-24 15:03:19 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn controlled_keyed_diffing_out_of_order_max_test() {
|
2021-09-02 04:10:09 +00:00
|
|
|
let dom = new_dom();
|
2021-08-24 15:03:19 +00:00
|
|
|
|
|
|
|
let left = rsx!({
|
|
|
|
[0, 1, 2, 3, 4].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let right = rsx!({
|
|
|
|
[3, 0, 1, 10, 2].iter().map(|f| {
|
|
|
|
rsx! { div { key: "{f}" }}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-11-09 17:10:11 +00:00
|
|
|
let (_, changes) = dom.diff_lazynodes(left, right);
|
2021-08-24 15:03:19 +00:00
|
|
|
log::debug!("{:#?}", &changes);
|
|
|
|
assert_eq!(
|
|
|
|
changes.edits,
|
|
|
|
[
|
2021-10-05 07:37:15 +00:00
|
|
|
CreateElement {
|
2021-11-12 02:50:08 +00:00
|
|
|
root: 6,
|
2021-10-05 07:37:15 +00:00
|
|
|
tag: "div"
|
|
|
|
},
|
2021-11-12 02:50:08 +00:00
|
|
|
InsertBefore { n: 1, root: 3 },
|
|
|
|
PushRoot { root: 4 },
|
|
|
|
InsertBefore { n: 1, root: 1 },
|
2021-08-24 15:03:19 +00:00
|
|
|
]
|
|
|
|
);
|
2021-07-30 14:35:47 +00:00
|
|
|
}
|