many_roots.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #![allow(non_snake_case)]
  2. use dioxus::dioxus_core::Mutation::*;
  3. use dioxus::prelude::*;
  4. use dioxus_core::{AttributeValue, ElementId};
  5. use pretty_assertions::assert_eq;
  6. /// Should push the text node onto the stack and modify it
  7. /// Regression test for https://github.com/DioxusLabs/dioxus/issues/2809 and https://github.com/DioxusLabs/dioxus/issues/3055
  8. #[test]
  9. fn many_roots() {
  10. fn app() -> Element {
  11. let width = "100%";
  12. rsx! {
  13. div {
  14. MyNav {}
  15. MyOutlet {}
  16. div {
  17. // We need to make sure that dynamic attributes are set before the nodes before them are expanded
  18. // If they are set after, then the paths are incorrect
  19. width,
  20. }
  21. }
  22. }
  23. }
  24. fn MyNav() -> Element {
  25. rsx!(
  26. div { "trailing nav" }
  27. div { "whhhhh"}
  28. div { "bhhhh" }
  29. )
  30. }
  31. fn MyOutlet() -> Element {
  32. rsx!(
  33. div { "homepage 1" }
  34. )
  35. }
  36. let mut dom = VirtualDom::new(app);
  37. let edits = dom.rebuild_to_vec();
  38. assert_eq!(
  39. edits.edits,
  40. [
  41. // load the div {} container
  42. LoadTemplate { index: 0, id: ElementId(1) },
  43. // Set the width attribute first
  44. AssignId { path: &[2], id: ElementId(2,) },
  45. SetAttribute {
  46. name: "width",
  47. ns: Some("style",),
  48. value: AttributeValue::Text("100%".to_string()),
  49. id: ElementId(2,),
  50. },
  51. // Load MyOutlet next
  52. LoadTemplate { index: 0, id: ElementId(3) },
  53. ReplacePlaceholder { path: &[1], m: 1 },
  54. // Then MyNav
  55. LoadTemplate { index: 0, id: ElementId(4) },
  56. LoadTemplate { index: 1, id: ElementId(5) },
  57. LoadTemplate { index: 2, id: ElementId(6) },
  58. ReplacePlaceholder { path: &[0], m: 3 },
  59. // Then mount the div to the dom
  60. AppendChildren { m: 1, id: ElementId(0) },
  61. ]
  62. )
  63. }