AppFlowy/frontend/rust-lib/flowy-grid/tests/grid/row_util.rs

69 lines
2.3 KiB
Rust
Raw Normal View History

2022-06-23 15:06:03 +00:00
use crate::grid::script::GridEditorTest;
2022-06-24 07:23:39 +00:00
use flowy_grid::services::field::DateCellContentChangeset;
2022-06-23 15:06:03 +00:00
use flowy_grid::services::row::{CreateRowRevisionBuilder, CreateRowRevisionPayload};
use flowy_grid_data_model::entities::FieldType;
use flowy_grid_data_model::revision::FieldRevision;
use strum::EnumCount;
pub struct GridRowTestBuilder<'a> {
test: &'a GridEditorTest,
inner_builder: CreateRowRevisionBuilder<'a>,
}
impl<'a> GridRowTestBuilder<'a> {
pub fn new(test: &'a GridEditorTest) -> Self {
assert_eq!(test.field_revs.len(), FieldType::COUNT);
let inner_builder = CreateRowRevisionBuilder::new(&test.field_revs);
Self { test, inner_builder }
}
2022-06-24 07:23:39 +00:00
pub fn update_text_cell(mut self, data: String) -> Self {
let text_field = self.field_rev_with_type(&FieldType::DateTime);
self.inner_builder.add_cell(&text_field.id, data).unwrap();
self
}
pub fn update_number_cell(mut self, data: String) -> Self {
let number_field = self.field_rev_with_type(&FieldType::DateTime);
self.inner_builder.add_cell(&number_field.id, data).unwrap();
self
}
pub fn update_date_cell(mut self, value: i64) -> Self {
let value = serde_json::to_string(&DateCellContentChangeset {
date: Some(value.to_string()),
time: None,
})
.unwrap();
let date_field = self.field_rev_with_type(&FieldType::DateTime);
self.inner_builder.add_cell(&date_field.id, value).unwrap();
self
}
pub fn update_checkbox_cell(mut self, data: bool) -> Self {
let number_field = self.field_rev_with_type(&FieldType::Checkbox);
self.inner_builder.add_cell(&number_field.id, data.to_string()).unwrap();
self
}
pub fn update_url_cell(mut self, data: String) -> Self {
let number_field = self.field_rev_with_type(&FieldType::Checkbox);
self.inner_builder.add_cell(&number_field.id, data).unwrap();
self
}
2022-06-24 10:13:40 +00:00
pub fn field_rev_with_type(&self, field_type: &FieldType) -> FieldRevision {
2022-06-24 07:23:39 +00:00
self.test
2022-06-23 15:06:03 +00:00
.field_revs
.iter()
2022-06-24 10:13:40 +00:00
.find(|field_rev| &field_rev.field_type == field_type)
2022-06-24 07:23:39 +00:00
.unwrap()
2022-06-24 10:13:40 +00:00
.clone()
2022-06-23 15:06:03 +00:00
}
pub fn build(self) -> CreateRowRevisionPayload {
self.inner_builder.build()
}
}