create_passthru.rs 2.8 KB

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