initial_build.rs 3.3 KB

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