create_dom.rs 5.7 KB

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