earlyabort.rs 1.7 KB

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