Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

chore: add view bloc test (#1343)

Nathan.fooo0d8be870313b
diff --git a/frontend/app_flowy/lib/startup/deps_resolver.dart b/frontend/app_flowy/lib/startup/deps_resolver.dart
index e34ebc7205..864af3e0bc 100644
--- a/frontend/app_flowy/lib/startup/deps_resolver.dart
+++ b/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),
),
);
diff --git a/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart b/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart
index 86db402209..507c727aa8 100644
--- a/frontend/app_flowy/lib/workspace/application/view/view_bloc.dart
+++ b/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)),
diff --git a/frontend/app_flowy/lib/workspace/presentation/home/menu/app/section/item.dart b/frontend/app_flowy/lib/workspace/presentation/home/menu/app/section/item.dart
index 5b24fcd138..624f823eed 100644
--- a/frontend/app_flowy/lib/workspace/presentation/home/menu/app/section/item.dart
+++ b/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) {
diff --git a/frontend/app_flowy/test/bloc_test/menu_test/app_bloc_test.dart b/frontend/app_flowy/test/bloc_test/menu_test/app_bloc_test.dart
index e7c0477636..577e12b0e3 100644
--- a/frontend/app_flowy/test/bloc_test/menu_test/app_bloc_test.dart
+++ b/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);
},
diff --git a/frontend/app_flowy/test/bloc_test/menu_test/view_bloc_test.dart b/frontend/app_flowy/test/bloc_test/menu_test/view_bloc_test.dart
new file mode 100644
index 0000000000..b80e3e5282
--- /dev/null
+++ b/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);
+ },
+ );
+ });
+}
diff --git a/frontend/app_flowy/test/util.dart b/frontend/app_flowy/test/util.dart
index b8367f90fe..96d559b188 100644
--- a/frontend/app_flowy/test/util.dart
+++ b/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);
}