feat: add FlatVDom round-trip conversion tests
This commit is contained in:
@@ -18,6 +18,7 @@ use reader::Reader;
|
||||
use rhei::RHEI_PREFIX;
|
||||
use style::{AncestorInfo, ComputedStyle, StructuralContext};
|
||||
pub use types::Value;
|
||||
use types::{FlatVDom, VNode};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub struct Interpreter;
|
||||
@@ -662,3 +663,69 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_flat_vdom_roundtrip() {
|
||||
let mut root = Element::new("Panel");
|
||||
root.element_id = ElementId(1);
|
||||
root.push_prop("class", "container");
|
||||
root.push_prop("color", "red");
|
||||
|
||||
let mut child1 = Element::new("Button");
|
||||
child1.element_id = ElementId(2);
|
||||
child1.push_prop("label", "Click");
|
||||
|
||||
let mut child2 = Element::new("Text");
|
||||
child2.element_id = ElementId(3);
|
||||
child2.push_prop("text", "Hello");
|
||||
|
||||
root.children.push(child1);
|
||||
root.children.push(child2);
|
||||
|
||||
let roots = vec![root];
|
||||
|
||||
let fv = FlatVDom::from_elements(&roots);
|
||||
assert_eq!(fv.node_count(), 3, "FlatVDom should have 3 nodes");
|
||||
|
||||
let roundtrip = fv.into_elements();
|
||||
assert_eq!(roundtrip.len(), 1, "Should have 1 root");
|
||||
assert_eq!(roundtrip[0].type_name, "Panel");
|
||||
assert_eq!(roundtrip[0].children.len(), 2);
|
||||
assert_eq!(roundtrip[0].children[0].type_name, "Button");
|
||||
assert_eq!(roundtrip[0].children[0].get_prop("label"), Some("Click"));
|
||||
assert_eq!(roundtrip[0].children[1].type_name, "Text");
|
||||
assert_eq!(roundtrip[0].children[1].get_prop("text"), Some("Hello"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flat_vdom_empty() {
|
||||
let fv = FlatVDom::from_elements(&[]);
|
||||
assert_eq!(fv.node_count(), 0);
|
||||
let elements = fv.into_elements();
|
||||
assert!(elements.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flat_vdom_nested() {
|
||||
let mut outer = Element::new("Window");
|
||||
outer.element_id = ElementId(1);
|
||||
let mut inner = Element::new("Panel");
|
||||
inner.element_id = ElementId(2);
|
||||
let mut btn = Element::new("Button");
|
||||
btn.element_id = ElementId(3);
|
||||
btn.push_prop("label", "Nested");
|
||||
inner.children.push(btn);
|
||||
outer.children.push(inner);
|
||||
|
||||
let roots = vec![outer];
|
||||
let fv = FlatVDom::from_elements(&roots);
|
||||
assert_eq!(fv.node_count(), 3);
|
||||
|
||||
let elements = fv.into_elements();
|
||||
assert_eq!(elements[0].children[0].children[0].get_prop("label"), Some("Nested"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user