earlyabort.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // create template
  30. CreateElement { root: Some(1), tag: "template", children: 1 },
  31. CreateElement { root: None, tag: "div", children: 1 },
  32. CreateTextNode { root: None, text: "Hello, world!" },
  33. // clone template
  34. CloneNodeChildren { id: Some(1), new_ids: vec![2] },
  35. AppendChildren { root: Some(0), children: vec![2] }
  36. ]
  37. );
  38. let edits = dom.hard_diff(ScopeId(0));
  39. assert_eq!(
  40. edits.edits,
  41. [
  42. CreatePlaceholder { root: Some(3) },
  43. ReplaceWith { root: Some(2), nodes: vec![3] }
  44. ]
  45. );
  46. let edits = dom.hard_diff(ScopeId(0));
  47. assert_eq!(
  48. edits.edits,
  49. [
  50. CloneNodeChildren { id: Some(1), new_ids: vec![2] },
  51. ReplaceWith { root: Some(3), nodes: vec![2] }
  52. ]
  53. );
  54. }