diff_dynamic_node.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use dioxus::dioxus_core::{ElementId, Mutation::*};
  2. use dioxus::prelude::*;
  3. use pretty_assertions::assert_eq;
  4. #[test]
  5. fn toggle_option_text() {
  6. let mut dom = VirtualDom::new(|| {
  7. let gen = generation();
  8. let text = if gen % 2 != 0 { Some("hello") } else { None };
  9. println!("{:?}", text);
  10. rsx! {
  11. div {
  12. {text}
  13. }
  14. }
  15. });
  16. // load the div and then assign the None as a placeholder
  17. assert_eq!(
  18. dom.rebuild_to_vec().edits,
  19. [
  20. LoadTemplate { index: 0, id: ElementId(1,) },
  21. CreatePlaceholder { id: ElementId(2,) },
  22. ReplacePlaceholder { path: &[0], m: 1 },
  23. AppendChildren { id: ElementId(0), m: 1 },
  24. ]
  25. );
  26. // Rendering again should replace the placeholder with an text node
  27. dom.mark_dirty(ScopeId::APP);
  28. assert_eq!(
  29. dom.render_immediate_to_vec().edits,
  30. [
  31. CreateTextNode { value: "hello".to_string(), id: ElementId(3,) },
  32. ReplaceWith { id: ElementId(2,), m: 1 },
  33. ]
  34. );
  35. // Rendering again should replace the placeholder with an text node
  36. dom.mark_dirty(ScopeId::APP);
  37. assert_eq!(
  38. dom.render_immediate_to_vec().edits,
  39. [
  40. CreatePlaceholder { id: ElementId(2,) },
  41. ReplaceWith { id: ElementId(3,), m: 1 },
  42. ]
  43. );
  44. }