AppFlowy/frontend/rust-lib/flowy-grid/tests/grid/field_test.rs
2022-06-28 09:35:18 +08:00

115 lines
3.5 KiB
Rust

use crate::grid::field_util::*;
use crate::grid::script::EditorScript::*;
use crate::grid::script::*;
use flowy_grid::services::field::{SelectOption, SingleSelectTypeOption};
use flowy_grid_data_model::entities::FieldChangesetParams;
use flowy_grid_data_model::revision::TypeOptionDataEntry;
#[tokio::test]
async fn grid_create_field() {
let mut test = GridEditorTest::new().await;
let (params, field_rev) = create_text_field(&test.grid_id);
let scripts = vec![
CreateField { params },
AssertFieldEqual {
field_index: test.field_count,
field_rev,
},
];
test.run_scripts(scripts).await;
let (params, field_rev) = create_single_select_field(&test.grid_id);
let scripts = vec![
CreateField { params },
AssertFieldEqual {
field_index: test.field_count,
field_rev,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_create_duplicate_field() {
let mut test = GridEditorTest::new().await;
let (params, _) = create_text_field(&test.grid_id);
let field_count = test.field_count;
let expected_field_count = field_count + 1;
let scripts = vec![
CreateField { params: params.clone() },
CreateField { params },
AssertFieldCount(expected_field_count),
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_field_with_empty_change() {
let mut test = GridEditorTest::new().await;
let (params, field_rev) = create_single_select_field(&test.grid_id);
let changeset = FieldChangesetParams {
field_id: field_rev.id.clone(),
grid_id: test.grid_id.clone(),
..Default::default()
};
let scripts = vec![
CreateField { params },
UpdateField { changeset },
AssertFieldEqual {
field_index: test.field_count,
field_rev,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_update_field() {
let mut test = GridEditorTest::new().await;
let (params, single_select_field) = create_single_select_field(&test.grid_id);
let mut single_select_type_option = SingleSelectTypeOption::from(&single_select_field);
single_select_type_option.options.push(SelectOption::new("Unknown"));
let changeset = FieldChangesetParams {
field_id: single_select_field.id.clone(),
grid_id: test.grid_id.clone(),
frozen: Some(true),
width: Some(1000),
type_option_data: Some(single_select_type_option.protobuf_bytes().to_vec()),
..Default::default()
};
// The expected_field must be equal to the field that applied the changeset
let mut expected_field_rev = single_select_field.clone();
expected_field_rev.frozen = true;
expected_field_rev.width = 1000;
expected_field_rev.insert_type_option_entry(&single_select_type_option);
let scripts = vec![
CreateField { params },
UpdateField { changeset },
AssertFieldEqual {
field_index: test.field_count,
field_rev: expected_field_rev,
},
];
test.run_scripts(scripts).await;
}
#[tokio::test]
async fn grid_delete_field() {
let mut test = GridEditorTest::new().await;
let original_field_count = test.field_count;
let (params, text_field_rev) = create_text_field(&test.grid_id);
let scripts = vec![
CreateField { params },
DeleteField {
field_rev: text_field_rev,
},
AssertFieldCount(original_field_count),
];
test.run_scripts(scripts).await;
}