cycle.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use dioxus::dioxus_core::{ElementId, Mutation::*};
  2. use dioxus::prelude::*;
  3. /// As we clean up old templates, the ID for the node should cycle
  4. #[test]
  5. fn cycling_elements() {
  6. let mut dom = VirtualDom::new(|| match generation() % 2 {
  7. 0 => rsx! { div { "wasd" } },
  8. 1 => rsx! { div { "abcd" } },
  9. _ => unreachable!(),
  10. });
  11. {
  12. let edits = dom.rebuild_to_vec().sanitize();
  13. assert_eq!(
  14. edits.edits,
  15. [
  16. LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
  17. AppendChildren { m: 1, id: ElementId(0) },
  18. ]
  19. );
  20. }
  21. dom.mark_dirty(ScopeId::APP);
  22. assert_eq!(
  23. dom.render_immediate_to_vec().sanitize().edits,
  24. [
  25. LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
  26. ReplaceWith { id: ElementId(1,), m: 1 },
  27. ]
  28. );
  29. // notice that the IDs cycle back to ElementId(1), preserving a minimal memory footprint
  30. dom.mark_dirty(ScopeId::APP);
  31. assert_eq!(
  32. dom.render_immediate_to_vec().sanitize().edits,
  33. [
  34. LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
  35. ReplaceWith { id: ElementId(2,), m: 1 },
  36. ]
  37. );
  38. dom.mark_dirty(ScopeId::APP);
  39. assert_eq!(
  40. dom.render_immediate_to_vec().sanitize().edits,
  41. [
  42. LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
  43. ReplaceWith { id: ElementId(1,), m: 1 },
  44. ]
  45. );
  46. }