1
0

cycle.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use 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(|cx| {
  7. cx.render(match cx.generation() % 2 {
  8. 0 => rsx! { div { "wasd" } },
  9. 1 => rsx! { div { "abcd" } },
  10. _ => unreachable!(),
  11. })
  12. });
  13. {
  14. let edits = dom.rebuild().santize();
  15. assert_eq!(
  16. edits.edits,
  17. [
  18. LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
  19. AppendChildren { m: 1, id: ElementId(0) },
  20. ]
  21. );
  22. }
  23. dom.mark_dirty(ScopeId(0));
  24. assert_eq!(
  25. dom.render_immediate().santize().edits,
  26. [
  27. LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
  28. ReplaceWith { id: ElementId(1,), m: 1 },
  29. ]
  30. );
  31. // notice that the IDs cycle back to ElementId(1), preserving a minimal memory footprint
  32. dom.mark_dirty(ScopeId(0));
  33. assert_eq!(
  34. dom.render_immediate().santize().edits,
  35. [
  36. LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
  37. ReplaceWith { id: ElementId(2,), m: 1 },
  38. ]
  39. );
  40. dom.mark_dirty(ScopeId(0));
  41. assert_eq!(
  42. dom.render_immediate().santize().edits,
  43. [
  44. LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
  45. ReplaceWith { id: ElementId(1,), m: 1 },
  46. ]
  47. );
  48. }