passthru.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #![allow(unused, non_upper_case_globals)]
  2. //! Diffing Tests
  3. //!
  4. //! These tests only verify that the diffing algorithm works properly for single components.
  5. //!
  6. //! It does not validated that component lifecycles work properly. This is done in another test file.
  7. use dioxus::{prelude::*, DomEdit, ScopeId};
  8. use dioxus_core as dioxus;
  9. use dioxus_core_macro::*;
  10. use dioxus_html as dioxus_elements;
  11. mod test_logging;
  12. fn new_dom() -> VirtualDom {
  13. const IS_LOGGING_ENABLED: bool = false;
  14. test_logging::set_up_logging(IS_LOGGING_ENABLED);
  15. VirtualDom::new(|cx| rsx!(cx, "hi"))
  16. }
  17. use DomEdit::*;
  18. /// Should push the text node onto the stack and modify it
  19. #[test]
  20. fn nested_passthru_creates() {
  21. fn app(cx: Scope) -> Element {
  22. cx.render(rsx! {
  23. Child {
  24. Child {
  25. Child {
  26. div {
  27. "hi"
  28. }
  29. }
  30. }
  31. }
  32. })
  33. };
  34. #[inline_props]
  35. fn Child<'a>(cx: Scope, children: Element<'a>) -> Element {
  36. cx.render(rsx! {
  37. children
  38. })
  39. };
  40. let mut dom = VirtualDom::new(app);
  41. let mut channel = dom.get_scheduler_channel();
  42. assert!(dom.has_work());
  43. let edits = dom.rebuild();
  44. assert_eq!(
  45. edits.edits,
  46. [
  47. CreateElement { tag: "div", root: 1 },
  48. CreateTextNode { text: "hi", root: 2 },
  49. AppendChildren { many: 1 },
  50. AppendChildren { many: 1 },
  51. ]
  52. )
  53. }
  54. /// Should push the text node onto the stack and modify it
  55. #[test]
  56. fn nested_passthru_creates_add() {
  57. fn app(cx: Scope) -> Element {
  58. cx.render(rsx! {
  59. Child {
  60. "1"
  61. Child {
  62. "2"
  63. Child {
  64. "3"
  65. div {
  66. "hi"
  67. }
  68. }
  69. }
  70. }
  71. })
  72. };
  73. #[inline_props]
  74. fn Child<'a>(cx: Scope, children: Element<'a>) -> Element {
  75. cx.render(rsx! {
  76. children
  77. })
  78. };
  79. let mut dom = VirtualDom::new(app);
  80. let mut channel = dom.get_scheduler_channel();
  81. assert!(dom.has_work());
  82. let edits = dom.rebuild();
  83. assert_eq!(
  84. edits.edits,
  85. [
  86. CreateTextNode { text: "1", root: 1 },
  87. CreateTextNode { text: "2", root: 2 },
  88. CreateTextNode { text: "3", root: 3 },
  89. CreateElement { tag: "div", root: 4 },
  90. CreateTextNode { text: "hi", root: 5 },
  91. AppendChildren { many: 1 },
  92. AppendChildren { many: 4 },
  93. ]
  94. )
  95. }