create_passthru.rs 2.8 KB

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