From 3b75b917f9e65069ef50ca78c61bb77209688b9d Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Fri, 24 Jun 2022 23:20:16 +0530 Subject: [PATCH] refactor:adding mock classes to test_utilities --- .../user/avatar_bloc_fixtures.dart | 12 +++-------- test/application/user/avatar_bloc_test.dart | 8 +++---- .../user/profile/profile_bloc_test.dart | 21 +++++++++++++++++++ .../user/profile/profile_fixtures.dart | 13 ++++++++++++ .../user_details/user_details_bloc_test.dart | 1 - test/test_utilities.dart | 14 +++++++++++++ 6 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 test/application/user/profile/profile_bloc_test.dart create mode 100644 test/application/user/profile/profile_fixtures.dart diff --git a/test/application/user/avatar_bloc_fixtures.dart b/test/application/user/avatar_bloc_fixtures.dart index 6d8f01f0..ec0ee26c 100644 --- a/test/application/user/avatar_bloc_fixtures.dart +++ b/test/application/user/avatar_bloc_fixtures.dart @@ -1,15 +1,9 @@ -import 'dart:io'; - import 'package:collaction_app/application/user/avatar/avatar_bloc.dart'; -import 'package:collaction_app/infrastructure/user/avatar_repository.dart'; -import 'package:mocktail/mocktail.dart'; - -class MockAvatarRepo extends Mock implements AvatarRepository {} -class MockAvatarFile extends Mock implements File {} +import '../../test_utilities.dart'; -final avatarRepo = MockAvatarRepo(); -final avatarRepo2 = MockAvatarRepo(); +final avatarRepo = MockAvatarRepository(); +final avatarRepo2 = MockAvatarRepository(); final tAvatarBloc = AvatarBloc(avatarRepo); final tAvatarFile = MockAvatarFile(); final tUri = Uri.parse('testAvatarUploadUri'); diff --git a/test/application/user/avatar_bloc_test.dart b/test/application/user/avatar_bloc_test.dart index c95de6ca..57a9bcea 100644 --- a/test/application/user/avatar_bloc_test.dart +++ b/test/application/user/avatar_bloc_test.dart @@ -4,7 +4,7 @@ import 'package:collaction_app/domain/user/upload_failures.dart'; import 'package:dartz/dartz.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; - +import '../../test_utilities.dart'; import 'avatar_bloc_fixtures.dart'; void main() { @@ -19,7 +19,7 @@ void main() { }); { - final avatarRepo1 = MockAvatarRepo(); + final avatarRepo1 = MockAvatarRepository(); when(() => avatarRepo1.getAvatarUploadPath()).thenAnswer( (_) => Future.value( left(const UploadPathFailure.unexpected()), @@ -39,7 +39,7 @@ void main() { } { - final avatarRepo2 = MockAvatarRepo(); + final avatarRepo2 = MockAvatarRepository(); when(() => avatarRepo2.getAvatarUploadPath()).thenAnswer( (_) => Future.value(right(tUri)), @@ -62,7 +62,7 @@ void main() { ); } { - final avatarRepo3 = MockAvatarRepo(); + final avatarRepo3 = MockAvatarRepository(); when(() => avatarRepo3.getAvatarUploadPath()).thenAnswer( (_) => Future.value(right(tUri)), ); diff --git a/test/application/user/profile/profile_bloc_test.dart b/test/application/user/profile/profile_bloc_test.dart new file mode 100644 index 00000000..cd92dfee --- /dev/null +++ b/test/application/user/profile/profile_bloc_test.dart @@ -0,0 +1,21 @@ +import 'package:collaction_app/application/user/profile/profile_bloc.dart'; +import 'package:dartz/dartz.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; + +import 'profile_fixtures.dart'; + +void main() { + group('Testing Profile BLoC initial state', () { + test('Testing initial state', () { + expect(tProfileBloc.state, ProfileState.initial()); + }); + + { + final tProfileRepository1 = tProfileRepository; + when(() => tProfileRepository1.getUserProfile()).thenAnswer( + (_) => Future.value(right(tUserProfile)), + ); + } + }); +} diff --git a/test/application/user/profile/profile_fixtures.dart b/test/application/user/profile/profile_fixtures.dart new file mode 100644 index 00000000..07449d69 --- /dev/null +++ b/test/application/user/profile/profile_fixtures.dart @@ -0,0 +1,13 @@ +import 'package:collaction_app/application/user/profile/profile_bloc.dart'; +import 'package:collaction_app/domain/profile/profile.dart'; +import 'package:collaction_app/domain/profile/user_profile.dart'; +import 'package:collaction_app/domain/user/user.dart'; + +import '../../../test_utilities.dart'; + +final tProfileRepository = MockProfileRepository(); +final tAvatarRepository = MockAvatarRepository(); +const tUser = User(id: 'tId', getIdToken: getAnonymousIdToken); +const tProfile = Profile(bio: 'tBio'); +const tUserProfile = UserProfile(user: tUser, profile: tProfile); +final tProfileBloc = ProfileBloc(tProfileRepository, tAvatarRepository); diff --git a/test/application/user_details/user_details_bloc_test.dart b/test/application/user_details/user_details_bloc_test.dart index 00fa17db..ee58ad00 100644 --- a/test/application/user_details/user_details_bloc_test.dart +++ b/test/application/user_details/user_details_bloc_test.dart @@ -19,7 +19,6 @@ void main() { when(() => tUserRepo.observeUser()).thenAnswer((_) { final x = StreamController(); x.add(tUser); - print('x.stream is User? ${x.stream.distinct() is Stream}'); return x.stream.distinct(); }); diff --git a/test/test_utilities.dart b/test/test_utilities.dart index 5acbad44..85c08016 100644 --- a/test/test_utilities.dart +++ b/test/test_utilities.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:collaction_app/domain/auth/i_auth_repository.dart'; import 'package:collaction_app/domain/contact_form/contact_form_dto.dart'; import 'package:collaction_app/domain/contact_form/i_contact_form_api.dart'; @@ -5,6 +7,8 @@ import 'package:collaction_app/domain/core/i_settings_repository.dart'; import 'package:collaction_app/domain/crowdaction/crowdaction.dart'; import 'package:collaction_app/domain/crowdaction/crowdaction_status.dart'; import 'package:collaction_app/domain/crowdaction/i_crowdaction_repository.dart'; +import 'package:collaction_app/domain/user/i_avatar_repository.dart'; +import 'package:collaction_app/domain/user/i_profile_repository.dart'; import 'package:collaction_app/domain/user/i_user_repository.dart'; import 'package:collaction_app/domain/user/user.dart'; import 'package:dartz/dartz.dart'; @@ -24,6 +28,16 @@ class MockSettingsRepository extends Mock implements ISettingsRepository {} class MockContactFormApi extends Mock implements IContactFormApi {} +class MockAvatarRepository extends Mock implements IAvatarRepository {} + +class MockAvatarFile extends Mock implements File {} + +class MockProfileRepository extends Mock implements IProfileRepository {} + +// ignore: avoid_positional_boolean_parameters +Future getAnonymousIdToken([bool forceRefresh = false]) => + Future.value(null); + // ignore: avoid_classes_with_only_static_members class TestUtilities { static void mockUser(Stream userStream) {