vdom_rebuild.rs 2.5 KB

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