mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: add transform field tests (#1504)
Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
use crate::grid::grid_editor::GridEditorTest;
|
||||
use flowy_grid::entities::{CreateFieldParams, FieldChangesetParams};
|
||||
use flowy_grid::entities::{CreateFieldParams, FieldChangesetParams, FieldType};
|
||||
use grid_rev_model::FieldRevision;
|
||||
|
||||
pub enum FieldScript {
|
||||
@ -12,6 +12,14 @@ pub enum FieldScript {
|
||||
DeleteField {
|
||||
field_rev: FieldRevision,
|
||||
},
|
||||
SwitchToField {
|
||||
field_id: String,
|
||||
new_field_type: FieldType,
|
||||
},
|
||||
UpdateTypeOption {
|
||||
field_id: String,
|
||||
type_option: Vec<u8>,
|
||||
},
|
||||
AssertFieldCount(usize),
|
||||
AssertFieldFrozen {
|
||||
field_index: usize,
|
||||
@ -71,6 +79,25 @@ impl GridFieldTest {
|
||||
self.field_revs = self.editor.get_field_revs(None).await.unwrap();
|
||||
assert_eq!(self.field_count, self.field_revs.len());
|
||||
}
|
||||
FieldScript::SwitchToField {
|
||||
field_id,
|
||||
new_field_type,
|
||||
} => {
|
||||
//
|
||||
self.editor
|
||||
.switch_to_field_type(&field_id, &new_field_type)
|
||||
.await
|
||||
.unwrap();
|
||||
self.field_revs = self.editor.get_field_revs(None).await.unwrap();
|
||||
}
|
||||
FieldScript::UpdateTypeOption { field_id, type_option } => {
|
||||
//
|
||||
self.editor
|
||||
.update_field_type_option(&self.grid_id, &field_id, type_option, None)
|
||||
.await
|
||||
.unwrap();
|
||||
self.field_revs = self.editor.get_field_revs(None).await.unwrap();
|
||||
}
|
||||
FieldScript::AssertFieldCount(count) => {
|
||||
assert_eq!(self.editor.get_field_revs(None).await.unwrap().len(), count);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
use crate::grid::field_test::script::FieldScript::*;
|
||||
use crate::grid::field_test::script::GridFieldTest;
|
||||
use crate::grid::field_test::util::*;
|
||||
use flowy_grid::entities::FieldChangesetParams;
|
||||
use bytes::Bytes;
|
||||
use flowy_grid::entities::{FieldChangesetParams, FieldType};
|
||||
use flowy_grid::services::field::selection_type_option::SelectOptionPB;
|
||||
use flowy_grid::services::field::SingleSelectTypeOptionPB;
|
||||
use flowy_grid::services::field::{gen_option_id, SingleSelectTypeOptionPB, CHECK, UNCHECK};
|
||||
|
||||
#[tokio::test]
|
||||
async fn grid_create_field() {
|
||||
@ -121,3 +122,60 @@ async fn grid_delete_field() {
|
||||
];
|
||||
test.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn grid_switch_from_select_option_to_checkbox_test() {
|
||||
let mut test = GridFieldTest::new().await;
|
||||
let field_rev = test.get_first_field_rev(FieldType::SingleSelect);
|
||||
|
||||
// Update the type option data of single select option
|
||||
let mut single_select_type_option = test.get_single_select_type_option(&field_rev.id);
|
||||
single_select_type_option.options.clear();
|
||||
// Add a new option with name CHECK
|
||||
single_select_type_option.options.push(SelectOptionPB {
|
||||
id: gen_option_id(),
|
||||
name: CHECK.to_string(),
|
||||
color: Default::default(),
|
||||
});
|
||||
// Add a new option with name UNCHECK
|
||||
single_select_type_option.options.push(SelectOptionPB {
|
||||
id: gen_option_id(),
|
||||
name: UNCHECK.to_string(),
|
||||
color: Default::default(),
|
||||
});
|
||||
|
||||
let bytes: Bytes = single_select_type_option.try_into().unwrap();
|
||||
let scripts = vec![
|
||||
UpdateTypeOption {
|
||||
field_id: field_rev.id.clone(),
|
||||
type_option: bytes.to_vec(),
|
||||
},
|
||||
SwitchToField {
|
||||
field_id: field_rev.id.clone(),
|
||||
new_field_type: FieldType::Checkbox,
|
||||
},
|
||||
];
|
||||
test.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn grid_switch_from_checkbox_to_select_option_test() {
|
||||
let mut test = GridFieldTest::new().await;
|
||||
let field_rev = test.get_first_field_rev(FieldType::Checkbox).clone();
|
||||
let scripts = vec![SwitchToField {
|
||||
field_id: field_rev.id.clone(),
|
||||
new_field_type: FieldType::SingleSelect,
|
||||
}];
|
||||
test.run_scripts(scripts).await;
|
||||
|
||||
let single_select_type_option = test.get_single_select_type_option(&field_rev.id);
|
||||
assert_eq!(single_select_type_option.options.len(), 2);
|
||||
assert!(single_select_type_option
|
||||
.options
|
||||
.iter()
|
||||
.any(|option| option.name == UNCHECK));
|
||||
assert!(single_select_type_option
|
||||
.options
|
||||
.iter()
|
||||
.any(|option| option.name == CHECK));
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ impl GridFilterTest {
|
||||
}
|
||||
FilterScript::CreateTextFilter { condition, content} => {
|
||||
|
||||
let field_rev = self.get_field_rev(FieldType::RichText);
|
||||
let field_rev = self.get_first_field_rev(FieldType::RichText);
|
||||
let text_filter= TextFilterPB {
|
||||
condition,
|
||||
content
|
||||
@ -139,7 +139,7 @@ impl GridFilterTest {
|
||||
self.editor.create_or_update_filter(params).await.unwrap();
|
||||
}
|
||||
FilterScript::CreateNumberFilter {condition, content} => {
|
||||
let field_rev = self.get_field_rev(FieldType::Number);
|
||||
let field_rev = self.get_first_field_rev(FieldType::Number);
|
||||
let number_filter = NumberFilterPB {
|
||||
condition,
|
||||
content
|
||||
@ -149,7 +149,7 @@ impl GridFilterTest {
|
||||
self.insert_filter(payload).await;
|
||||
}
|
||||
FilterScript::CreateCheckboxFilter {condition} => {
|
||||
let field_rev = self.get_field_rev(FieldType::Checkbox);
|
||||
let field_rev = self.get_first_field_rev(FieldType::Checkbox);
|
||||
let checkbox_filter = CheckboxFilterPB {
|
||||
condition
|
||||
};
|
||||
@ -158,7 +158,7 @@ impl GridFilterTest {
|
||||
self.insert_filter(payload).await;
|
||||
}
|
||||
FilterScript::CreateDateFilter { condition, start, end, timestamp} => {
|
||||
let field_rev = self.get_field_rev(FieldType::DateTime);
|
||||
let field_rev = self.get_first_field_rev(FieldType::DateTime);
|
||||
let date_filter = DateFilterPB {
|
||||
condition,
|
||||
start,
|
||||
@ -171,14 +171,14 @@ impl GridFilterTest {
|
||||
self.insert_filter(payload).await;
|
||||
}
|
||||
FilterScript::CreateMultiSelectFilter { condition, option_ids} => {
|
||||
let field_rev = self.get_field_rev(FieldType::MultiSelect);
|
||||
let field_rev = self.get_first_field_rev(FieldType::MultiSelect);
|
||||
let filter = SelectOptionFilterPB { condition, option_ids };
|
||||
let payload =
|
||||
AlterFilterPayloadPB::new(field_rev, filter);
|
||||
self.insert_filter(payload).await;
|
||||
}
|
||||
FilterScript::CreateSingleSelectFilter { condition, option_ids} => {
|
||||
let field_rev = self.get_field_rev(FieldType::SingleSelect);
|
||||
let field_rev = self.get_first_field_rev(FieldType::SingleSelect);
|
||||
let filter = SelectOptionFilterPB { condition, option_ids };
|
||||
let payload =
|
||||
AlterFilterPayloadPB::new(field_rev, filter);
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::grid::filter_test::script::FilterScript::*;
|
||||
use crate::grid::filter_test::script::GridFilterTest;
|
||||
use flowy_grid::entities::SelectOptionCondition;
|
||||
use flowy_grid::entities::{FieldType, SelectOptionCondition};
|
||||
|
||||
#[tokio::test]
|
||||
async fn grid_filter_multi_select_is_empty_test() {
|
||||
@ -31,7 +31,8 @@ async fn grid_filter_multi_select_is_not_empty_test() {
|
||||
#[tokio::test]
|
||||
async fn grid_filter_multi_select_is_test() {
|
||||
let mut test = GridFilterTest::new().await;
|
||||
let mut options = test.get_multi_select_type_option();
|
||||
let field_rev = test.get_first_field_rev(FieldType::MultiSelect);
|
||||
let mut options = test.get_multi_select_type_option(&field_rev.id);
|
||||
let scripts = vec![
|
||||
CreateMultiSelectFilter {
|
||||
condition: SelectOptionCondition::OptionIs,
|
||||
@ -45,7 +46,8 @@ async fn grid_filter_multi_select_is_test() {
|
||||
#[tokio::test]
|
||||
async fn grid_filter_multi_select_is_test2() {
|
||||
let mut test = GridFilterTest::new().await;
|
||||
let mut options = test.get_multi_select_type_option();
|
||||
let field_rev = test.get_first_field_rev(FieldType::MultiSelect);
|
||||
let mut options = test.get_multi_select_type_option(&field_rev.id);
|
||||
let scripts = vec![
|
||||
CreateMultiSelectFilter {
|
||||
condition: SelectOptionCondition::OptionIs,
|
||||
@ -72,7 +74,8 @@ async fn grid_filter_single_select_is_empty_test() {
|
||||
#[tokio::test]
|
||||
async fn grid_filter_single_select_is_test() {
|
||||
let mut test = GridFilterTest::new().await;
|
||||
let mut options = test.get_single_select_type_option();
|
||||
let field_rev = test.get_first_field_rev(FieldType::SingleSelect);
|
||||
let mut options = test.get_single_select_type_option(&field_rev.id).options;
|
||||
let scripts = vec![
|
||||
CreateSingleSelectFilter {
|
||||
condition: SelectOptionCondition::OptionIs,
|
||||
@ -86,7 +89,8 @@ async fn grid_filter_single_select_is_test() {
|
||||
#[tokio::test]
|
||||
async fn grid_filter_single_select_is_test2() {
|
||||
let mut test = GridFilterTest::new().await;
|
||||
let mut options = test.get_single_select_type_option();
|
||||
let field_rev = test.get_first_field_rev(FieldType::SingleSelect);
|
||||
let mut options = test.get_single_select_type_option(&field_rev.id).options;
|
||||
let option = options.remove(0);
|
||||
let scripts = vec![
|
||||
CreateSingleSelectFilter {
|
||||
|
@ -38,7 +38,7 @@ async fn grid_filter_text_is_not_empty_test() {
|
||||
test.run_scripts(scripts).await;
|
||||
|
||||
let filter = test.grid_filters().await.pop().unwrap();
|
||||
let field_rev = test.get_field_rev(FieldType::RichText).clone();
|
||||
let field_rev = test.get_first_field_rev(FieldType::RichText).clone();
|
||||
test.run_scripts(vec![
|
||||
DeleteFilter {
|
||||
filter_id: filter.id,
|
||||
@ -184,7 +184,7 @@ async fn grid_update_text_filter_test() {
|
||||
#[tokio::test]
|
||||
async fn grid_filter_delete_test() {
|
||||
let mut test = GridFilterTest::new().await;
|
||||
let field_rev = test.get_field_rev(FieldType::RichText).clone();
|
||||
let field_rev = test.get_first_field_rev(FieldType::RichText).clone();
|
||||
let text_filter = TextFilterPB {
|
||||
condition: TextFilterCondition::TextIsEmpty,
|
||||
content: "".to_string(),
|
||||
|
@ -89,7 +89,19 @@ impl GridEditorTest {
|
||||
self.editor.get_all_filters().await.unwrap()
|
||||
}
|
||||
|
||||
pub fn get_field_rev(&self, field_type: FieldType) -> &Arc<FieldRevision> {
|
||||
pub fn get_field_rev(&self, field_id: &str, field_type: FieldType) -> &Arc<FieldRevision> {
|
||||
self.field_revs
|
||||
.iter()
|
||||
.filter(|field_rev| {
|
||||
let t_field_type: FieldType = field_rev.ty.into();
|
||||
field_rev.id == field_id && t_field_type == field_type
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.pop()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn get_first_field_rev(&self, field_type: FieldType) -> &Arc<FieldRevision> {
|
||||
self.field_revs
|
||||
.iter()
|
||||
.filter(|field_rev| {
|
||||
@ -101,23 +113,33 @@ impl GridEditorTest {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn get_multi_select_type_option(&self) -> Vec<SelectOptionPB> {
|
||||
pub fn get_multi_select_type_option(&self, field_id: &str) -> Vec<SelectOptionPB> {
|
||||
let field_type = FieldType::MultiSelect;
|
||||
let field_rev = self.get_field_rev(field_type.clone());
|
||||
let field_rev = self.get_field_rev(field_id, field_type.clone());
|
||||
let type_option = field_rev
|
||||
.get_type_option::<MultiSelectTypeOptionPB>(field_type.into())
|
||||
.unwrap();
|
||||
type_option.options
|
||||
}
|
||||
|
||||
pub fn get_single_select_type_option(&self) -> Vec<SelectOptionPB> {
|
||||
pub fn get_single_select_type_option(&self, field_id: &str) -> SingleSelectTypeOptionPB {
|
||||
let field_type = FieldType::SingleSelect;
|
||||
let field_rev = self.get_field_rev(field_type.clone());
|
||||
let field_rev = self.get_field_rev(field_id, field_type.clone());
|
||||
let type_option = field_rev
|
||||
.get_type_option::<SingleSelectTypeOptionPB>(field_type.into())
|
||||
.unwrap();
|
||||
type_option.options
|
||||
type_option
|
||||
}
|
||||
|
||||
pub fn get_checkbox_type_option(&self, field_id: &str) -> CheckboxTypeOptionPB {
|
||||
let field_type = FieldType::Checkbox;
|
||||
let field_rev = self.get_field_rev(field_id, field_type.clone());
|
||||
let type_option = field_rev
|
||||
.get_type_option::<CheckboxTypeOptionPB>(field_type.into())
|
||||
.unwrap();
|
||||
type_option
|
||||
}
|
||||
|
||||
pub fn block_id(&self) -> &str {
|
||||
&self.block_meta_revs.last().unwrap().block_id
|
||||
}
|
||||
|
Reference in New Issue
Block a user