1
0

create_dom.rs 5.8 KB

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