1
0

vdom_rebuild.rs 2.3 KB

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