passthru.rs 2.5 KB

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