create_dom.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //! Prove that the dom works normally through virtualdom methods.
  2. //! This methods all use "rebuild" which completely bypasses the scheduler.
  3. //! Hard rebuilds don't consume any events from the event queue.
  4. use dioxus::{prelude::*, DomEdit};
  5. use dioxus_core as dioxus;
  6. use dioxus_html as dioxus_elements;
  7. mod test_logging;
  8. use DomEdit::*;
  9. fn new_dom<P: Properties + 'static>(app: FC<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: FC<()> = |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 { id: 0, tag: "div" },
  31. CreateElement { id: 1, tag: "div" },
  32. CreateTextNode {
  33. id: 2,
  34. text: "Hello, world!"
  35. },
  36. AppendChildren { many: 1 },
  37. AppendChildren { many: 1 },
  38. AppendChildren { many: 1 },
  39. ]
  40. );
  41. }
  42. #[async_std::test]
  43. async fn create() {
  44. static APP: FC<()> = |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 { id: 0, tag: "div" },
  67. CreateElement { id: 1, tag: "div" },
  68. CreateTextNode {
  69. id: 2,
  70. text: "Hello, world!"
  71. },
  72. CreateElement { id: 3, tag: "div" },
  73. CreateElement { id: 4, tag: "div" },
  74. CreateTextNode {
  75. id: 5,
  76. text: "hello"
  77. },
  78. CreateTextNode {
  79. id: 6,
  80. text: "world"
  81. },
  82. AppendChildren { many: 2 },
  83. AppendChildren { many: 1 },
  84. AppendChildren { many: 2 },
  85. AppendChildren { many: 1 },
  86. AppendChildren { many: 1 },
  87. ]
  88. );
  89. }
  90. #[async_std::test]
  91. async fn create_list() {
  92. static APP: FC<()> = |cx| {
  93. cx.render(rsx! {
  94. {(0..3).map(|f| rsx!{ div {
  95. "hello"
  96. }})}
  97. })
  98. };
  99. let mut dom = new_dom(APP, ());
  100. let mutations = dom.rebuild();
  101. // copilot wrote this test :P
  102. assert_eq!(
  103. mutations.edits,
  104. [
  105. CreateElement { id: 0, tag: "div" },
  106. CreateTextNode {
  107. id: 1,
  108. text: "hello"
  109. },
  110. AppendChildren { many: 1 },
  111. CreateElement { id: 2, tag: "div" },
  112. CreateTextNode {
  113. id: 3,
  114. text: "hello"
  115. },
  116. AppendChildren { many: 1 },
  117. CreateElement { id: 4, tag: "div" },
  118. CreateTextNode {
  119. id: 5,
  120. text: "hello"
  121. },
  122. AppendChildren { many: 1 },
  123. AppendChildren { many: 3 },
  124. ]
  125. );
  126. }
  127. #[async_std::test]
  128. async fn create_simple() {
  129. static APP: FC<()> = |cx| {
  130. cx.render(rsx! {
  131. div {}
  132. div {}
  133. div {}
  134. div {}
  135. })
  136. };
  137. let mut dom = new_dom(APP, ());
  138. let mutations = dom.rebuild();
  139. // copilot wrote this test :P
  140. assert_eq!(
  141. mutations.edits,
  142. [
  143. CreateElement { id: 0, tag: "div" },
  144. CreateElement { id: 1, tag: "div" },
  145. CreateElement { id: 2, tag: "div" },
  146. CreateElement { id: 3, tag: "div" },
  147. AppendChildren { many: 4 },
  148. ]
  149. );
  150. }
  151. #[async_std::test]
  152. async fn create_components() {
  153. static App: FC<()> = |cx| {
  154. cx.render(rsx! {
  155. Child { "abc1" }
  156. Child { "abc2" }
  157. Child { "abc3" }
  158. })
  159. };
  160. static Child: FC<()> = |cx| {
  161. cx.render(rsx! {
  162. h1 {}
  163. div { {cx.children()} }
  164. p {}
  165. })
  166. };
  167. let mut dom = new_dom(App, ());
  168. let mutations = dom.rebuild();
  169. assert_eq!(
  170. mutations.edits,
  171. [
  172. CreateElement { id: 0, tag: "h1" },
  173. CreateElement { id: 1, tag: "div" },
  174. CreateTextNode {
  175. id: 2,
  176. text: "abc1"
  177. },
  178. AppendChildren { many: 1 },
  179. CreateElement { id: 3, tag: "p" },
  180. CreateElement { id: 4, tag: "h1" },
  181. CreateElement { id: 5, tag: "div" },
  182. CreateTextNode {
  183. id: 6,
  184. text: "abc2"
  185. },
  186. AppendChildren { many: 1 },
  187. CreateElement { id: 7, tag: "p" },
  188. CreateElement { id: 8, tag: "h1" },
  189. CreateElement { id: 9, tag: "div" },
  190. CreateTextNode {
  191. id: 10,
  192. text: "abc3"
  193. },
  194. AppendChildren { many: 1 },
  195. CreateElement { id: 11, tag: "p" },
  196. AppendChildren { many: 9 },
  197. ]
  198. );
  199. }
  200. #[async_std::test]
  201. async fn anchors() {
  202. static App: FC<()> = |cx| {
  203. cx.render(rsx! {
  204. {true.then(|| rsx!{ div { "hello" } })}
  205. {false.then(|| rsx!{ div { "goodbye" } })}
  206. })
  207. };
  208. let mut dom = new_dom(App, ());
  209. let mutations = dom.rebuild();
  210. assert_eq!(
  211. mutations.edits,
  212. [
  213. CreateElement { id: 0, tag: "div" },
  214. CreateTextNode {
  215. id: 1,
  216. text: "hello"
  217. },
  218. AppendChildren { many: 1 },
  219. CreatePlaceholder { id: 2 },
  220. AppendChildren { many: 2 },
  221. ]
  222. );
  223. }
  224. #[async_std::test]
  225. async fn suspended() {
  226. static App: FC<()> = |cx| {
  227. let val = use_suspense(
  228. cx,
  229. || async {
  230. //
  231. },
  232. |cx, _| cx.render(rsx! { "hi "}),
  233. );
  234. cx.render(rsx! { {val} })
  235. };
  236. let mut dom = new_dom(App, ());
  237. let mutations = dom.rebuild();
  238. assert_eq!(
  239. mutations.edits,
  240. [CreatePlaceholder { id: 0 }, AppendChildren { many: 1 },]
  241. );
  242. }