forked from AppFlowy-IO/AppFlowy
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add document test (AppFlowy-IO#2932)
- Loading branch information
Showing
9 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod document_event; | ||
pub mod text_block_event; | ||
pub mod utils; |
125 changes: 125 additions & 0 deletions
125
frontend/rust-lib/flowy-test/src/document/text_block_event.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use crate::document::document_event::DocumentEventTest; | ||
use crate::document::utils::{gen_id, gen_text_block_data}; | ||
use flowy_document2::entities::*; | ||
use std::sync::Arc; | ||
|
||
const TEXT_BLOCK_TY: &str = "paragraph"; | ||
|
||
pub struct TextBlockEventTest { | ||
doc: Arc<DocumentEventTest>, | ||
doc_id: String, | ||
} | ||
|
||
impl TextBlockEventTest { | ||
pub async fn new() -> Self { | ||
let doc = DocumentEventTest::new().await; | ||
let doc_id = doc.create_document().await.id; | ||
Self { | ||
doc: Arc::new(doc), | ||
doc_id, | ||
} | ||
} | ||
|
||
pub async fn get(&self, block_id: &str) -> Option<BlockPB> { | ||
let doc = self.doc.clone(); | ||
let doc_id = self.doc_id.clone(); | ||
doc.get_block(&doc_id, block_id).await | ||
} | ||
|
||
/// Insert a new text block at the index of parent's children. | ||
pub async fn insert_index(&self, text: String, index: usize, parent_id: Option<&str>) -> String { | ||
let doc = self.doc.clone(); | ||
let doc_id = self.doc_id.clone(); | ||
let page_id = self.doc.get_page_id(&doc_id).await; | ||
let parent_id = parent_id | ||
.map(|id| id.to_string()) | ||
.unwrap_or_else(|| page_id); | ||
let parent_children = self.doc.get_block_children(&doc_id, &parent_id).await; | ||
|
||
let prev_id = { | ||
// If index is 0, then the new block will be the first child of parent. | ||
if index == 0 { | ||
None | ||
} else { | ||
parent_children.and_then(|children| { | ||
// If index is greater than the length of children, then the new block will be the last child of parent. | ||
if index >= children.len() { | ||
children.last().cloned() | ||
} else { | ||
children.get(index - 1).cloned() | ||
} | ||
}) | ||
} | ||
}; | ||
|
||
let new_block_id = gen_id(); | ||
let data = gen_text_block_data(text); | ||
|
||
let new_block = BlockPB { | ||
id: new_block_id.clone(), | ||
ty: TEXT_BLOCK_TY.to_string(), | ||
data, | ||
parent_id: parent_id.clone(), | ||
children_id: gen_id(), | ||
}; | ||
let action = BlockActionPB { | ||
action: BlockActionTypePB::Insert, | ||
payload: BlockActionPayloadPB { | ||
block: new_block, | ||
prev_id, | ||
parent_id: Some(parent_id), | ||
}, | ||
}; | ||
let payload = ApplyActionPayloadPB { | ||
document_id: doc_id, | ||
actions: vec![action], | ||
}; | ||
doc.apply_actions(payload).await; | ||
new_block_id | ||
} | ||
|
||
pub async fn update(&self, block_id: &str, text: String) { | ||
let doc = self.doc.clone(); | ||
let doc_id = self.doc_id.clone(); | ||
let block = self.get(block_id).await.unwrap(); | ||
let data = gen_text_block_data(text); | ||
let new_block = { | ||
let mut new_block = block.clone(); | ||
new_block.data = data; | ||
new_block | ||
}; | ||
let action = BlockActionPB { | ||
action: BlockActionTypePB::Update, | ||
payload: BlockActionPayloadPB { | ||
block: new_block, | ||
prev_id: None, | ||
parent_id: Some(block.parent_id.clone()), | ||
}, | ||
}; | ||
let payload = ApplyActionPayloadPB { | ||
document_id: doc_id, | ||
actions: vec![action], | ||
}; | ||
doc.apply_actions(payload).await; | ||
} | ||
|
||
pub async fn delete(&self, block_id: &str) { | ||
let doc = self.doc.clone(); | ||
let doc_id = self.doc_id.clone(); | ||
let block = self.get(block_id).await.unwrap(); | ||
let parent_id = block.parent_id.clone(); | ||
let action = BlockActionPB { | ||
action: BlockActionTypePB::Delete, | ||
payload: BlockActionPayloadPB { | ||
block, | ||
prev_id: None, | ||
parent_id: Some(parent_id), | ||
}, | ||
}; | ||
let payload = ApplyActionPayloadPB { | ||
document_id: doc_id, | ||
actions: vec![action], | ||
}; | ||
doc.apply_actions(payload).await; | ||
} | ||
} |
12 changes: 11 additions & 1 deletion
12
...st-lib/flowy-test/tests/document/utils.rs → ...rust-lib/flowy-test/src/document/utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use flowy_test::document::text_block_event::TextBlockEventTest; | ||
use flowy_test::document::utils::gen_text_block_data; | ||
|
||
#[tokio::test] | ||
async fn insert_text_block_test() { | ||
let test = TextBlockEventTest::new().await; | ||
let text = "Hello World".to_string(); | ||
let block_id = test.insert_index(text.clone(), 1, None).await; | ||
let block = test.get(&block_id).await; | ||
assert!(block.is_some()); | ||
let block = block.unwrap(); | ||
let data = gen_text_block_data(text); | ||
assert_eq!(block.data, data); | ||
} | ||
|
||
#[tokio::test] | ||
async fn update_text_block_test() { | ||
let test = TextBlockEventTest::new().await; | ||
let insert_text = "Hello World".to_string(); | ||
let block_id = test.insert_index(insert_text.clone(), 1, None).await; | ||
let update_text = "Hello World 2".to_string(); | ||
test.update(&block_id, update_text.clone()).await; | ||
let block = test.get(&block_id).await; | ||
assert!(block.is_some()); | ||
let block = block.unwrap(); | ||
let update_data = gen_text_block_data(update_text); | ||
assert_eq!(block.data, update_data); | ||
} |
4 changes: 2 additions & 2 deletions
4
...ust-lib/flowy-test/tests/document/test.rs → ...lowy-test/tests/document/document_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
mod test; | ||
mod utils; | ||
mod block_test; | ||
mod document_test; |