initial_build.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. use std::cell::Cell;
  2. use dioxus_core::VNode;
  3. use dioxus_core::*;
  4. use dioxus_core_macro::*;
  5. use dioxus_html as dioxus_elements;
  6. use dioxus_native_core::real_dom::RealDom;
  7. #[test]
  8. fn tree_initial_build_simple() {
  9. use std::cell::Cell;
  10. #[allow(non_snake_case)]
  11. fn Base(cx: Scope) -> Element {
  12. rsx!(cx, div {})
  13. }
  14. let vdom = VirtualDom::new(Base);
  15. let mutations = vdom.create_vnodes(rsx! {
  16. div{}
  17. });
  18. let mut tree: RealDom<(), ()> = RealDom::new();
  19. let _to_update = tree.apply_mutations(vec![mutations]);
  20. let root_div = VElement {
  21. id: Cell::new(Some(ElementId(1))),
  22. key: None,
  23. tag: "div",
  24. namespace: None,
  25. parent: Cell::new(Some(ElementId(0))),
  26. listeners: &[],
  27. attributes: &[],
  28. children: &[],
  29. };
  30. assert_eq!(tree.size(), 1);
  31. assert!(&tree.contains_node(&VNode::Element(&root_div)));
  32. assert_eq!(tree[1].height, 1);
  33. }
  34. #[test]
  35. fn tree_initial_build_with_children() {
  36. #[allow(non_snake_case)]
  37. fn Base(cx: Scope) -> Element {
  38. rsx!(cx, div {})
  39. }
  40. let vdom = VirtualDom::new(Base);
  41. let mutations = vdom.create_vnodes(rsx! {
  42. div{
  43. div{
  44. "hello"
  45. p{
  46. "world"
  47. }
  48. "hello world"
  49. }
  50. }
  51. });
  52. let mut tree: RealDom<(), ()> = RealDom::new();
  53. let _to_update = tree.apply_mutations(vec![mutations]);
  54. let first_text = VText {
  55. id: Cell::new(Some(ElementId(3))),
  56. text: "hello",
  57. is_static: true,
  58. };
  59. let first_text_node = VNode::Text(&first_text);
  60. let child_text = VText {
  61. id: Cell::new(Some(ElementId(5))),
  62. text: "world",
  63. is_static: true,
  64. };
  65. let child_text_node = VNode::Text(&child_text);
  66. let child_p_el = VElement {
  67. id: Cell::new(Some(ElementId(4))),
  68. key: None,
  69. tag: "p",
  70. namespace: None,
  71. parent: Cell::new(Some(ElementId(2))),
  72. listeners: &[],
  73. attributes: &[],
  74. children: &[child_text_node],
  75. };
  76. let child_p_node = VNode::Element(&child_p_el);
  77. let second_text = VText {
  78. id: Cell::new(Some(ElementId(6))),
  79. text: "hello world",
  80. is_static: true,
  81. };
  82. let second_text_node = VNode::Text(&second_text);
  83. let child_div_el = VElement {
  84. id: Cell::new(Some(ElementId(2))),
  85. key: None,
  86. tag: "div",
  87. namespace: None,
  88. parent: Cell::new(Some(ElementId(1))),
  89. listeners: &[],
  90. attributes: &[],
  91. children: &[first_text_node, child_p_node, second_text_node],
  92. };
  93. let child_div_node = VNode::Element(&child_div_el);
  94. let root_div = VElement {
  95. id: Cell::new(Some(ElementId(1))),
  96. key: None,
  97. tag: "div",
  98. namespace: None,
  99. parent: Cell::new(Some(ElementId(0))),
  100. listeners: &[],
  101. attributes: &[],
  102. children: &[child_div_node],
  103. };
  104. assert_eq!(tree.size(), 6);
  105. assert!(&tree.contains_node(&VNode::Element(&root_div)));
  106. assert_eq!(tree[1].height, 1);
  107. assert_eq!(tree[2].height, 2);
  108. assert_eq!(tree[3].height, 3);
  109. assert_eq!(tree[4].height, 3);
  110. assert_eq!(tree[5].height, 4);
  111. assert_eq!(tree[6].height, 3);
  112. }