earlyabort.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. CreateTemplate { id: 0 },
  30. CreateElementTemplate {
  31. root: 4503599627370495,
  32. tag: "div",
  33. locally_static: true,
  34. fully_static: true
  35. },
  36. CreateTextNodeTemplate {
  37. root: 4503599627370496,
  38. text: "Hello, world!",
  39. locally_static: true
  40. },
  41. AppendChildren { many: 1 },
  42. FinishTemplate { len: 1 },
  43. CreateTemplateRef { id: 1, template_id: 0 },
  44. AppendChildren { many: 1 }
  45. ]
  46. );
  47. let edits = dom.hard_diff(ScopeId(0));
  48. assert_eq!(
  49. edits.edits,
  50. [CreatePlaceholder { root: 2 }, ReplaceWith { root: 1, m: 1 },],
  51. );
  52. let edits = dom.hard_diff(ScopeId(0));
  53. assert_eq!(
  54. edits.edits,
  55. [
  56. CreateTemplateRef { id: 1, template_id: 0 }, // gets reused
  57. ReplaceWith { root: 2, m: 1 }
  58. ]
  59. );
  60. }