earlyabort.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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::*;
  7. mod test_logging;
  8. use dioxus_core::{DomEdit::*, ScopeId};
  9. const IS_LOGGING_ENABLED: bool = false;
  10. fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
  11. test_logging::set_up_logging(IS_LOGGING_ENABLED);
  12. VirtualDom::new_with_props(app, props)
  13. }
  14. /// This test ensures that if a component aborts early, it is replaced with a placeholder.
  15. /// In debug, this should also toss a warning.
  16. #[test]
  17. fn test_early_abort() {
  18. const app: Component = |cx| {
  19. let val = cx.use_hook(|_| 0);
  20. *val += 1;
  21. if *val == 2 {
  22. return None;
  23. }
  24. rsx!(cx, div { "Hello, world!" })
  25. };
  26. let mut dom = new_dom(app, ());
  27. let edits = dom.rebuild();
  28. assert_eq!(
  29. edits.edits,
  30. [
  31. CreateElement { tag: "div", root: 1 },
  32. CreateTextNode { text: "Hello, world!", root: 2 },
  33. AppendChildren { many: 1 },
  34. AppendChildren { many: 1 },
  35. ]
  36. );
  37. let edits = dom.hard_diff(ScopeId(0));
  38. assert_eq!(
  39. edits.edits,
  40. [CreatePlaceholder { root: 3 }, ReplaceWith { root: 1, m: 1 },],
  41. );
  42. let edits = dom.hard_diff(ScopeId(0));
  43. assert_eq!(
  44. edits.edits,
  45. [
  46. CreateElement { tag: "div", root: 1 }, // keys get reused
  47. CreateTextNode { text: "Hello, world!", root: 2 }, // keys get reused
  48. AppendChildren { many: 1 },
  49. ReplaceWith { root: 3, m: 1 },
  50. ]
  51. );
  52. }