use crate::json::Value;
use crate::nodes::root::RootNode;
use crate::upstream_tests::support::{create_test_decorator_node, initialize_unit_test, update};
use crate::{
CreateEditorArgs, EditorReadMode, LexicalNode, NodeClone, NodeRef, create_paragraph_node,
create_text_node, get_root, get_selection,
};
#[test]
fn root_node_constructor() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
let root_node = RootNode::new();
let other = RootNode::new();
assert_eq!(format!("{root_node:?}"), format!("{other:?}"));
assert_eq!(root_node.get_type(), "root");
assert_eq!((&root_node as &dyn LexicalNode).get_key(), "root");
assert_eq!(root_node.get_text_content(), "");
Ok(())
});
}
#[test]
fn root_node_export_json() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
let json = RootNode::new().export_json(false);
assert_eq!(json.get("textFormat"), Some(&Value::Undefined));
assert_eq!(json.get("textStyle"), Some(&Value::Undefined));
assert_eq!(
Value::Object(json).to_string(),
r#"{"children":[],"direction":null,"format":"","indent":0,"type":"root","version":1}"#
);
Ok(())
});
}
#[test]
fn root_node_clone() {
let root_node = RootNode::new();
let root_node_clone = root_node.clone_node();
assert!(!std::ptr::addr_eq(
&*root_node_clone as *const dyn LexicalNode,
&root_node as *const RootNode
));
assert_eq!(format!("{root_node_clone:?}"), format!("{root_node:?}"));
}
#[cfg(feature = "html")]
#[test]
fn root_node_create_dom() {
let editor = initialize_unit_test(CreateEditorArgs::default());
let root_node = RootNode::new();
assert!(
root_node
.create_dom(
&NodeRef::new("root"),
&crate::EditorConfig::default(),
&editor
)
.is_err()
);
}
#[test]
fn root_node_update_dom() {
let root_node = RootNode::new();
assert!(!root_node.update_dom(&root_node));
}
#[test]
fn root_node_is_attached() {
let editor = initialize_unit_test(CreateEditorArgs::default());
assert!(editor.read(|| get_root().is_attached()).unwrap());
}
#[test]
fn root_node_is_root_node() {
let root_node = RootNode::new();
assert!((&root_node as &dyn LexicalNode).is::<RootNode>());
}
#[test]
fn cached_get_text_content_with_decorators() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
let paragraph = create_paragraph_node();
get_root().append(std::slice::from_ref(¶graph))?;
paragraph.append(&[create_test_decorator_node()])
});
assert_eq!(
editor
.read_with_mode(EditorReadMode::Latest, || get_root().get_text_content())
.unwrap(),
"Hello world"
);
}
fn anchor_and_focus_nodes() -> Option<(NodeRef, NodeRef)> {
let selection = get_selection()?.range()?;
Some((
selection.anchor.get_node().unwrap(),
selection.focus.get_node().unwrap(),
))
}
#[test]
fn root_node_clear_to_handle_selection_update() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
let paragraph = create_paragraph_node();
get_root().append(std::slice::from_ref(¶graph))?;
let text = create_text_node("Hello");
paragraph.append(std::slice::from_ref(&text))?;
text.select(None, None)?;
Ok(())
});
update(&editor, || get_root().clear());
update(&editor, || {
if let Some((anchor, focus)) = anchor_and_focus_nodes() {
assert_eq!(anchor, get_root());
assert_eq!(focus, get_root());
}
Ok(())
});
}
#[test]
fn root_node_is_selected_when_its_selected_child_is_removed() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
let paragraph = create_paragraph_node();
get_root().append(std::slice::from_ref(¶graph))?;
let text = create_text_node("Hello");
paragraph.append(std::slice::from_ref(&text))?;
text.select(None, None)?;
Ok(())
});
update(&editor, || {
get_root().get_first_child().unwrap().remove(false)
});
update(&editor, || {
if let Some((anchor, focus)) = anchor_and_focus_nodes() {
assert_eq!(anchor, get_root());
assert_eq!(focus, get_root());
}
Ok(())
});
}
#[test]
fn root_node_is_not_selected_when_all_children_are_removed_with_no_selection() {
let editor = initialize_unit_test(CreateEditorArgs::default());
update(&editor, || {
assert!(get_selection().is_none());
get_root().append(&[create_paragraph_node()])?;
assert!(get_selection().is_none());
Ok(())
});
update(&editor, || {
assert!(get_selection().is_none());
get_root().clear()?;
assert!(get_selection().is_none());
Ok(())
});
update(&editor, || {
assert!(get_selection().is_none());
Ok(())
});
}