1
0

vdom_rebuild.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #![allow(unused, non_upper_case_globals)]
  2. //! Rebuilding tests
  3. //! ----------------
  4. //!
  5. //! This tests module ensures that the initial build of the virtualdom is correct.
  6. //! This does not include dynamic tests or the diffing algorithm itself.
  7. //!
  8. //! It does prove that mounting works properly and the correct edit streams are generated.
  9. //!
  10. //! Don't have a good way to validate, everything is done manually ATM
  11. use dioxus::prelude::*;
  12. use dioxus_core::DomEdit::*;
  13. #[test]
  14. fn app_runs() {
  15. static App: Component = |cx| render!(div{"hello"} );
  16. let mut vdom = VirtualDom::new(App);
  17. let edits = vdom.rebuild();
  18. }
  19. #[test]
  20. fn fragments_work() {
  21. static App: Component = |cx| {
  22. cx.render(rsx!(
  23. div{"hello"}
  24. div{"goodbye"}
  25. ))
  26. };
  27. let mut vdom = VirtualDom::new(App);
  28. let edits = vdom.rebuild();
  29. // should result in a final "appendchildren n=2"
  30. dbg!(edits);
  31. }
  32. #[test]
  33. fn lists_work() {
  34. static App: Component = |cx| {
  35. cx.render(rsx!(
  36. h1 {"hello"}
  37. (0..6).map(|f| rsx!(span{ "{f}" }))
  38. ))
  39. };
  40. let mut vdom = VirtualDom::new(App);
  41. let edits = vdom.rebuild();
  42. dbg!(edits);
  43. }
  44. #[test]
  45. fn conditional_rendering() {
  46. static App: Component = |cx| {
  47. cx.render(rsx!(
  48. h1 {"hello"}
  49. {true.then(|| rsx!(span{ "a" }))}
  50. {false.then(|| rsx!(span{ "b" }))}
  51. ))
  52. };
  53. let mut vdom = VirtualDom::new(App);
  54. let mutations = vdom.rebuild();
  55. assert_eq!(
  56. mutations.edits,
  57. [
  58. CreateTemplate { id: 0 },
  59. CreateElementTemplate {
  60. root: 4503599627370495,
  61. tag: "h1",
  62. locally_static: true,
  63. fully_static: true
  64. },
  65. CreateTextNodeTemplate { root: 4503599627370496, text: "hello", locally_static: true },
  66. AppendChildren { many: 1 },
  67. CreatePlaceholderTemplate { root: 4503599627370497 },
  68. CreatePlaceholderTemplate { root: 4503599627370498 },
  69. FinishTemplate { len: 3 },
  70. CreateTemplateRef { id: 1, template_id: 0 },
  71. EnterTemplateRef { root: 1 },
  72. CreateTemplate { id: 1 },
  73. CreateElementTemplate {
  74. root: 4503599627370495,
  75. tag: "span",
  76. locally_static: true,
  77. fully_static: true
  78. },
  79. CreateTextNodeTemplate { root: 4503599627370496, text: "a", locally_static: true },
  80. AppendChildren { many: 1 },
  81. FinishTemplate { len: 1 },
  82. CreateTemplateRef { id: 2, template_id: 1 },
  83. ReplaceWith { root: 4503599627370497, m: 1 },
  84. ExitTemplateRef {},
  85. EnterTemplateRef { root: 1 },
  86. CreatePlaceholder { root: 3 },
  87. ReplaceWith { root: 4503599627370498, m: 1 },
  88. ExitTemplateRef {},
  89. AppendChildren { many: 1 }
  90. ]
  91. )
  92. }
  93. #[test]
  94. fn child_components() {
  95. static App: Component = |cx| {
  96. cx.render(rsx!(
  97. {true.then(|| rsx!(Child { }))}
  98. {false.then(|| rsx!(Child { }))}
  99. ))
  100. };
  101. static Child: Component = |cx| {
  102. cx.render(rsx!(
  103. h1 {"hello"}
  104. h1 {"goodbye"}
  105. ))
  106. };
  107. let mut vdom = VirtualDom::new(App);
  108. let edits = vdom.rebuild();
  109. dbg!(edits);
  110. }