block_test.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use flowy_document2::parser::json::block::Block;
  2. use serde_json::json;
  3. #[test]
  4. fn test_empty_data_and_children() {
  5. let json = json!({
  6. "type": "page",
  7. });
  8. let block = serde_json::from_value::<Block>(json).unwrap();
  9. assert_eq!(block.ty, "page");
  10. assert!(block.data.is_empty());
  11. assert!(block.children.is_empty());
  12. }
  13. #[test]
  14. fn test_data() {
  15. let json = json!({
  16. "type": "todo_list",
  17. "data": {
  18. "delta": [{ "insert": "Click anywhere and just start typing." }],
  19. "checked": false
  20. }
  21. });
  22. let block = serde_json::from_value::<Block>(json).unwrap();
  23. assert_eq!(block.ty, "todo_list");
  24. assert_eq!(block.data.len(), 2);
  25. assert_eq!(block.data.get("checked").unwrap(), false);
  26. assert_eq!(
  27. block.data.get("delta").unwrap().to_owned(),
  28. json!([{ "insert": "Click anywhere and just start typing." }])
  29. );
  30. assert!(block.children.is_empty());
  31. }
  32. #[test]
  33. fn test_children() {
  34. let json = json!({
  35. "type": "page",
  36. "children": [
  37. {
  38. "type": "heading",
  39. "data": {
  40. "delta": [{ "insert": "Welcome to AppFlowy!" }],
  41. "level": 1
  42. }
  43. },
  44. {
  45. "type": "todo_list",
  46. "data": {
  47. "delta": [{ "insert": "Welcome to AppFlowy!" }],
  48. "checked": false
  49. }
  50. }
  51. ]});
  52. let block = serde_json::from_value::<Block>(json).unwrap();
  53. assert!(block.data.is_empty());
  54. assert_eq!(block.ty, "page");
  55. assert_eq!(block.children.len(), 2);
  56. // heading
  57. let heading = &block.children[0];
  58. assert_eq!(heading.ty, "heading");
  59. assert_eq!(heading.data.len(), 2);
  60. // todo_list
  61. let todo_list = &block.children[1];
  62. assert_eq!(todo_list.ty, "todo_list");
  63. assert_eq!(todo_list.data.len(), 2);
  64. }
  65. #[test]
  66. fn test_nested_children() {
  67. let json = json!({
  68. "type": "page",
  69. "children": [
  70. {
  71. "type": "paragraph",
  72. "children": [
  73. {
  74. "type": "paragraph",
  75. "children": [
  76. {
  77. "type": "paragraph",
  78. "children": [
  79. {
  80. "type": "paragraph"
  81. }
  82. ]
  83. }
  84. ]
  85. }
  86. ]
  87. }
  88. ]
  89. });
  90. let block = serde_json::from_value::<Block>(json).unwrap();
  91. assert!(block.data.is_empty());
  92. assert_eq!(block.ty, "page");
  93. assert_eq!(
  94. block.children[0].children[0].children[0].children[0].ty,
  95. "paragraph"
  96. );
  97. }