create_passthru.rs 2.9 KB

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