use console_error_panic_hook;
use percy_dom::event::VirtualEvents;
use percy_dom::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Element, Node};
pub struct DiffPatchTest<'a> {
pub desc: &'static str,
pub old: VirtualNode,
pub new: VirtualNode,
pub override_expected: Option<&'a str>,
}
impl<'a> DiffPatchTest<'a> {
pub fn test(&mut self) {
console_error_panic_hook::set_once();
let mut events = VirtualEvents::new();
let (root_node, enode) = self.old.create_dom_node(&mut events);
events.set_root(enode);
let patched_root_node: Node = root_node.clone();
let patches = percy_dom::diff(&self.old, &self.new);
percy_dom::patch(root_node, &self.new, &mut events, &patches).unwrap();
let expected_outer_html = match self.override_expected {
Some(ref expected) => expected.to_string(),
None => self.new.to_string(),
};
let actual_outer_html = match patched_root_node.node_type() {
Node::ELEMENT_NODE => patched_root_node.unchecked_into::<Element>().outer_html(),
Node::TEXT_NODE => patched_root_node.text_content().unwrap_or("".into()),
_ => panic!("Unhandled node type"),
};
assert_eq!(&actual_outer_html, &expected_outer_html, "{}", self.desc);
}
}