create_dom.rs 7.0 KB

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