vdom_rebuild.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //! Rebuilding tests
  2. //! ----------------
  3. //!
  4. //! This tests module ensures that the initial build of the virtualdom is correct.
  5. //! This does not include dynamic tests or the diffing algorithm itself.
  6. //!
  7. //! It does prove that mounting works properly and the correct edit streams are generated.
  8. //!
  9. //! Don't have a good way to validate, everything is done manually ATM
  10. use dioxus::prelude::*;
  11. use dioxus_core as dioxus;
  12. use dioxus_html as dioxus_elements;
  13. #[test]
  14. fn app_runs() {
  15. static App: FC<()> = |cx, props| {
  16. //
  17. cx.render(rsx!( div{"hello"} ))
  18. };
  19. let mut vdom = VirtualDom::new(App);
  20. let edits = vdom.rebuild();
  21. dbg!(edits);
  22. }
  23. #[test]
  24. fn fragments_work() {
  25. static App: FC<()> = |cx, props| {
  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: FC<()> = |cx, props| {
  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: FC<()> = |cx, props| {
  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. dbg!(&mutations);
  60. // the "false" fragment should generate an empty placeholder to re-visit
  61. assert!(mutations.edits[mutations.edits.len() - 2].is("CreatePlaceholder"));
  62. }
  63. #[test]
  64. fn child_components() {
  65. static App: FC<()> = |cx, props| {
  66. cx.render(rsx!(
  67. {true.then(|| rsx!(Child { }))}
  68. {false.then(|| rsx!(Child { }))}
  69. ))
  70. };
  71. static Child: FC<()> = |cx, props| {
  72. cx.render(rsx!(
  73. h1 {"hello"}
  74. h1 {"goodbye"}
  75. ))
  76. };
  77. let mut vdom = VirtualDom::new(App);
  78. let edits = vdom.rebuild();
  79. dbg!(edits);
  80. }
  81. #[test]
  82. fn suspended_works() {
  83. static App: FC<()> = |cx, props| {
  84. let title = use_suspense(cx, || async { "bob" }, |cx, f| cx.render(rsx! { "{f}"}));
  85. cx.render(rsx!("hello" { title }))
  86. };
  87. let mut vdom = VirtualDom::new(App);
  88. let edits = vdom.rebuild();
  89. dbg!(edits);
  90. }