create_iterative.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //! tests to prove that the iterative implementation works
  2. use anyhow::{Context, Result};
  3. use dioxus::{arena::SharedResources, diff::DiffMachine, prelude::*, DomEdit, Mutations};
  4. mod test_logging;
  5. use dioxus_core as dioxus;
  6. use dioxus_html as dioxus_elements;
  7. use DomEdit::*;
  8. #[test]
  9. fn test_original_diff() {
  10. static App: FC<()> = |cx| {
  11. cx.render(rsx! {
  12. div {
  13. div {
  14. "Hello, world!"
  15. }
  16. }
  17. })
  18. };
  19. let mut dom = VirtualDom::new(App);
  20. let mutations = dom.rebuild().unwrap();
  21. assert_eq!(
  22. mutations.edits,
  23. [
  24. CreateElement { id: 0, tag: "div" },
  25. CreateElement { id: 1, tag: "div" },
  26. CreateTextNode {
  27. id: 2,
  28. text: "Hello, world!"
  29. },
  30. AppendChildren { many: 1 },
  31. AppendChildren { many: 1 },
  32. AppendChildren { many: 1 },
  33. ]
  34. );
  35. }
  36. #[async_std::test]
  37. async fn create() {
  38. static App: FC<()> = |cx| {
  39. cx.render(rsx! {
  40. div {
  41. div {
  42. "Hello, world!"
  43. div {
  44. div {
  45. Fragment {
  46. "hello"
  47. "world"
  48. }
  49. }
  50. }
  51. }
  52. }
  53. })
  54. };
  55. test_logging::set_up_logging();
  56. let mut dom = VirtualDom::new(App);
  57. let mutations = dom.rebuild_async().await.unwrap();
  58. assert_eq!(
  59. mutations.edits,
  60. [
  61. CreateElement { id: 0, tag: "div" },
  62. CreateElement { id: 1, tag: "div" },
  63. CreateTextNode {
  64. id: 2,
  65. text: "Hello, world!"
  66. },
  67. CreateElement { id: 3, tag: "div" },
  68. CreateElement { id: 4, tag: "div" },
  69. CreateTextNode {
  70. id: 5,
  71. text: "hello"
  72. },
  73. CreateTextNode {
  74. id: 6,
  75. text: "world"
  76. },
  77. AppendChildren { many: 2 },
  78. AppendChildren { many: 1 },
  79. AppendChildren { many: 2 },
  80. AppendChildren { many: 1 },
  81. AppendChildren { many: 1 },
  82. ]
  83. );
  84. }
  85. #[async_std::test]
  86. async fn create_list() {
  87. static App: FC<()> = |cx| {
  88. cx.render(rsx! {
  89. {(0..3).map(|f| rsx!{ div {
  90. "hello"
  91. }})}
  92. })
  93. };
  94. test_logging::set_up_logging();
  95. let mut dom = VirtualDom::new(App);
  96. let mutations = dom.rebuild_async().await.unwrap();
  97. // copilot wrote this test :P
  98. assert_eq!(
  99. mutations.edits,
  100. [
  101. CreateElement { id: 0, tag: "div" },
  102. CreateTextNode {
  103. id: 1,
  104. text: "hello"
  105. },
  106. AppendChildren { many: 1 },
  107. CreateElement { id: 2, tag: "div" },
  108. CreateTextNode {
  109. id: 3,
  110. text: "hello"
  111. },
  112. AppendChildren { many: 1 },
  113. CreateElement { id: 4, tag: "div" },
  114. CreateTextNode {
  115. id: 5,
  116. text: "hello"
  117. },
  118. AppendChildren { many: 1 },
  119. AppendChildren { many: 3 },
  120. ]
  121. );
  122. }
  123. #[async_std::test]
  124. async fn create_simple() {
  125. static App: FC<()> = |cx| {
  126. cx.render(rsx! {
  127. div {}
  128. div {}
  129. div {}
  130. div {}
  131. })
  132. };
  133. test_logging::set_up_logging();
  134. let mut dom = VirtualDom::new(App);
  135. let mutations = dom.rebuild_async().await.unwrap();
  136. // copilot wrote this test :P
  137. assert_eq!(
  138. mutations.edits,
  139. [
  140. CreateElement { id: 0, tag: "div" },
  141. CreateElement { id: 1, tag: "div" },
  142. CreateElement { id: 2, tag: "div" },
  143. CreateElement { id: 3, tag: "div" },
  144. AppendChildren { many: 4 },
  145. ]
  146. );
  147. }
  148. #[async_std::test]
  149. async fn create_components() {
  150. static App: FC<()> = |cx| {
  151. cx.render(rsx! {
  152. Child { "abc1" }
  153. Child { "abc2" }
  154. Child { "abc3" }
  155. })
  156. };
  157. static Child: FC<()> = |cx| {
  158. cx.render(rsx! {
  159. h1 {}
  160. div { {cx.children()} }
  161. p {}
  162. })
  163. };
  164. test_logging::set_up_logging();
  165. let mut dom = VirtualDom::new(App);
  166. let mutations = dom.rebuild_async().await.unwrap();
  167. assert_eq!(
  168. mutations.edits,
  169. [
  170. CreateElement { id: 0, tag: "h1" },
  171. CreateElement { id: 1, tag: "div" },
  172. CreateTextNode {
  173. id: 2,
  174. text: "abc1"
  175. },
  176. AppendChildren { many: 1 },
  177. CreateElement { id: 3, tag: "p" },
  178. CreateElement { id: 4, tag: "h1" },
  179. CreateElement { id: 5, tag: "div" },
  180. CreateTextNode {
  181. id: 6,
  182. text: "abc2"
  183. },
  184. AppendChildren { many: 1 },
  185. CreateElement { id: 7, tag: "p" },
  186. CreateElement { id: 8, tag: "h1" },
  187. CreateElement { id: 9, tag: "div" },
  188. CreateTextNode {
  189. id: 10,
  190. text: "abc3"
  191. },
  192. AppendChildren { many: 1 },
  193. CreateElement { id: 11, tag: "p" },
  194. AppendChildren { many: 9 },
  195. ]
  196. );
  197. }
  198. #[async_std::test]
  199. async fn anchors() {
  200. static App: FC<()> = |cx| {
  201. cx.render(rsx! {
  202. {true.then(|| rsx!{ div { "hello" } })}
  203. {false.then(|| rsx!{ div { "goodbye" } })}
  204. })
  205. };
  206. test_logging::set_up_logging();
  207. let mut dom = VirtualDom::new(App);
  208. let mutations = dom.rebuild_async().await.unwrap();
  209. assert_eq!(
  210. mutations.edits,
  211. [
  212. CreateElement { id: 0, tag: "div" },
  213. CreateTextNode {
  214. id: 1,
  215. text: "hello"
  216. },
  217. AppendChildren { many: 1 },
  218. CreatePlaceholder { id: 2 },
  219. AppendChildren { many: 2 },
  220. ]
  221. );
  222. }
  223. #[async_std::test]
  224. async fn suspended() {
  225. static App: FC<()> = |cx| {
  226. let val = use_suspense(cx, || async {}, |cx, _| cx.render(rsx! { "hi "}));
  227. cx.render(rsx! { {val} })
  228. };
  229. test_logging::set_up_logging();
  230. let mut dom = VirtualDom::new(App);
  231. let mutations = dom.rebuild_async().await.unwrap();
  232. assert_eq!(
  233. mutations.edits,
  234. [CreatePlaceholder { id: 0 }, AppendChildren { many: 1 },]
  235. );
  236. }