passthru.rs 2.6 KB

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