vdom_rebuild.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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::DomEdit;
  13. use dioxus_core as dioxus;
  14. use dioxus_core_macro::*;
  15. use dioxus_html as dioxus_elements;
  16. use DomEdit::*;
  17. #[test]
  18. fn app_runs() {
  19. static App: Component = |cx| rsx!(cx, div{"hello"} );
  20. let mut vdom = VirtualDom::new(App);
  21. let edits = vdom.rebuild();
  22. }
  23. #[test]
  24. fn fragments_work() {
  25. static App: Component = |cx| {
  26. cx.render(rsx!(
  27. div{"hello"}
  28. div{"goodbye"}
  29. ))
  30. };
  31. let mut vdom = VirtualDom::new(App);
  32. let edits = vdom.rebuild();
  33. // should result in a final "appendchildren n=2"
  34. dbg!(edits);
  35. }
  36. #[test]
  37. fn lists_work() {
  38. static App: Component = |cx| {
  39. cx.render(rsx!(
  40. h1 {"hello"}
  41. (0..6).map(|f| rsx!(span{ "{f}" }))
  42. ))
  43. };
  44. let mut vdom = VirtualDom::new(App);
  45. let edits = vdom.rebuild();
  46. dbg!(edits);
  47. }
  48. #[test]
  49. fn conditional_rendering() {
  50. static App: Component = |cx| {
  51. cx.render(rsx!(
  52. h1 {"hello"}
  53. {true.then(|| rsx!(span{ "a" }))}
  54. {false.then(|| rsx!(span{ "b" }))}
  55. ))
  56. };
  57. let mut vdom = VirtualDom::new(App);
  58. let mutations = vdom.rebuild();
  59. assert_eq!(
  60. mutations.edits,
  61. [
  62. CreateElement { root: 1, tag: "h1" },
  63. CreateTextNode {
  64. root: 2,
  65. text: "hello"
  66. },
  67. AppendChildren { many: 1 },
  68. CreateElement {
  69. root: 3,
  70. tag: "span"
  71. },
  72. CreateTextNode { root: 4, text: "a" },
  73. AppendChildren { many: 1 },
  74. CreatePlaceholder { root: 5 },
  75. AppendChildren { many: 3 },
  76. ]
  77. )
  78. }
  79. #[test]
  80. fn child_components() {
  81. static App: Component = |cx| {
  82. cx.render(rsx!(
  83. {true.then(|| rsx!(Child { }))}
  84. {false.then(|| rsx!(Child { }))}
  85. ))
  86. };
  87. static Child: Component = |cx| {
  88. cx.render(rsx!(
  89. h1 {"hello"}
  90. h1 {"goodbye"}
  91. ))
  92. };
  93. let mut vdom = VirtualDom::new(App);
  94. let edits = vdom.rebuild();
  95. dbg!(edits);
  96. }