vdom_rebuild.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 as dioxus;
  13. use dioxus_core_macro::*;
  14. use dioxus_html as dioxus_elements;
  15. #[test]
  16. fn app_runs() {
  17. static App: FC<()> = |cx, props| {
  18. //
  19. cx.render(rsx!( div{"hello"} ))
  20. };
  21. let mut vdom = VirtualDom::new(App);
  22. let edits = vdom.rebuild();
  23. dbg!(edits);
  24. }
  25. #[test]
  26. fn fragments_work() {
  27. static App: FC<()> = |cx, props| {
  28. cx.render(rsx!(
  29. div{"hello"}
  30. div{"goodbye"}
  31. ))
  32. };
  33. let mut vdom = VirtualDom::new(App);
  34. let edits = vdom.rebuild();
  35. // should result in a final "appendchildren n=2"
  36. dbg!(edits);
  37. }
  38. #[test]
  39. fn lists_work() {
  40. static App: FC<()> = |cx, props| {
  41. cx.render(rsx!(
  42. h1 {"hello"}
  43. {(0..6).map(|f| rsx!(span{ "{f}" }))}
  44. ))
  45. };
  46. let mut vdom = VirtualDom::new(App);
  47. let edits = vdom.rebuild();
  48. dbg!(edits);
  49. }
  50. #[test]
  51. fn conditional_rendering() {
  52. static App: FC<()> = |cx, props| {
  53. cx.render(rsx!(
  54. h1 {"hello"}
  55. {true.then(|| rsx!(span{ "a" }))}
  56. {false.then(|| rsx!(span{ "b" }))}
  57. ))
  58. };
  59. let mut vdom = VirtualDom::new(App);
  60. let mutations = vdom.rebuild();
  61. dbg!(&mutations);
  62. // the "false" fragment should generate an empty placeholder to re-visit
  63. assert!(mutations.edits[mutations.edits.len() - 2].is("CreatePlaceholder"));
  64. }
  65. #[test]
  66. fn child_components() {
  67. static App: FC<()> = |cx, props| {
  68. cx.render(rsx!(
  69. {true.then(|| rsx!(Child { }))}
  70. {false.then(|| rsx!(Child { }))}
  71. ))
  72. };
  73. static Child: FC<()> = |cx, props| {
  74. cx.render(rsx!(
  75. h1 {"hello"}
  76. h1 {"goodbye"}
  77. ))
  78. };
  79. let mut vdom = VirtualDom::new(App);
  80. let edits = vdom.rebuild();
  81. dbg!(edits);
  82. }