create_iterative.rs 6.5 KB

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