passthru.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Child {
  10. Child {
  11. Child {
  12. div {
  13. "hi"
  14. }
  15. }
  16. }
  17. }
  18. })
  19. }
  20. #[inline_props]
  21. fn Child<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
  22. cx.render(rsx! { children })
  23. }
  24. let mut dom = VirtualDom::new(app);
  25. let edits = dom.rebuild().santize();
  26. assert_eq!(
  27. edits.edits,
  28. [
  29. LoadTemplate { name: "template", index: 0 },
  30. AppendChildren { m: 1 },
  31. ]
  32. )
  33. }
  34. /// Should load all the templates and append them
  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. LoadTemplate { name: "template", index: 0 },
  62. LoadTemplate { name: "template", index: 0 },
  63. LoadTemplate { name: "template", index: 0 },
  64. LoadTemplate { name: "template", index: 1 },
  65. AppendChildren { m: 4 },
  66. ]
  67. );
  68. }
  69. #[test]
  70. fn dynamic_node_as_root() {
  71. fn app(cx: Scope) -> Element {
  72. let a = 123;
  73. let b = 456;
  74. cx.render(rsx! { "{a}" "{b}" })
  75. }
  76. let mut dom = VirtualDom::new(app);
  77. let edits = dom.rebuild().santize();
  78. // Since the roots were all dynamic, they should not cause any template muations
  79. assert_eq!(edits.template_mutations, []);
  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", id: ElementId(1) },
  85. CreateTextNode { value: "456", id: ElementId(2) },
  86. AppendChildren { m: 2 }
  87. ]
  88. )
  89. }