From eeff1ae96d0f7960676050fe5f27ff8aab412438 Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Thu, 21 Mar 2024 12:13:18 +0530 Subject: [PATCH] Fix Upcoming Events --- lib/data/di/service_locator.config.dart | 72 ++++----- lib/data/repo/employee_repo.dart | 2 +- .../shared/events/bloc/celebrations_bloc.dart | 12 +- .../events/celebration_event_bloc_test.dart | 22 +-- .../celebration_event_bloc_test.mocks.dart | 142 ++++++++++++++---- 5 files changed, 163 insertions(+), 87 deletions(-) diff --git a/lib/data/di/service_locator.config.dart b/lib/data/di/service_locator.config.dart index e986e8fc..bf31112e 100644 --- a/lib/data/di/service_locator.config.dart +++ b/lib/data/di/service_locator.config.dart @@ -24,13 +24,13 @@ import 'package:projectunity/data/bloc/network/network_connection_bloc.dart' import 'package:projectunity/data/bloc/user_state/space_change_notifier.dart' as _i21; import 'package:projectunity/data/bloc/user_state/user_state_controller_bloc.dart' - as _i53; + as _i54; import 'package:projectunity/data/core/functions/shared_function.dart' as _i3; import 'package:projectunity/data/di/app_module.dart' as _i61; import 'package:projectunity/data/pref/user_preference.dart' as _i24; import 'package:projectunity/data/provider/device_info.dart' as _i8; import 'package:projectunity/data/provider/user_state.dart' as _i25; -import 'package:projectunity/data/repo/employee_repo.dart' as _i51; +import 'package:projectunity/data/repo/employee_repo.dart' as _i52; import 'package:projectunity/data/repo/form_repo.dart' as _i31; import 'package:projectunity/data/repo/leave_repo.dart' as _i34; import 'package:projectunity/data/services/account_service.dart' as _i26; @@ -46,17 +46,17 @@ import 'package:projectunity/data/services/storage_service.dart' as _i23; import 'package:projectunity/data/state_manager/auth/desktop/desktop_auth_manager.dart' as _i6; import 'package:projectunity/ui/admin/forms/create_form/bloc/create_form_bloc.dart' - as _i47; + as _i48; import 'package:projectunity/ui/admin/forms/form_list/bloc/admin_form_list_bloc.dart' as _i44; import 'package:projectunity/ui/admin/home/home_screen/bloc/admin_home_bloc.dart' - as _i56; + as _i57; import 'package:projectunity/ui/admin/home/invite_member/bloc/invite_member_bloc.dart' as _i32; import 'package:projectunity/ui/admin/leaves/details/bloc/admin_leave_details_bloc.dart' as _i45; import 'package:projectunity/ui/admin/leaves/leave_screen/bloc%20/admin_leaves_bloc.dart' - as _i57; + as _i58; import 'package:projectunity/ui/admin/members/detail/bloc/employee_detail_bloc.dart' as _i60; import 'package:projectunity/ui/admin/members/details_leaves/bloc/admin_employee_details_leave_bloc.dart' @@ -64,20 +64,20 @@ import 'package:projectunity/ui/admin/members/details_leaves/bloc/admin_employee import 'package:projectunity/ui/admin/members/edit_employee/bloc/admin_edit_employee_bloc.dart' as _i42; import 'package:projectunity/ui/admin/members/list/bloc/member_list_bloc.dart' - as _i58; + as _i59; import 'package:projectunity/ui/shared/appbar_drawer/drawer/bloc/app_drawer_bloc.dart' - as _i49; + as _i50; import 'package:projectunity/ui/shared/events/bloc/celebrations_bloc.dart' - as _i59; + as _i47; import 'package:projectunity/ui/shared/profile/edit_profile/bloc/employee_edit_profile_bloc.dart' - as _i50; + as _i51; import 'package:projectunity/ui/shared/profile/view_profile/bloc/view_profile_bloc.dart' - as _i54; -import 'package:projectunity/ui/shared/who_is_out_card/bloc/who_is_out_card_bloc.dart' as _i55; +import 'package:projectunity/ui/shared/who_is_out_card/bloc/who_is_out_card_bloc.dart' + as _i56; import 'package:projectunity/ui/sign_in/bloc/sign_in_view_bloc.dart' as _i35; import 'package:projectunity/ui/space/create_space/bloc/create_workspace_bloc.dart' - as _i48; + as _i49; import 'package:projectunity/ui/space/edit_space/bloc/edit_space_bloc.dart' as _i29; import 'package:projectunity/ui/space/join_space/bloc/join_space_bloc.dart' @@ -97,7 +97,7 @@ import 'package:projectunity/ui/user/leaves/leaves_screen/bloc/leaves/user_leave import 'package:projectunity/ui/user/members/detail/bloc/user_employee_detail_bloc.dart' as _i36; import 'package:projectunity/ui/user/members/members_screen/bloc/user_members_bloc.dart' - as _i52; + as _i53; import 'package:projectunity/ui/widget/pick_profile_image/bloc/pick_image_bloc.dart' as _i19; import 'package:shared_preferences/shared_preferences.dart' as _i20; @@ -247,77 +247,77 @@ extension GetItInjectableX on _i1.GetIt { gh<_i18.NotificationService>(), gh<_i3.AppFunctions>(), )); - gh.factory<_i47.CreateFormBloc>(() => _i47.CreateFormBloc( + gh.factory<_i47.CelebrationsBloc>( + () => _i47.CelebrationsBloc(gh<_i30.EmployeeService>())); + gh.factory<_i48.CreateFormBloc>(() => _i48.CreateFormBloc( gh<_i31.FormRepo>(), gh<_i14.ImagePicker>(), gh<_i23.StorageService>(), gh<_i25.UserStateNotifier>(), )); - gh.factory<_i48.CreateSpaceBLoc>(() => _i48.CreateSpaceBLoc( + gh.factory<_i49.CreateSpaceBLoc>(() => _i49.CreateSpaceBLoc( gh<_i22.SpaceService>(), gh<_i25.UserStateNotifier>(), gh<_i30.EmployeeService>(), gh<_i14.ImagePicker>(), gh<_i23.StorageService>(), )); - gh.factory<_i49.DrawerBloc>(() => _i49.DrawerBloc( + gh.factory<_i50.DrawerBloc>(() => _i50.DrawerBloc( gh<_i22.SpaceService>(), gh<_i25.UserStateNotifier>(), gh<_i26.AccountService>(), gh<_i30.EmployeeService>(), )); - gh.factory<_i50.EmployeeEditProfileBloc>(() => _i50.EmployeeEditProfileBloc( + gh.factory<_i51.EmployeeEditProfileBloc>(() => _i51.EmployeeEditProfileBloc( gh<_i30.EmployeeService>(), gh<_i24.UserPreference>(), gh<_i25.UserStateNotifier>(), gh<_i23.StorageService>(), )); - gh.singleton<_i51.EmployeeRepo>( - () => _i51.EmployeeRepo( + gh.singleton<_i52.EmployeeRepo>( + () => _i52.EmployeeRepo( gh<_i30.EmployeeService>(), gh<_i25.UserStateNotifier>(), gh<_i10.FirebaseCrashlytics>(), ), dispose: (i) => i.dispose(), ); - gh.factory<_i52.UserEmployeesBloc>( - () => _i52.UserEmployeesBloc(gh<_i51.EmployeeRepo>())); - gh.factory<_i53.UserStateControllerBloc>(() => _i53.UserStateControllerBloc( - gh<_i51.EmployeeRepo>(), + gh.factory<_i53.UserEmployeesBloc>( + () => _i53.UserEmployeesBloc(gh<_i52.EmployeeRepo>())); + gh.factory<_i54.UserStateControllerBloc>(() => _i54.UserStateControllerBloc( + gh<_i52.EmployeeRepo>(), gh<_i25.UserStateNotifier>(), gh<_i22.SpaceService>(), gh<_i21.SpaceChangeNotifier>(), )); - gh.factory<_i54.ViewProfileBloc>(() => _i54.ViewProfileBloc( + gh.factory<_i55.ViewProfileBloc>(() => _i55.ViewProfileBloc( gh<_i25.UserStateNotifier>(), - gh<_i51.EmployeeRepo>(), + gh<_i52.EmployeeRepo>(), )); - gh.factory<_i55.WhoIsOutCardBloc>(() => _i55.WhoIsOutCardBloc( - gh<_i51.EmployeeRepo>(), + gh.factory<_i56.WhoIsOutCardBloc>(() => _i56.WhoIsOutCardBloc( + gh<_i52.EmployeeRepo>(), gh<_i34.LeaveRepo>(), )); - gh.factory<_i56.AdminHomeBloc>(() => _i56.AdminHomeBloc( + gh.factory<_i57.AdminHomeBloc>(() => _i57.AdminHomeBloc( gh<_i34.LeaveRepo>(), - gh<_i51.EmployeeRepo>(), + gh<_i52.EmployeeRepo>(), )); - gh.factory<_i57.AdminLeavesBloc>(() => _i57.AdminLeavesBloc( + gh.factory<_i58.AdminLeavesBloc>(() => _i58.AdminLeavesBloc( gh<_i34.LeaveRepo>(), - gh<_i51.EmployeeRepo>(), + gh<_i52.EmployeeRepo>(), )); - gh.factory<_i58.AdminMembersBloc>(() => _i58.AdminMembersBloc( - gh<_i51.EmployeeRepo>(), + gh.factory<_i59.AdminMembersBloc>(() => _i59.AdminMembersBloc( + gh<_i52.EmployeeRepo>(), gh<_i15.InvitationService>(), gh<_i25.UserStateNotifier>(), )); - gh.factory<_i59.CelebrationsBloc>( - () => _i59.CelebrationsBloc(gh<_i51.EmployeeRepo>())); gh.factory<_i60.EmployeeDetailBloc>(() => _i60.EmployeeDetailBloc( gh<_i26.AccountService>(), gh<_i22.SpaceService>(), gh<_i25.UserStateNotifier>(), gh<_i30.EmployeeService>(), gh<_i34.LeaveRepo>(), - gh<_i51.EmployeeRepo>(), + gh<_i52.EmployeeRepo>(), )); return this; } diff --git a/lib/data/repo/employee_repo.dart b/lib/data/repo/employee_repo.dart index 42c6f1c2..51252a3a 100644 --- a/lib/data/repo/employee_repo.dart +++ b/lib/data/repo/employee_repo.dart @@ -36,7 +36,7 @@ class EmployeeRepo { List get allEmployees { if (_employeeController.hasValue) { - return _employeeController.stream.value; + return _employeeController.value; } return []; } diff --git a/lib/ui/shared/events/bloc/celebrations_bloc.dart b/lib/ui/shared/events/bloc/celebrations_bloc.dart index 285d1470..c168296c 100644 --- a/lib/ui/shared/events/bloc/celebrations_bloc.dart +++ b/lib/ui/shared/events/bloc/celebrations_bloc.dart @@ -4,23 +4,23 @@ import 'package:injectable/injectable.dart'; import 'package:projectunity/data/core/exception/error_const.dart'; import 'package:projectunity/data/core/extensions/date_time.dart'; import 'package:projectunity/data/model/employee/employee.dart'; +import 'package:projectunity/data/services/employee_service.dart'; import 'package:projectunity/ui/shared/events/bloc/celebrations_state.dart'; import '../../../../data/core/utils/bloc_status.dart'; -import '../../../../data/repo/employee_repo.dart'; import '../model/event.dart'; import 'celebrations_event.dart'; @Injectable() class CelebrationsBloc extends Bloc { - final EmployeeRepo _employeeRepo; + final EmployeeService _employeeService; List employees = []; List allBirthdayEvents = []; List allAnniversaryEvents = []; List currentWeekBday = []; List currentWeekAnniversaries = []; - CelebrationsBloc(this._employeeRepo) : super(const CelebrationsState()) { + CelebrationsBloc(this._employeeService) : super(const CelebrationsState()) { on(_fetchEvent); on(_showAllBirthdays); on(_showAllAnniversaries); @@ -30,10 +30,8 @@ class CelebrationsBloc extends Bloc { FetchCelebrations event, Emitter emit) async { try { emit(state.copyWith(status: Status.loading)); - employees = _employeeRepo.allEmployees - .where((employee) => employee.status == EmployeeStatus.active) - .toList(); - employees = employees.map((e) { + final List allEmployees = await _employeeService.getEmployees(); + employees = allEmployees.map((e) { if (e.dateOfBirth != null) { final birthdate = e.dateOfBirth!.convertToUpcomingDay(); final Event event = Event( diff --git a/test/unit_test/shared/events/celebration_event_bloc_test.dart b/test/unit_test/shared/events/celebration_event_bloc_test.dart index 8613ebf4..1815c313 100644 --- a/test/unit_test/shared/events/celebration_event_bloc_test.dart +++ b/test/unit_test/shared/events/celebration_event_bloc_test.dart @@ -4,17 +4,16 @@ import 'package:mockito/mockito.dart'; import 'package:projectunity/data/core/extensions/date_time.dart'; import 'package:projectunity/data/core/utils/bloc_status.dart'; import 'package:projectunity/data/model/employee/employee.dart'; -import 'package:projectunity/data/repo/employee_repo.dart'; +import 'package:projectunity/data/services/employee_service.dart'; import 'package:projectunity/ui/shared/events/bloc/celebrations_bloc.dart'; import 'package:projectunity/ui/shared/events/bloc/celebrations_event.dart'; import 'package:projectunity/ui/shared/events/bloc/celebrations_state.dart'; import 'package:projectunity/ui/shared/events/model/event.dart'; +import 'celebration_event_bloc_test.mocks.dart'; -import '../../admin/home/home_screen/admin_home_bloc_test.mocks.dart'; - -@GenerateMocks([EmployeeRepo]) +@GenerateMocks([EmployeeService]) void main() { - late EmployeeRepo employeeRepo; + late EmployeeService employeeService; late CelebrationsBloc celebrationsBloc; const CelebrationsState celebrationsState = CelebrationsState(); Employee employee1 = Employee( @@ -55,8 +54,8 @@ void main() { imageUrl: employee2.imageUrl); setUp(() { - employeeRepo = MockEmployeeRepo(); - celebrationsBloc = CelebrationsBloc(employeeRepo); + employeeService = MockEmployeeService(); + celebrationsBloc = CelebrationsBloc(employeeService); }); group("All tests of events", () { @@ -66,7 +65,8 @@ void main() { test("Test FetchCelebrations- emit loading state and then success state", () { - when(employeeRepo.allEmployees).thenReturn([employee1, employee2]); + when(employeeService.getEmployees()) + .thenAnswer((_) async => [employee1, employee2]); celebrationsBloc.add(FetchCelebrations()); expectLater( celebrationsBloc.stream, @@ -82,7 +82,8 @@ void main() { test("Test ShowBirthdaysEvent- emit success state with all the birthdays", () { - when(employeeRepo.allEmployees).thenReturn([employee1, employee2]); + when(employeeService.getEmployees()) + .thenAnswer((_) async => [employee1, employee2]); celebrationsBloc.add(FetchCelebrations()); celebrationsBloc.add(ShowBirthdaysEvent()); @@ -105,7 +106,8 @@ void main() { test( "Test ShowAnniversariesEvent- emit success state with all the anniversary", () { - when(employeeRepo.allEmployees).thenReturn([employee1, employee2]); + when(employeeService.getEmployees()) + .thenAnswer((_) async => [employee1, employee2]); celebrationsBloc.add(FetchCelebrations()); celebrationsBloc.add(ShowAnniversariesEvent()); diff --git a/test/unit_test/shared/events/celebration_event_bloc_test.mocks.dart b/test/unit_test/shared/events/celebration_event_bloc_test.mocks.dart index efeb9aab..b16c0dc8 100644 --- a/test/unit_test/shared/events/celebration_event_bloc_test.mocks.dart +++ b/test/unit_test/shared/events/celebration_event_bloc_test.mocks.dart @@ -3,11 +3,12 @@ // Do not manually edit this file. // ignore_for_file: no_leading_underscores_for_library_prefixes -import 'dart:async' as _i3; +import 'dart:async' as _i4; +import 'package:cloud_firestore/cloud_firestore.dart' as _i2; import 'package:mockito/mockito.dart' as _i1; -import 'package:projectunity/data/model/employee/employee.dart' as _i4; -import 'package:projectunity/data/repo/employee_repo.dart' as _i2; +import 'package:projectunity/data/model/employee/employee.dart' as _i5; +import 'package:projectunity/data/services/employee_service.dart' as _i3; // ignore_for_file: type=lint // ignore_for_file: avoid_redundant_argument_values @@ -22,58 +23,133 @@ import 'package:projectunity/data/repo/employee_repo.dart' as _i2; // ignore_for_file: camel_case_types // ignore_for_file: subtype_of_sealed_class -/// A class which mocks [EmployeeRepo]. +class _FakeFirebaseFirestore_0 extends _i1.SmartFake + implements _i2.FirebaseFirestore { + _FakeFirebaseFirestore_0( + Object parent, + Invocation parentInvocation, + ) : super( + parent, + parentInvocation, + ); +} + +/// A class which mocks [EmployeeService]. /// /// See the documentation for Mockito's code generation for more information. -class MockEmployeeRepo extends _i1.Mock implements _i2.EmployeeRepo { - MockEmployeeRepo() { +class MockEmployeeService extends _i1.Mock implements _i3.EmployeeService { + MockEmployeeService() { _i1.throwOnMissingStub(this); } @override - _i3.Stream> get employees => (super.noSuchMethod( - Invocation.getter(#employees), - returnValue: _i3.Stream>.empty(), - ) as _i3.Stream>); + _i2.FirebaseFirestore get fireStore => (super.noSuchMethod( + Invocation.getter(#fireStore), + returnValue: _FakeFirebaseFirestore_0( + this, + Invocation.getter(#fireStore), + ), + ) as _i2.FirebaseFirestore); + + @override + _i4.Stream> employees(String? spaceId) => + (super.noSuchMethod( + Invocation.method( + #employees, + [spaceId], + ), + returnValue: _i4.Stream>.empty(), + ) as _i4.Stream>); + + @override + _i4.Future addEmployeeBySpaceId({ + required _i5.Employee? employee, + required String? spaceId, + }) => + (super.noSuchMethod( + Invocation.method( + #addEmployeeBySpaceId, + [], + { + #employee: employee, + #spaceId: spaceId, + }, + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future<_i5.Employee?> getEmployeeBySpaceId({ + required String? userId, + required String? spaceId, + }) => + (super.noSuchMethod( + Invocation.method( + #getEmployeeBySpaceId, + [], + { + #userId: userId, + #spaceId: spaceId, + }, + ), + returnValue: _i4.Future<_i5.Employee?>.value(), + ) as _i4.Future<_i5.Employee?>); @override - List<_i4.Employee> get allEmployees => (super.noSuchMethod( - Invocation.getter(#allEmployees), - returnValue: <_i4.Employee>[], - ) as List<_i4.Employee>); + _i4.Future> getEmployees() => (super.noSuchMethod( + Invocation.method( + #getEmployees, + [], + ), + returnValue: _i4.Future>.value(<_i5.Employee>[]), + ) as _i4.Future>); @override - _i3.Stream> get activeEmployees => (super.noSuchMethod( - Invocation.getter(#activeEmployees), - returnValue: _i3.Stream>.empty(), - ) as _i3.Stream>); + _i4.Future<_i5.Employee?> getEmployee(String? id) => (super.noSuchMethod( + Invocation.method( + #getEmployee, + [id], + ), + returnValue: _i4.Future<_i5.Employee?>.value(), + ) as _i4.Future<_i5.Employee?>); @override - _i3.Stream<_i4.Employee?> memberDetails(String? uid) => (super.noSuchMethod( + _i4.Future hasUser(String? email) => (super.noSuchMethod( Invocation.method( - #memberDetails, - [uid], + #hasUser, + [email], ), - returnValue: _i3.Stream<_i4.Employee?>.empty(), - ) as _i3.Stream<_i4.Employee?>); + returnValue: _i4.Future.value(false), + ) as _i4.Future); @override - _i3.Future reset() => (super.noSuchMethod( + _i4.Future updateEmployeeDetails({required _i5.Employee? employee}) => + (super.noSuchMethod( Invocation.method( - #reset, + #updateEmployeeDetails, [], + {#employee: employee}, ), - returnValue: _i3.Future.value(), - returnValueForMissingStub: _i3.Future.value(), - ) as _i3.Future); + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); @override - _i3.Future dispose() => (super.noSuchMethod( + _i4.Future changeAccountStatus({ + required String? id, + required _i5.EmployeeStatus? status, + }) => + (super.noSuchMethod( Invocation.method( - #dispose, + #changeAccountStatus, [], + { + #id: id, + #status: status, + }, ), - returnValue: _i3.Future.value(), - returnValueForMissingStub: _i3.Future.value(), - ) as _i3.Future); + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); }