earlyabort.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. //! Prove that the dom works normally through virtualdom methods.
  3. //!
  4. //! This methods all use "rebuild" which completely bypasses the scheduler.
  5. //! Hard rebuilds don't consume any events from the event queue.
  6. use dioxus::{prelude::*, DomEdit, ScopeId};
  7. use dioxus_core as dioxus;
  8. use dioxus_core_macro::*;
  9. use dioxus_html as dioxus_elements;
  10. mod test_logging;
  11. use DomEdit::*;
  12. const IS_LOGGING_ENABLED: bool = false;
  13. fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
  14. test_logging::set_up_logging(IS_LOGGING_ENABLED);
  15. VirtualDom::new_with_props(app, props)
  16. }
  17. /// This test ensures that if a component aborts early, it is replaced with a placeholder.
  18. /// In debug, this should also toss a warning.
  19. #[test]
  20. fn test_early_abort() {
  21. const app: Component = |cx| {
  22. let val = cx.use_hook(|_| 0);
  23. *val += 1;
  24. if *val == 2 {
  25. return None;
  26. }
  27. rsx!(cx, div { "Hello, world!" })
  28. };
  29. let mut dom = new_dom(app, ());
  30. let edits = dom.rebuild();
  31. assert_eq!(
  32. edits.edits,
  33. [
  34. CreateElement {
  35. tag: "div",
  36. root: 1,
  37. },
  38. CreateTextNode {
  39. text: "Hello, world!",
  40. root: 2,
  41. },
  42. AppendChildren { many: 1 },
  43. AppendChildren { many: 1 },
  44. ]
  45. );
  46. let edits = dom.hard_diff(ScopeId(0));
  47. assert_eq!(
  48. edits.edits,
  49. [CreatePlaceholder { root: 3 }, ReplaceWith { root: 1, m: 1 },],
  50. );
  51. let edits = dom.hard_diff(ScopeId(0));
  52. assert_eq!(
  53. edits.edits,
  54. [
  55. CreateElement {
  56. tag: "div",
  57. root: 2,
  58. },
  59. CreateTextNode {
  60. text: "Hello, world!",
  61. root: 4,
  62. },
  63. AppendChildren { many: 1 },
  64. ReplaceWith { root: 3, m: 1 },
  65. ]
  66. );
  67. }