diff_dynamic_node.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use dioxus::dioxus_core::{ElementId, Mutation::*};
  2. use dioxus::prelude::*;
  3. use pretty_assertions::assert_eq;
  4. #[test]
  5. fn toggle_option_text() {
  6. let mut dom = VirtualDom::new(|| {
  7. let gen = generation();
  8. let text = if gen % 2 != 0 { Some("hello") } else { None };
  9. println!("{:?}", text);
  10. rsx! {
  11. div {
  12. {text}
  13. }
  14. }
  15. });
  16. // load the div and then assign the None as a placeholder
  17. assert_eq!(
  18. dom.rebuild_to_vec().edits,
  19. [
  20. LoadTemplate { index: 0, id: ElementId(1,) },
  21. CreatePlaceholder { id: ElementId(2,) },
  22. ReplacePlaceholder { path: &[0], m: 1 },
  23. AppendChildren { id: ElementId(0), m: 1 },
  24. ]
  25. );
  26. // Rendering again should replace the placeholder with an text node
  27. dom.mark_dirty(ScopeId::APP);
  28. assert_eq!(
  29. dom.render_immediate_to_vec().edits,
  30. [
  31. CreateTextNode { value: "hello".to_string(), id: ElementId(3,) },
  32. ReplaceWith { id: ElementId(2,), m: 1 },
  33. ]
  34. );
  35. // Rendering again should replace the placeholder with an text node
  36. dom.mark_dirty(ScopeId::APP);
  37. assert_eq!(
  38. dom.render_immediate_to_vec().edits,
  39. [
  40. CreatePlaceholder { id: ElementId(2,) },
  41. ReplaceWith { id: ElementId(3,), m: 1 },
  42. ]
  43. );
  44. }
  45. // Regression test for https://github.com/DioxusLabs/dioxus/issues/2815
  46. #[test]
  47. fn toggle_template() {
  48. fn app() -> Element {
  49. rsx!(
  50. Comp {
  51. if true {
  52. "{true}"
  53. }
  54. }
  55. )
  56. }
  57. #[component]
  58. fn Comp(children: Element) -> Element {
  59. let show = generation() % 2 == 0;
  60. rsx! {
  61. if show {
  62. {children}
  63. }
  64. }
  65. }
  66. let mut dom = VirtualDom::new(app);
  67. dom.rebuild(&mut dioxus_core::NoOpMutations);
  68. // Rendering again should replace the placeholder with an text node
  69. dom.mark_dirty(ScopeId::APP);
  70. assert_eq!(
  71. dom.render_immediate_to_vec().edits,
  72. [
  73. CreatePlaceholder { id: ElementId(2) },
  74. ReplaceWith { id: ElementId(1), m: 1 },
  75. ]
  76. );
  77. dom.mark_dirty(ScopeId(ScopeId::APP.0 + 1));
  78. assert_eq!(
  79. dom.render_immediate_to_vec().edits,
  80. [
  81. CreateTextNode { value: "true".to_string(), id: ElementId(1) },
  82. ReplaceWith { id: ElementId(2), m: 1 },
  83. ]
  84. );
  85. dom.mark_dirty(ScopeId(ScopeId::APP.0 + 1));
  86. assert_eq!(
  87. dom.render_immediate_to_vec().edits,
  88. [
  89. CreatePlaceholder { id: ElementId(2) },
  90. ReplaceWith { id: ElementId(1), m: 1 },
  91. ]
  92. );
  93. dom.mark_dirty(ScopeId(ScopeId::APP.0 + 1));
  94. assert_eq!(
  95. dom.render_immediate_to_vec().edits,
  96. [
  97. CreateTextNode { value: "true".to_string(), id: ElementId(1) },
  98. ReplaceWith { id: ElementId(2), m: 1 },
  99. ]
  100. );
  101. }