vdom_rebuild.rs 2.9 KB

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