chore: add view bloc test (#1343)

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Nathan.fooo 2022-10-23 17:52:17 +08:00 committed by GitHub
parent c65d00e95c
commit 0d8be87031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 22 deletions

View File

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

View File

@ -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)),

View File

@ -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) {

View File

@ -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);
},

View File

@ -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);
},
);
});
}

View File

@ -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);
}