passthru.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 },
  28. AppendChildren { m: 1 },
  29. ]
  30. )
  31. }
  32. /// Should load all the templates and append them
  33. #[test]
  34. fn nested_passthru_creates_add() {
  35. fn app(cx: Scope) -> Element {
  36. cx.render(rsx! {
  37. child_comp {
  38. "1"
  39. child_comp {
  40. "2"
  41. child_comp {
  42. "3"
  43. div {
  44. "hi"
  45. }
  46. }
  47. }
  48. }
  49. })
  50. }
  51. #[inline_props]
  52. fn child_comp<'a>(cx: Scope, children: Element<'a>) -> Element {
  53. cx.render(rsx! { children })
  54. }
  55. let mut dom = VirtualDom::new(app);
  56. assert_eq!(
  57. dom.rebuild().santize().edits,
  58. [
  59. LoadTemplate { name: "template", index: 0 },
  60. LoadTemplate { name: "template", index: 0 },
  61. LoadTemplate { name: "template", index: 0 },
  62. LoadTemplate { name: "template", index: 1 },
  63. AppendChildren { m: 4 },
  64. ]
  65. );
  66. }
  67. #[test]
  68. fn dynamic_node_as_root() {
  69. fn app(cx: Scope) -> Element {
  70. let a = 123;
  71. let b = 456;
  72. cx.render(rsx! { "{a}" "{b}" })
  73. }
  74. let mut dom = VirtualDom::new(app);
  75. let edits = dom.rebuild().santize();
  76. // Since the roots were all dynamic, they should not cause any template muations
  77. assert_eq!(edits.template_mutations, []);
  78. // The root node is text, so we just create it on the spot
  79. assert_eq!(
  80. edits.edits,
  81. [
  82. CreateTextNode { value: "123", id: ElementId(1) },
  83. CreateTextNode { value: "456", id: ElementId(2) },
  84. AppendChildren { m: 2 }
  85. ]
  86. )
  87. }