editor_test.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use super::script::{NodeScript::*, *};
  2. use lib_ot::core::AttributeBuilder;
  3. use lib_ot::{
  4. core::{NodeData, Path},
  5. text_delta::TextOperationBuilder,
  6. };
  7. #[test]
  8. fn editor_deserialize_node_test() {
  9. let mut test = NodeTest::new();
  10. let node: NodeData = serde_json::from_str(EXAMPLE_JSON).unwrap();
  11. let path: Path = 0.into();
  12. let expected_delta = TextOperationBuilder::new()
  13. .insert("👋 ")
  14. .insert_with_attributes(
  15. "Welcome to ",
  16. AttributeBuilder::new().insert("href", "appflowy.io").build(),
  17. )
  18. .insert_with_attributes(
  19. "AppFlowy Editor",
  20. AttributeBuilder::new().insert("italic", true).build(),
  21. )
  22. .build();
  23. test.run_scripts(vec![
  24. InsertNode {
  25. path,
  26. node_data: node.clone(),
  27. rev_id: 1,
  28. },
  29. AssertNumberOfNodesAtPath { path: None, len: 1 },
  30. AssertNumberOfNodesAtPath {
  31. path: Some(0.into()),
  32. len: 2,
  33. },
  34. AssertNodeDelta {
  35. path: vec![0, 1].into(),
  36. expected: expected_delta,
  37. },
  38. AssertNodeData {
  39. path: vec![0, 0].into(),
  40. expected: Some(node.children[0].clone()),
  41. },
  42. AssertNodeJSON {
  43. expected: EXAMPLE_JSON.to_string(),
  44. },
  45. ]);
  46. }
  47. #[allow(dead_code)]
  48. const EXAMPLE_JSON: &str = r#"{
  49. "type": "editor",
  50. "children": [
  51. {
  52. "type": "image",
  53. "attributes": {
  54. "image_src": "https://s1.ax1x.com/2022/08/26/v2sSbR.jpg"
  55. }
  56. },
  57. {
  58. "type": "text",
  59. "attributes": {
  60. "heading": "h1"
  61. },
  62. "body": {
  63. "delta": [
  64. {
  65. "insert": "👋 "
  66. },
  67. {
  68. "insert": "Welcome to ",
  69. "attributes": {
  70. "href": "appflowy.io"
  71. }
  72. },
  73. {
  74. "insert": "AppFlowy Editor",
  75. "attributes": {
  76. "italic": true
  77. }
  78. }
  79. ]
  80. }
  81. }
  82. ]
  83. }"#;