vdom_rebuild.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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| render!(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: Some(1), tag: "template", children: 3 },
  59. CreateElement { root: None, tag: "h1", children: 1 },
  60. CreateTextNode { root: None, text: "hello" },
  61. CreatePlaceholder { root: None },
  62. CreatePlaceholder { root: None },
  63. CloneNodeChildren { id: Some(1), new_ids: vec![2, 3, 4] },
  64. CreateElement { root: Some(5), tag: "template", children: 1 },
  65. CreateElement { root: None, tag: "span", children: 1 },
  66. CreateTextNode { root: None, text: "a" },
  67. CloneNodeChildren { id: Some(5), new_ids: vec![6] },
  68. SetLastNode { id: 3 },
  69. ReplaceWith { root: None, nodes: vec![6] },
  70. CreatePlaceholder { root: Some(7) },
  71. SetLastNode { id: 4 },
  72. ReplaceWith { root: None, nodes: vec![7] },
  73. AppendChildren { root: Some(0), children: vec![2, 3, 4] }
  74. ]
  75. )
  76. }
  77. #[test]
  78. fn child_components() {
  79. static App: Component = |cx| {
  80. cx.render(rsx!(
  81. {true.then(|| rsx!(Child { }))}
  82. {false.then(|| rsx!(Child { }))}
  83. ))
  84. };
  85. static Child: Component = |cx| {
  86. cx.render(rsx!(
  87. h1 {"hello"}
  88. h1 {"goodbye"}
  89. ))
  90. };
  91. let mut vdom = VirtualDom::new(App);
  92. let edits = vdom.rebuild();
  93. dbg!(edits);
  94. }