Browse Source

chore: add view bloc test (#1343)

Co-authored-by: nathan <[email protected]>
Nathan.fooo 2 years ago
parent
commit
0d8be87031

+ 0 - 2
frontend/app_flowy/lib/startup/deps_resolver.dart

@@ -89,8 +89,6 @@ void _resolveFolderDeps(GetIt getIt) {
   getIt.registerFactoryParam<ViewBloc, ViewPB, void>(
     (view, _) => ViewBloc(
       view: view,
-      service: ViewService(),
-      listener: getIt<ViewListener>(param1: view),
     ),
   );
 

+ 13 - 8
frontend/app_flowy/lib/workspace/application/view/view_bloc.dart

@@ -15,9 +15,9 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
 
   ViewBloc({
     required this.view,
-    required this.service,
-    required this.listener,
-  }) : super(ViewState.init(view)) {
+  })  : service = ViewService(),
+        listener = ViewListener(view: view),
+        super(ViewState.init(view)) {
     on<ViewEvent>((event, emit) async {
       await event.map(
         initial: (e) {
@@ -31,14 +31,19 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
         },
         viewDidUpdate: (e) {
           e.result.fold(
-            (view) =>
-                emit(state.copyWith(view: view, successOrFailure: left(unit))),
-            (error) => emit(state.copyWith(successOrFailure: right(error))),
+            (view) => emit(
+              state.copyWith(view: view, successOrFailure: left(unit)),
+            ),
+            (error) => emit(
+              state.copyWith(successOrFailure: right(error)),
+            ),
           );
         },
         rename: (e) async {
-          final result =
-              await service.updateView(viewId: view.id, name: e.newName);
+          final result = await service.updateView(
+            viewId: view.id,
+            name: e.newName,
+          );
           emit(
             result.fold(
               (l) => state.copyWith(successOrFailure: left(unit)),

+ 5 - 4
frontend/app_flowy/lib/workspace/presentation/home/menu/app/section/item.dart

@@ -38,10 +38,11 @@ class ViewSectionItem extends StatelessWidget {
     return MultiBlocProvider(
       providers: [
         BlocProvider(
-            create: (ctx) => getIt<ViewBloc>(param1: view)
-              ..add(
-                const ViewEvent.initial(),
-              )),
+          create: (ctx) => getIt<ViewBloc>(param1: view)
+            ..add(
+              const ViewEvent.initial(),
+            ),
+        ),
       ],
       child: BlocBuilder<ViewBloc, ViewState>(
         builder: (blocContext, state) {

+ 2 - 8
frontend/app_flowy/test/bloc_test/menu_test/app_bloc_test.dart

@@ -15,7 +15,7 @@ void main() {
   });
 
   group(
-    'AppBloc',
+    '$AppBloc',
     () {
       late AppPB app;
       setUp(() async {
@@ -67,7 +67,7 @@ void main() {
     },
   );
 
-  group('AppBloc', () {
+  group('$AppBloc', () {
     late ViewPB view;
     late AppPB app;
     setUpAll(() async {
@@ -90,12 +90,6 @@ void main() {
       "delete the document",
       build: () => AppBloc(app: app)..add(const AppEvent.initial()),
       act: (bloc) => bloc.add(AppEvent.deleteView(view.id)),
-    );
-    blocTest<AppBloc, AppState>(
-      "verify the document is exist",
-      build: () => AppBloc(app: app)..add(const AppEvent.initial()),
-      act: (bloc) => bloc.add(const AppEvent.loadViews()),
-      wait: blocResponseDuration(),
       verify: (bloc) {
         assert(bloc.state.views.isEmpty);
       },

+ 67 - 0
frontend/app_flowy/test/bloc_test/menu_test/view_bloc_test.dart

@@ -0,0 +1,67 @@
+import 'package:app_flowy/plugins/doc/document.dart';
+import 'package:app_flowy/workspace/application/app/app_bloc.dart';
+import 'package:app_flowy/workspace/application/view/view_bloc.dart';
+import 'package:bloc_test/bloc_test.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+import '../../util.dart';
+
+void main() {
+  late AppFlowyUnitTest test;
+  setUpAll(() async {
+    test = await AppFlowyUnitTest.ensureInitialized();
+  });
+
+  group('$ViewBloc', () {
+    late AppBloc appBloc;
+
+    setUpAll(() async {
+      final app = await test.createTestApp();
+      appBloc = AppBloc(app: app)..add(const AppEvent.initial());
+      appBloc.add(AppEvent.createView(
+        "Test document",
+        DocumentPluginBuilder(),
+      ));
+      await blocResponseFuture();
+    });
+
+    blocTest<ViewBloc, ViewState>(
+      "rename view",
+      build: () => ViewBloc(view: appBloc.state.views.first)
+        ..add(const ViewEvent.initial()),
+      act: (bloc) {
+        bloc.add(const ViewEvent.rename('Hello world'));
+      },
+      wait: blocResponseDuration(),
+      verify: (bloc) {
+        assert(bloc.state.view.name == "Hello world");
+      },
+    );
+
+    blocTest<ViewBloc, ViewState>(
+      "duplicate view",
+      build: () => ViewBloc(view: appBloc.state.views.first)
+        ..add(const ViewEvent.initial()),
+      act: (bloc) {
+        bloc.add(const ViewEvent.duplicate());
+      },
+      wait: blocResponseDuration(),
+      verify: (bloc) {
+        assert(appBloc.state.views.length == 2);
+      },
+    );
+
+    blocTest<ViewBloc, ViewState>(
+      "delete view",
+      build: () => ViewBloc(view: appBloc.state.views.first)
+        ..add(const ViewEvent.initial()),
+      act: (bloc) {
+        bloc.add(const ViewEvent.delete());
+      },
+      wait: blocResponseDuration(),
+      verify: (bloc) {
+        assert(appBloc.state.views.length == 1);
+      },
+    );
+  });
+}

+ 4 - 0
frontend/app_flowy/test/util.dart

@@ -104,6 +104,10 @@ class FlowyTestApp implements EntryPoint {
   }
 }
 
+Future<void> blocResponseFuture() {
+  return Future.delayed(const Duration(milliseconds: 100));
+}
+
 Duration blocResponseDuration({int millseconds = 100}) {
   return Duration(milliseconds: millseconds);
 }