create_dom.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. CreateTemplate { id: 0 },
  28. CreateElementTemplate {
  29. root: 4503599627370495,
  30. tag: "div",
  31. locally_static: true,
  32. fully_static: true
  33. },
  34. CreateElementTemplate {
  35. root: 4503599627370496,
  36. tag: "div",
  37. locally_static: true,
  38. fully_static: true
  39. },
  40. CreateTextNodeTemplate {
  41. root: 4503599627370497,
  42. text: "Hello, world!",
  43. locally_static: true
  44. },
  45. AppendChildren { many: 1 },
  46. AppendChildren { many: 1 },
  47. FinishTemplate { len: 1 },
  48. CreateTemplateRef { id: 1, template_id: 0 },
  49. AppendChildren { many: 1 }
  50. ]
  51. );
  52. }
  53. #[test]
  54. fn create() {
  55. static APP: Component = |cx| {
  56. cx.render(rsx! {
  57. div {
  58. div {
  59. "Hello, world!"
  60. div {
  61. div {
  62. Fragment {
  63. "hello"
  64. "world"
  65. }
  66. }
  67. }
  68. }
  69. }
  70. })
  71. };
  72. let mut dom = new_dom(APP, ());
  73. let mutations = dom.rebuild();
  74. assert_eq!(
  75. mutations.edits,
  76. [
  77. CreateTemplate { id: 0 },
  78. CreateElementTemplate {
  79. root: 4503599627370495,
  80. tag: "div",
  81. locally_static: true,
  82. fully_static: false
  83. },
  84. CreateElementTemplate {
  85. root: 4503599627370496,
  86. tag: "div",
  87. locally_static: true,
  88. fully_static: false
  89. },
  90. CreateTextNodeTemplate {
  91. root: 4503599627370497,
  92. text: "Hello, world!",
  93. locally_static: true
  94. },
  95. CreateElementTemplate {
  96. root: 4503599627370498,
  97. tag: "div",
  98. locally_static: true,
  99. fully_static: false
  100. },
  101. CreateElementTemplate {
  102. root: 4503599627370499,
  103. tag: "div",
  104. locally_static: true,
  105. fully_static: false
  106. },
  107. CreatePlaceholderTemplate { root: 4503599627370500 },
  108. AppendChildren { many: 1 },
  109. AppendChildren { many: 1 },
  110. AppendChildren { many: 2 },
  111. AppendChildren { many: 1 },
  112. FinishTemplate { len: 1 },
  113. CreateTemplateRef { id: 1, template_id: 0 },
  114. EnterTemplateRef { root: 1 },
  115. CreateTextNode { root: 2, text: "hello" },
  116. CreateTextNode { root: 3, text: "world" },
  117. ReplaceWith { root: 4503599627370500, m: 2 },
  118. ExitTemplateRef {},
  119. AppendChildren { many: 1 }
  120. ]
  121. );
  122. }
  123. #[test]
  124. fn create_list() {
  125. static APP: Component = |cx| {
  126. cx.render(rsx! {
  127. {(0..3).map(|f| rsx!{ div {
  128. "hello"
  129. }})}
  130. })
  131. };
  132. let mut dom = new_dom(APP, ());
  133. let mutations = dom.rebuild();
  134. // copilot wrote this test :P
  135. assert_eq!(
  136. mutations.edits,
  137. [
  138. CreateTemplate { id: 0 },
  139. CreateElementTemplate {
  140. root: 4503599627370495,
  141. tag: "div",
  142. locally_static: true,
  143. fully_static: true
  144. },
  145. CreateTextNodeTemplate { root: 4503599627370496, text: "hello", locally_static: true },
  146. AppendChildren { many: 1 },
  147. FinishTemplate { len: 1 },
  148. CreateTemplateRef { id: 1, template_id: 0 },
  149. CreateTemplateRef { id: 2, template_id: 0 },
  150. CreateTemplateRef { id: 3, template_id: 0 },
  151. AppendChildren { many: 3 }
  152. ]
  153. );
  154. }
  155. #[test]
  156. fn create_simple() {
  157. static APP: Component = |cx| {
  158. cx.render(rsx! {
  159. div {}
  160. div {}
  161. div {}
  162. div {}
  163. })
  164. };
  165. let mut dom = new_dom(APP, ());
  166. let mutations = dom.rebuild();
  167. // copilot wrote this test :P
  168. assert_eq!(
  169. mutations.edits,
  170. [
  171. CreateTemplate { id: 0 },
  172. CreateElementTemplate {
  173. root: 4503599627370495,
  174. tag: "div",
  175. locally_static: true,
  176. fully_static: true
  177. },
  178. AppendChildren { many: 0 },
  179. CreateElementTemplate {
  180. root: 4503599627370496,
  181. tag: "div",
  182. locally_static: true,
  183. fully_static: true
  184. },
  185. AppendChildren { many: 0 },
  186. CreateElementTemplate {
  187. root: 4503599627370497,
  188. tag: "div",
  189. locally_static: true,
  190. fully_static: true
  191. },
  192. AppendChildren { many: 0 },
  193. CreateElementTemplate {
  194. root: 4503599627370498,
  195. tag: "div",
  196. locally_static: true,
  197. fully_static: true
  198. },
  199. AppendChildren { many: 0 },
  200. FinishTemplate { len: 4 },
  201. CreateTemplateRef { id: 1, template_id: 0 },
  202. AppendChildren { many: 1 }
  203. ]
  204. );
  205. }
  206. #[test]
  207. fn create_components() {
  208. static App: Component = |cx| {
  209. cx.render(rsx! {
  210. Child { "abc1" }
  211. Child { "abc2" }
  212. Child { "abc3" }
  213. })
  214. };
  215. #[derive(Props)]
  216. struct ChildProps<'a> {
  217. children: Element<'a>,
  218. }
  219. fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
  220. cx.render(rsx! {
  221. h1 {}
  222. div { &cx.props.children }
  223. p {}
  224. })
  225. }
  226. let mut dom = new_dom(App, ());
  227. let mutations = dom.rebuild();
  228. assert_eq!(
  229. mutations.edits,
  230. [
  231. CreateTemplate { id: 0 },
  232. CreateElementTemplate {
  233. root: 4503599627370495,
  234. tag: "h1",
  235. locally_static: true,
  236. fully_static: true
  237. },
  238. AppendChildren { many: 0 },
  239. CreateElementTemplate {
  240. root: 4503599627370496,
  241. tag: "div",
  242. locally_static: true,
  243. fully_static: false
  244. },
  245. CreatePlaceholderTemplate { root: 4503599627370497 },
  246. AppendChildren { many: 1 },
  247. CreateElementTemplate {
  248. root: 4503599627370498,
  249. tag: "p",
  250. locally_static: true,
  251. fully_static: true
  252. },
  253. AppendChildren { many: 0 },
  254. FinishTemplate { len: 3 },
  255. CreateTemplateRef { id: 1, template_id: 0 },
  256. EnterTemplateRef { root: 1 },
  257. CreateTextNode { root: 2, text: "abc1" },
  258. ReplaceWith { root: 4503599627370497, m: 1 },
  259. ExitTemplateRef {},
  260. CreateTemplateRef { id: 3, template_id: 0 },
  261. EnterTemplateRef { root: 3 },
  262. CreateTextNode { root: 4, text: "abc2" },
  263. ReplaceWith { root: 4503599627370497, m: 1 },
  264. ExitTemplateRef {},
  265. CreateTemplateRef { id: 5, template_id: 0 },
  266. EnterTemplateRef { root: 5 },
  267. CreateTextNode { root: 6, text: "abc3" },
  268. ReplaceWith { root: 4503599627370497, m: 1 },
  269. ExitTemplateRef {},
  270. AppendChildren { many: 3 }
  271. ]
  272. );
  273. }
  274. #[test]
  275. fn anchors() {
  276. static App: Component = |cx| {
  277. cx.render(rsx! {
  278. {true.then(|| rsx!{ div { "hello" } })}
  279. {false.then(|| rsx!{ div { "goodbye" } })}
  280. })
  281. };
  282. let mut dom = new_dom(App, ());
  283. let mutations = dom.rebuild();
  284. assert_eq!(
  285. mutations.edits,
  286. [
  287. CreateTemplate { id: 0 },
  288. CreateElementTemplate {
  289. root: 4503599627370495,
  290. tag: "div",
  291. locally_static: true,
  292. fully_static: true
  293. },
  294. CreateTextNodeTemplate { root: 4503599627370496, text: "hello", locally_static: true },
  295. AppendChildren { many: 1 },
  296. FinishTemplate { len: 1 },
  297. CreateTemplateRef { id: 1, template_id: 0 },
  298. CreatePlaceholder { root: 2 },
  299. AppendChildren { many: 2 }
  300. ]
  301. );
  302. }