vdom_rebuild.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { root: 2, text: "hello" },
  64. AppendChildren { many: 1 },
  65. CreateElement { root: 3, tag: "span" },
  66. CreateTextNode { root: 4, text: "a" },
  67. AppendChildren { many: 1 },
  68. CreatePlaceholder { root: 5 },
  69. AppendChildren { many: 3 },
  70. ]
  71. )
  72. }
  73. #[test]
  74. fn child_components() {
  75. static App: Component = |cx| {
  76. cx.render(rsx!(
  77. {true.then(|| rsx!(Child { }))}
  78. {false.then(|| rsx!(Child { }))}
  79. ))
  80. };
  81. static Child: Component = |cx| {
  82. cx.render(rsx!(
  83. h1 {"hello"}
  84. h1 {"goodbye"}
  85. ))
  86. };
  87. let mut vdom = VirtualDom::new(App);
  88. let edits = vdom.rebuild();
  89. dbg!(edits);
  90. }