create_passthru.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use dioxus::dioxus_core::Mutation::*;
  2. use dioxus::prelude::*;
  3. use dioxus_core::ElementId;
  4. /// Should push the text node onto the stack and modify it
  5. #[test]
  6. fn nested_passthru_creates() {
  7. fn app() -> Element {
  8. rsx! {
  9. PassThru {
  10. PassThru {
  11. PassThru { div { "hi" } }
  12. }
  13. }
  14. }
  15. }
  16. #[component]
  17. fn PassThru(children: Element) -> Element {
  18. rsx!({ children })
  19. }
  20. let mut dom = VirtualDom::new(app);
  21. let edits = dom.rebuild_to_vec();
  22. assert_eq!(
  23. edits.edits,
  24. [
  25. LoadTemplate { index: 0, id: ElementId(1) },
  26. AppendChildren { m: 1, id: ElementId(0) },
  27. ]
  28. )
  29. }
  30. /// Should load all the templates and append them
  31. ///
  32. /// Take note on how we don't spit out the template for child_comp since it's entirely dynamic
  33. #[test]
  34. fn nested_passthru_creates_add() {
  35. fn app() -> Element {
  36. rsx! {
  37. ChildComp {
  38. "1"
  39. ChildComp {
  40. "2"
  41. ChildComp {
  42. "3"
  43. div { "hi" }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. #[component]
  50. fn ChildComp(children: Element) -> Element {
  51. rsx! {{children}}
  52. }
  53. let mut dom = VirtualDom::new(app);
  54. assert_eq!(
  55. dom.rebuild_to_vec().edits,
  56. [
  57. // load 1
  58. LoadTemplate { index: 0, id: ElementId(1) },
  59. // load 2
  60. LoadTemplate { index: 0, id: ElementId(2) },
  61. // load 3
  62. LoadTemplate { index: 0, id: ElementId(3) },
  63. // load div that contains 4
  64. LoadTemplate { index: 1, id: ElementId(4) },
  65. AppendChildren { id: ElementId(0), m: 4 },
  66. ]
  67. );
  68. }
  69. /// note that the template is all dynamic roots - so it doesn't actually get cached as a template
  70. #[test]
  71. fn dynamic_node_as_root() {
  72. fn app() -> Element {
  73. let a = 123;
  74. let b = 456;
  75. rsx! { "{a}" "{b}" }
  76. }
  77. let mut dom = VirtualDom::new(app);
  78. let edits = dom.rebuild_to_vec();
  79. // Since the roots were all dynamic, they should not cause any template muations
  80. // The root node is text, so we just create it on the spot
  81. assert_eq!(
  82. edits.edits,
  83. [
  84. CreateTextNode { value: "123".to_string(), id: ElementId(1) },
  85. CreateTextNode { value: "456".to_string(), id: ElementId(2) },
  86. AppendChildren { id: ElementId(0), m: 2 }
  87. ]
  88. )
  89. }