Browse Source

test: edit a field in kanban board (#1428)

Nathan.fooo 2 years ago
parent
commit
e482ac75f9

+ 125 - 0
frontend/app_flowy/test/bloc_test/board_test/create_or_edit_field_test.dart

@@ -0,0 +1,125 @@
+import 'package:app_flowy/plugins/board/application/board_bloc.dart';
+import 'package:app_flowy/plugins/grid/application/field/field_editor_bloc.dart';
+import 'package:app_flowy/plugins/grid/application/field/type_option/type_option_context.dart';
+import 'package:bloc_test/bloc_test.dart';
+import 'package:flowy_sdk/protobuf/flowy-grid/field_entities.pb.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'util.dart';
+
+void main() {
+  late AppFlowyBoardTest boardTest;
+
+  setUpAll(() async {
+    boardTest = await AppFlowyBoardTest.ensureInitialized();
+  });
+
+  group('The grouped field is not changed after editing a field:', () {
+    late BoardBloc boardBloc;
+    late FieldEditorBloc editorBloc;
+    setUpAll(() async {
+      await boardTest.context.createTestBoard();
+    });
+
+    setUp(() async {
+      boardBloc = BoardBloc(view: boardTest.context.gridView)
+        ..add(const BoardEvent.initial());
+
+      final fieldContext = boardTest.context.singleSelectFieldContext();
+      final loader = FieldTypeOptionLoader(
+        gridId: boardTest.context.gridView.id,
+        field: fieldContext.field,
+      );
+
+      editorBloc = FieldEditorBloc(
+        gridId: boardTest.context.gridView.id,
+        fieldName: fieldContext.name,
+        isGroupField: fieldContext.isGroupField,
+        loader: loader,
+      )..add(const FieldEditorEvent.initial());
+
+      await boardResponseFuture();
+    });
+
+    blocTest<BoardBloc, BoardState>(
+      "initial",
+      build: () => boardBloc,
+      wait: boardResponseDuration(),
+      verify: (bloc) {
+        assert(bloc.groupControllers.values.length == 4);
+        assert(boardTest.context.fieldContexts.length == 2);
+      },
+    );
+
+    blocTest<FieldEditorBloc, FieldEditorState>(
+      "edit a field",
+      build: () => editorBloc,
+      act: (bloc) async {
+        editorBloc.add(const FieldEditorEvent.updateName('Hello world'));
+      },
+      wait: boardResponseDuration(),
+      verify: (bloc) {
+        bloc.state.field.fold(
+          () => throw Exception("The field should not be none"),
+          (field) {
+            assert(field.name == 'Hello world');
+          },
+        );
+      },
+    );
+
+    blocTest<BoardBloc, BoardState>(
+      "assert the groups were not changed",
+      build: () => boardBloc,
+      wait: boardResponseDuration(),
+      verify: (bloc) {
+        assert(bloc.groupControllers.values.length == 4,
+            "Expected 4, but receive ${bloc.groupControllers.values.length}");
+
+        assert(boardTest.context.fieldContexts.length == 2,
+            "Expected 2, but receive ${boardTest.context.fieldContexts.length}");
+      },
+    );
+  });
+  group('The grouped field is not changed after creating a new field:', () {
+    late BoardBloc boardBloc;
+    setUpAll(() async {
+      await boardTest.context.createTestBoard();
+    });
+
+    setUp(() async {
+      boardBloc = BoardBloc(view: boardTest.context.gridView)
+        ..add(const BoardEvent.initial());
+      await boardResponseFuture();
+    });
+
+    blocTest<BoardBloc, BoardState>(
+      "initial",
+      build: () => boardBloc,
+      wait: boardResponseDuration(),
+      verify: (bloc) {
+        assert(bloc.groupControllers.values.length == 4);
+        assert(boardTest.context.fieldContexts.length == 2);
+      },
+    );
+
+    test('create a field', () async {
+      await boardTest.context.createField(FieldType.Checkbox);
+      await boardResponseFuture();
+      final checkboxField = boardTest.context.fieldContexts.last.field;
+      assert(checkboxField.fieldType == FieldType.Checkbox);
+    });
+
+    blocTest<BoardBloc, BoardState>(
+      "assert the groups were not changed",
+      build: () => boardBloc,
+      wait: boardResponseDuration(),
+      verify: (bloc) {
+        assert(bloc.groupControllers.values.length == 4,
+            "Expected 4, but receive ${bloc.groupControllers.values.length}");
+
+        assert(boardTest.context.fieldContexts.length == 3,
+            "Expected 3, but receive ${boardTest.context.fieldContexts.length}");
+      },
+    );
+  });
+}

+ 1 - 1
frontend/app_flowy/test/bloc_test/board_test/group_by_field_test.dart

@@ -37,7 +37,7 @@ void main() {
     });
 
     blocTest<GridGroupBloc, GridGroupState>(
-      "set grouped by multi-select field",
+      "set grouped by the new multi-select field",
       build: () => GridGroupBloc(
         viewId: boardTest.context.gridView.id,
         fieldController: boardTest.context.fieldController,

+ 5 - 10
frontend/app_flowy/test/bloc_test/grid_test/grid_bloc_test.dart

@@ -10,9 +10,9 @@ void main() {
     await gridTest.createTestGrid();
   });
 
-  group('GridBloc', () {
+  group('Create a new row in Grid', () {
     blocTest<GridBloc, GridState>(
-      "Create row",
+      "Create a row",
       build: () =>
           GridBloc(view: gridTest.gridView)..add(const GridEvent.initial()),
       act: (bloc) => bloc.add(const GridEvent.createRow()),
@@ -23,7 +23,7 @@ void main() {
     );
   });
 
-  group('GridBloc', () {
+  group('Delete a row in the grid', () {
     late GridBloc gridBloc;
     setUpAll(() async {
       gridBloc = GridBloc(view: gridTest.gridView)
@@ -31,15 +31,10 @@ void main() {
       await gridResponseFuture();
     });
 
-    // The initial number of rows is three
-    test('', () async {
-      assert(gridBloc.state.rowInfos.length == 3);
-    });
-
-    test('delete row', () async {
+    test('delete the last row', () async {
       gridBloc.add(GridEvent.deleteRow(gridBloc.state.rowInfos.last));
       await gridResponseFuture();
-      assert(gridBloc.state.rowInfos.length == 2);
+      assert(gridBloc.state.rowInfos.length == 3);
     });
   });
 }

+ 7 - 2
frontend/rust-lib/flowy-grid/src/services/grid_view_editor.rs

@@ -291,8 +291,13 @@ impl GridViewRevisionEditor {
         .await
     }
     #[tracing::instrument(level = "trace", skip_all, err)]
-    pub(crate) async fn did_update_view_field(&self, _field_id: &str) -> FlowyResult<()> {
-        // Do nothing
+    pub(crate) async fn did_update_view_field(&self, field_id: &str) -> FlowyResult<()> {
+        let grouped_field_id = self.group_controller.read().await.field_id().to_owned();
+        if grouped_field_id == field_id {
+            let _ = self.group_by_view_field(field_id).await?;
+        } else {
+            // Do nothing
+        }
         Ok(())
     }
 

+ 1 - 1
frontend/rust-lib/flowy-grid/src/services/grid_view_manager.rs

@@ -193,7 +193,7 @@ impl GridViewManager {
     #[tracing::instrument(level = "trace", skip(self), err)]
     pub(crate) async fn did_update_view_field_type_option(&self, field_id: &str) -> FlowyResult<()> {
         let view_editor = self.get_default_view_editor().await?;
-        let _ = view_editor.group_by_view_field(field_id).await?;
+        let _ = view_editor.did_update_view_field(field_id).await?;
         Ok(())
     }