hydration.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. use dioxus::prelude::*;
  2. #[test]
  3. fn root_ids() {
  4. fn app() -> Element {
  5. rsx! { div { width: "100px" } }
  6. }
  7. let mut dom = VirtualDom::new(app);
  8. dom.rebuild(&mut dioxus_core::NoOpMutations);
  9. assert_eq!(
  10. dioxus_ssr::pre_render(&dom),
  11. r#"<div style="width:100px;" data-node-hydration="0"></div>"#
  12. );
  13. }
  14. #[test]
  15. fn dynamic_attributes() {
  16. fn app() -> Element {
  17. let dynamic = 123;
  18. rsx! {
  19. div { width: "100px", div { width: "{dynamic}px" } }
  20. }
  21. }
  22. let mut dom = VirtualDom::new(app);
  23. dom.rebuild(&mut dioxus_core::NoOpMutations);
  24. assert_eq!(
  25. dioxus_ssr::pre_render(&dom),
  26. r#"<div style="width:100px;" data-node-hydration="0"><div style="width:123px;" data-node-hydration="1"></div></div>"#
  27. );
  28. }
  29. #[test]
  30. fn listeners() {
  31. fn app() -> Element {
  32. rsx! {
  33. div { width: "100px", div { onclick: |_| {} } }
  34. }
  35. }
  36. let mut dom = VirtualDom::new(app);
  37. dom.rebuild(&mut dioxus_core::NoOpMutations);
  38. assert_eq!(
  39. dioxus_ssr::pre_render(&dom),
  40. r#"<div style="width:100px;" data-node-hydration="0"><div data-node-hydration="1,click:1"></div></div>"#
  41. );
  42. fn app2() -> Element {
  43. let dynamic = 123;
  44. rsx! {
  45. div { width: "100px", div { width: "{dynamic}px", onclick: |_| {} } }
  46. }
  47. }
  48. let mut dom = VirtualDom::new(app2);
  49. dom.rebuild(&mut dioxus_core::NoOpMutations);
  50. assert_eq!(
  51. dioxus_ssr::pre_render(&dom),
  52. r#"<div style="width:100px;" data-node-hydration="0"><div style="width:123px;" data-node-hydration="1,click:1"></div></div>"#
  53. );
  54. }
  55. #[test]
  56. fn text_nodes() {
  57. fn app() -> Element {
  58. let dynamic_text = "hello";
  59. rsx! {
  60. div { {dynamic_text} }
  61. }
  62. }
  63. let mut dom = VirtualDom::new(app);
  64. dom.rebuild(&mut dioxus_core::NoOpMutations);
  65. assert_eq!(
  66. dioxus_ssr::pre_render(&dom),
  67. r#"<div data-node-hydration="0"><!--node-id1-->hello<!--#--></div>"#
  68. );
  69. fn app2() -> Element {
  70. let dynamic = 123;
  71. rsx! {
  72. div { "{dynamic}" "{1234}" }
  73. }
  74. }
  75. let mut dom = VirtualDom::new(app2);
  76. dom.rebuild(&mut dioxus_core::NoOpMutations);
  77. assert_eq!(
  78. dioxus_ssr::pre_render(&dom),
  79. r#"<div data-node-hydration="0"><!--node-id1-->123<!--#--><!--node-id2-->1234<!--#--></div>"#
  80. );
  81. }
  82. #[allow(non_snake_case)]
  83. #[test]
  84. fn components_hydrate() {
  85. fn app() -> Element {
  86. rsx! { Child {} }
  87. }
  88. fn Child() -> Element {
  89. rsx! { div { "hello" } }
  90. }
  91. let mut dom = VirtualDom::new(app);
  92. dom.rebuild(&mut dioxus_core::NoOpMutations);
  93. assert_eq!(
  94. dioxus_ssr::pre_render(&dom),
  95. r#"<div data-node-hydration="0">hello</div>"#
  96. );
  97. fn app2() -> Element {
  98. rsx! { Child2 {} }
  99. }
  100. fn Child2() -> Element {
  101. let dyn_text = "hello";
  102. rsx! {
  103. div { {dyn_text} }
  104. }
  105. }
  106. let mut dom = VirtualDom::new(app2);
  107. dom.rebuild(&mut dioxus_core::NoOpMutations);
  108. assert_eq!(
  109. dioxus_ssr::pre_render(&dom),
  110. r#"<div data-node-hydration="0"><!--node-id1-->hello<!--#--></div>"#
  111. );
  112. fn app3() -> Element {
  113. rsx! { Child3 {} }
  114. }
  115. fn Child3() -> Element {
  116. rsx! { div { width: "{1}" } }
  117. }
  118. let mut dom = VirtualDom::new(app3);
  119. dom.rebuild(&mut dioxus_core::NoOpMutations);
  120. assert_eq!(
  121. dioxus_ssr::pre_render(&dom),
  122. r#"<div style="width:1;" data-node-hydration="0"></div>"#
  123. );
  124. fn app4() -> Element {
  125. rsx! { Child4 {} }
  126. }
  127. fn Child4() -> Element {
  128. rsx! {
  129. for _ in 0..2 {
  130. {rsx! { "{1}" }}
  131. }
  132. }
  133. }
  134. let mut dom = VirtualDom::new(app4);
  135. dom.rebuild(&mut dioxus_core::NoOpMutations);
  136. assert_eq!(
  137. dioxus_ssr::pre_render(&dom),
  138. r#"<!--node-id0-->1<!--#--><!--node-id1-->1<!--#-->"#
  139. );
  140. }
  141. #[test]
  142. fn hello_world_hydrates() {
  143. use dioxus::hooks::use_signal;
  144. fn app() -> Element {
  145. let mut count = use_signal(|| 0);
  146. rsx! {
  147. h1 { "High-Five counter: {count}" }
  148. button { onclick: move |_| count += 1, "Up high!" }
  149. button { onclick: move |_| count -= 1, "Down low!" }
  150. }
  151. }
  152. let mut dom = VirtualDom::new(app);
  153. dom.rebuild(&mut dioxus_core::NoOpMutations);
  154. assert_eq!(
  155. dioxus_ssr::pre_render(&dom),
  156. r#"<h1 data-node-hydration="0"><!--node-id1-->High-Five counter: 0<!--#--></h1><button data-node-hydration="2,click:1">Up high!</button><button data-node-hydration="3,click:1">Down low!</button>"#
  157. );
  158. }