passthru.rs 2.7 KB

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