create_dom.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. //! Prove that the dom works normally through virtualdom methods.
  3. //!
  4. //! This methods all use "rebuild" which completely bypasses the scheduler.
  5. //! Hard rebuilds don't consume any events from the event queue.
  6. use dioxus::prelude::*;
  7. use dioxus_core::DomEdit::*;
  8. fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
  9. VirtualDom::new_with_props(app, props)
  10. }
  11. #[test]
  12. fn test_original_diff() {
  13. static APP: Component = |cx| {
  14. cx.render(rsx! {
  15. div {
  16. div {
  17. "Hello, world!"
  18. }
  19. }
  20. })
  21. };
  22. let mut dom = new_dom(APP, ());
  23. let mutations = dom.rebuild();
  24. assert_eq!(
  25. mutations.edits,
  26. [
  27. CreateElement { root: 1, tag: "div" },
  28. CreateElement { root: 2, tag: "div" },
  29. CreateTextNode { root: 3, text: "Hello, world!" },
  30. AppendChildren { many: 1 },
  31. AppendChildren { many: 1 },
  32. AppendChildren { many: 1 },
  33. ]
  34. );
  35. }
  36. #[test]
  37. fn create() {
  38. static APP: Component = |cx| {
  39. cx.render(rsx! {
  40. div {
  41. div {
  42. "Hello, world!"
  43. div {
  44. div {
  45. Fragment {
  46. "hello"
  47. "world"
  48. }
  49. }
  50. }
  51. }
  52. }
  53. })
  54. };
  55. let mut dom = new_dom(APP, ());
  56. let mutations = dom.rebuild();
  57. assert_eq!(
  58. mutations.edits,
  59. [
  60. CreateElement { root: 1, tag: "div" },
  61. CreateElement { root: 2, tag: "div" },
  62. CreateTextNode { root: 3, text: "Hello, world!" },
  63. CreateElement { root: 4, tag: "div" },
  64. CreateElement { root: 5, tag: "div" },
  65. CreateTextNode { root: 6, text: "hello" },
  66. CreateTextNode { root: 7, text: "world" },
  67. AppendChildren { many: 2 },
  68. AppendChildren { many: 1 },
  69. AppendChildren { many: 2 },
  70. AppendChildren { many: 1 },
  71. AppendChildren { many: 1 },
  72. ]
  73. );
  74. }
  75. #[test]
  76. fn create_list() {
  77. static APP: Component = |cx| {
  78. cx.render(rsx! {
  79. {(0..3).map(|f| rsx!{ div {
  80. "hello"
  81. }})}
  82. })
  83. };
  84. let mut dom = new_dom(APP, ());
  85. let mutations = dom.rebuild();
  86. // copilot wrote this test :P
  87. assert_eq!(
  88. mutations.edits,
  89. [
  90. CreateElement { root: 1, tag: "div" },
  91. CreateTextNode { root: 2, text: "hello" },
  92. AppendChildren { many: 1 },
  93. CreateElement { root: 3, tag: "div" },
  94. CreateTextNode { root: 4, text: "hello" },
  95. AppendChildren { many: 1 },
  96. CreateElement { root: 5, tag: "div" },
  97. CreateTextNode { root: 6, text: "hello" },
  98. AppendChildren { many: 1 },
  99. AppendChildren { many: 3 },
  100. ]
  101. );
  102. }
  103. #[test]
  104. fn create_simple() {
  105. static APP: Component = |cx| {
  106. cx.render(rsx! {
  107. div {}
  108. div {}
  109. div {}
  110. div {}
  111. })
  112. };
  113. let mut dom = new_dom(APP, ());
  114. let mutations = dom.rebuild();
  115. // copilot wrote this test :P
  116. assert_eq!(
  117. mutations.edits,
  118. [
  119. CreateElement { root: 1, tag: "div" },
  120. CreateElement { root: 2, tag: "div" },
  121. CreateElement { root: 3, tag: "div" },
  122. CreateElement { root: 4, tag: "div" },
  123. AppendChildren { many: 4 },
  124. ]
  125. );
  126. }
  127. #[test]
  128. fn create_components() {
  129. static App: Component = |cx| {
  130. cx.render(rsx! {
  131. Child { "abc1" }
  132. Child { "abc2" }
  133. Child { "abc3" }
  134. })
  135. };
  136. #[derive(Props)]
  137. struct ChildProps<'a> {
  138. children: Element<'a>,
  139. }
  140. fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
  141. cx.render(rsx! {
  142. h1 {}
  143. div { &cx.props.children }
  144. p {}
  145. })
  146. }
  147. let mut dom = new_dom(App, ());
  148. let mutations = dom.rebuild();
  149. assert_eq!(
  150. mutations.edits,
  151. [
  152. CreateElement { root: 1, tag: "h1" },
  153. CreateElement { root: 2, tag: "div" },
  154. CreateTextNode { root: 3, text: "abc1" },
  155. AppendChildren { many: 1 },
  156. CreateElement { root: 4, tag: "p" },
  157. CreateElement { root: 5, tag: "h1" },
  158. CreateElement { root: 6, tag: "div" },
  159. CreateTextNode { root: 7, text: "abc2" },
  160. AppendChildren { many: 1 },
  161. CreateElement { root: 8, tag: "p" },
  162. CreateElement { root: 9, tag: "h1" },
  163. CreateElement { root: 10, tag: "div" },
  164. CreateTextNode { root: 11, text: "abc3" },
  165. AppendChildren { many: 1 },
  166. CreateElement { root: 12, tag: "p" },
  167. AppendChildren { many: 9 },
  168. ]
  169. );
  170. }
  171. #[test]
  172. fn anchors() {
  173. static App: Component = |cx| {
  174. cx.render(rsx! {
  175. {true.then(|| rsx!{ div { "hello" } })}
  176. {false.then(|| rsx!{ div { "goodbye" } })}
  177. })
  178. };
  179. let mut dom = new_dom(App, ());
  180. let mutations = dom.rebuild();
  181. assert_eq!(
  182. mutations.edits,
  183. [
  184. CreateElement { root: 1, tag: "div" },
  185. CreateTextNode { root: 2, text: "hello" },
  186. AppendChildren { many: 1 },
  187. CreatePlaceholder { root: 3 },
  188. AppendChildren { many: 2 },
  189. ]
  190. );
  191. }