From d08445c3ab254d058d0ac5a69e55cca4e94aa36d Mon Sep 17 00:00:00 2001 From: Pratik-canopas Date: Mon, 7 Aug 2023 15:01:59 +0530 Subject: [PATCH] Refactor unit test --- ...admin_leave_request_details_bloc_test.dart | 13 +++++---- .../leave_screen/admin_leaves_test.dart | 4 +-- .../detail/employee_detail_bloc_test.dart | 5 ++-- .../apply_leave/apply_leave_bloc_test.dart | 12 +++++--- .../apply_leave_bloc_test.mocks.dart | 29 +++++++++++++++++++ .../detail/user_leave_detail_bloc_test.dart | 29 ++++++++++--------- .../user_leave_count_bloc_test.dart | 19 ++++++------ .../user_employee_detail_bloc_test.dart | 2 +- 8 files changed, 76 insertions(+), 37 deletions(-) diff --git a/test/unit_test/admin/leaves/application_detail/admin_leave_request_details_bloc_test.dart b/test/unit_test/admin/leaves/application_detail/admin_leave_request_details_bloc_test.dart index 25ec998b..06cab953 100644 --- a/test/unit_test/admin/leaves/application_detail/admin_leave_request_details_bloc_test.dart +++ b/test/unit_test/admin/leaves/application_detail/admin_leave_request_details_bloc_test.dart @@ -5,6 +5,7 @@ import 'package:projectunity/data/core/exception/error_const.dart'; import 'package:projectunity/data/core/extensions/date_time.dart'; import 'package:projectunity/data/core/utils/bloc_status.dart'; import 'package:projectunity/data/model/leave/leave.dart'; +import 'package:projectunity/data/model/leave_count.dart'; import 'package:projectunity/data/provider/user_state.dart'; import 'package:projectunity/data/repo/leave_repo.dart'; import 'package:projectunity/data/services/mail_notification_service.dart'; @@ -34,7 +35,7 @@ void main() { AdminLeaveDetailsState leaveCountLoadingState = const AdminLeaveDetailsState( adminReply: '', - usedLeavesCount: 0.0, + usedLeavesCount: LeaveCounts(), error: null, leaveCountStatus: Status.loading, actionStatus: Status.initial); @@ -42,10 +43,10 @@ void main() { test( 'Emits loading state and success state respectively if leave counts are fetched successfully from firestore', () { - when(leaveRepo.getUserUsedLeaves(uid: 'id')).thenAnswer((_) async => 10); + when(leaveRepo.getUserUsedLeaves(uid: 'id')).thenAnswer((_) async => const LeaveCounts(casualLeaves: 5,urgentLeaves: 5)); AdminLeaveDetailsState successState = const AdminLeaveDetailsState( adminReply: '', - usedLeavesCount: 10, + usedLeavesCount: LeaveCounts(casualLeaves: 5,urgentLeaves: 5), error: null, actionStatus: Status.initial, leaveCountStatus: Status.success); @@ -60,7 +61,7 @@ void main() { when(leaveRepo.getUserUsedLeaves(uid: 'id')).thenThrow(Exception('error')); AdminLeaveDetailsState errorState = const AdminLeaveDetailsState( adminReply: '', - usedLeavesCount: 0, + usedLeavesCount: LeaveCounts(), error: firestoreFetchDataError, actionStatus: Status.initial, leaveCountStatus: Status.error); @@ -74,7 +75,7 @@ void main() { AdminLeaveDetailsState responseLoadingState = const AdminLeaveDetailsState( adminReply: '', - usedLeavesCount: 0, + usedLeavesCount: LeaveCounts(), actionStatus: Status.loading, leaveCountStatus: Status.initial, error: null); @@ -177,7 +178,7 @@ void main() { leaveId: 'leave-id')); AdminLeaveDetailsState errorState = const AdminLeaveDetailsState( adminReply: '', - usedLeavesCount: 0, + usedLeavesCount: LeaveCounts(), error: firestoreFetchDataError, actionStatus: Status.error); expectLater( diff --git a/test/unit_test/admin/leaves/leave_screen/admin_leaves_test.dart b/test/unit_test/admin/leaves/leave_screen/admin_leaves_test.dart index 2af93e5d..a9e1acaa 100644 --- a/test/unit_test/admin/leaves/leave_screen/admin_leaves_test.dart +++ b/test/unit_test/admin/leaves/leave_screen/admin_leaves_test.dart @@ -28,7 +28,7 @@ void main() { Leave andrewCurrentYearLeave = Leave( leaveId: 'leave-id', uid: 'andrew-id', - type: LeaveType.annualLeave, + type: LeaveType.casualLeave, startDate: DateTime.now().dateOnly, endDate: DateTime.now().dateOnly.add(const Duration(days: 1)), total: 2, @@ -56,7 +56,7 @@ void main() { Leave joiPreviousYearLeave = Leave( leaveId: 'leave-id', uid: 'joi-id', - type: LeaveType.annualLeave, + type: LeaveType.casualLeave, startDate: DateTime.now().dateOnly.subtract(const Duration(days: 365)), endDate: DateTime.now().dateOnly.subtract(const Duration(days: 364)), total: 2, diff --git a/test/unit_test/admin/member/detail/employee_detail_bloc_test.dart b/test/unit_test/admin/member/detail/employee_detail_bloc_test.dart index 9f642a80..aa8f3daa 100644 --- a/test/unit_test/admin/member/detail/employee_detail_bloc_test.dart +++ b/test/unit_test/admin/member/detail/employee_detail_bloc_test.dart @@ -1,6 +1,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; +import 'package:projectunity/data/model/leave_count.dart'; import 'package:projectunity/data/repo/employee_repo.dart'; import 'package:projectunity/data/core/exception/error_const.dart'; import 'package:projectunity/data/model/employee/employee.dart'; @@ -49,7 +50,7 @@ void main() { employeeRepo = MockEmployeeRepo(); employeeDetailBloc = EmployeeDetailBloc(accountService, spaceService, userStateNotifier, employeeService, leaveRepo, employeeRepo); - when(leaveRepo.getUserUsedLeaves(uid: employee.uid)).thenAnswer((_) async => 10); + when(leaveRepo.getUserUsedLeaves(uid: employee.uid)).thenAnswer((_) async => const LeaveCounts(urgentLeaves: 5,casualLeaves: 5)); when(userStateNotifier.currentSpaceId).thenReturn("space-id"); when(spaceService.getPaidLeaves(spaceId: "space-id")).thenAnswer((_) async => 12); }); @@ -109,7 +110,7 @@ void main() { employeeDetailBloc .add(EmployeeDetailInitialLoadEvent(employeeId: employee.uid)); EmployeeDetailLoadedState loadedState = EmployeeDetailLoadedState( - employee: employee, timeOffRatio: 10 / 12, usedLeaves: 10); + employee: employee, timeOffRatio: 10 / 12, usedLeaves: const LeaveCounts(urgentLeaves: 5,casualLeaves: 5)); expectLater(employeeDetailBloc.stream, emitsInOrder([EmployeeDetailLoadingState(), loadedState])); }); diff --git a/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.dart b/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.dart index 822e6e1a..4d1f2ff6 100644 --- a/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.dart +++ b/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.dart @@ -4,6 +4,7 @@ import 'package:mockito/mockito.dart'; import 'package:projectunity/data/core/exception/error_const.dart'; import 'package:projectunity/data/core/extensions/date_time.dart'; import 'package:projectunity/data/core/extensions/map_extension.dart'; +import 'package:projectunity/data/core/functions/shared_function.dart'; import 'package:projectunity/data/core/utils/bloc_status.dart'; import 'package:projectunity/data/model/employee/employee.dart'; import 'package:projectunity/data/model/leave/leave.dart'; @@ -22,6 +23,7 @@ void main() { late LeaveRepo leaveRepo; late UserStateNotifier userStateNotifier; late ApplyLeaveBloc leaveRequestBloc; + late AppFunctions appFunctions; late NotificationService notificationService; final DateTime currentDate = DateTime.now().dateOnly; @@ -38,8 +40,9 @@ void main() { leaveRepo = MockLeaveRepo(); userStateNotifier = MockUserStateNotifier(); notificationService = MockNotificationService(); - leaveRequestBloc = - ApplyLeaveBloc(userStateNotifier, leaveRepo, notificationService); + appFunctions = AppFunctions(); + leaveRequestBloc = ApplyLeaveBloc( + userStateNotifier, leaveRepo, notificationService, appFunctions); when(userStateNotifier.userUID).thenReturn("id"); when(userStateNotifier.employeeId).thenReturn("id"); @@ -47,8 +50,8 @@ void main() { }); test("leave Type change test", () { - leaveRequestBloc - .add(ApplyLeaveChangeLeaveTypeEvent(leaveType: LeaveType.urgentLeave)); + leaveRequestBloc.add( + ApplyLeaveChangeLeaveTypeEvent(leaveType: LeaveType.urgentLeave)); expect( leaveRequestBloc.stream, emits(ApplyLeaveState( @@ -224,6 +227,7 @@ void main() { dateDuration: currentDayMap.getSelectedLeaveOfTheDays( startDate: currentDate, endDate: futureDate))) .thenAnswer((_) async => true); + when(userStateNotifier.userUID).thenReturn('id'); Map updatedSelectedLeaves = currentDayMap.getSelectedLeaveOfTheDays( diff --git a/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.mocks.dart b/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.mocks.dart index 27330591..8868e6cc 100644 --- a/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.mocks.dart +++ b/test/unit_test/user/leaves/apply_leave/apply_leave_bloc_test.mocks.dart @@ -8,6 +8,7 @@ import 'dart:ui' as _i12; import 'package:cloud_firestore/cloud_firestore.dart' as _i8; import 'package:mockito/mockito.dart' as _i1; +import 'package:projectunity/data/core/functions/shared_function.dart' as _i14; import 'package:projectunity/data/model/account/account.dart' as _i10; import 'package:projectunity/data/model/employee/employee.dart' as _i4; import 'package:projectunity/data/model/leave/leave.dart' as _i7; @@ -487,3 +488,31 @@ class MockNotificationService extends _i1.Mock returnValueForMissingStub: _i6.Future.value(), ) as _i6.Future); } + +/// A class which mocks [AppFunctions]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockAppFunctions extends _i1.Mock implements _i14.AppFunctions { + MockAppFunctions() { + _i1.throwOnMissingStub(this); + } + + @override + bool isUrgentLeave({ + required DateTime? startDate, + required DateTime? appliedOn, + required double? totalLeaves, + }) => + (super.noSuchMethod( + Invocation.method( + #isUrgentLeave, + [], + { + #startDate: startDate, + #appliedOn: appliedOn, + #totalLeaves: totalLeaves, + }, + ), + returnValue: false, + ) as bool); +} diff --git a/test/unit_test/user/leaves/detail/user_leave_detail_bloc_test.dart b/test/unit_test/user/leaves/detail/user_leave_detail_bloc_test.dart index b16e5099..405fd117 100644 --- a/test/unit_test/user/leaves/detail/user_leave_detail_bloc_test.dart +++ b/test/unit_test/user/leaves/detail/user_leave_detail_bloc_test.dart @@ -23,7 +23,7 @@ void main() { upcomingLeave = Leave( leaveId: 'leaveId', uid: 'Uid', - type: LeaveType.marriageLeave, + type: LeaveType.casualLeave, startDate: DateTime.now().add(const Duration(days: 2)), endDate: DateTime.now().add(const Duration(days: 3)), total: 1, @@ -35,16 +35,13 @@ void main() { pastLeave = Leave( leaveId: 'leaveId', uid: 'Uid', - type: LeaveType.marriageLeave, - startDate: - DateTime.now().subtract(const Duration(days: 3)), - endDate: - DateTime.now().subtract(const Duration(days: 2)), + type: LeaveType.casualLeave, + startDate: DateTime.now().subtract(const Duration(days: 3)), + endDate: DateTime.now().subtract(const Duration(days: 2)), total: 1, reason: 'Suffering from viral fever', status: LeaveStatus.pending, - appliedOn: - DateTime.now().subtract(const Duration(days: 4)), + appliedOn: DateTime.now().subtract(const Duration(days: 4)), perDayDuration: const [LeaveDayDuration.firstHalfLeave]); }); @@ -73,7 +70,8 @@ void main() { () { userLeaveDetailBloc.add(FetchLeaveDetailEvent(leaveId: leaveId)); - when(leaveRepo.fetchLeave(leaveId: leaveId)).thenAnswer((_) async => pastLeave); + when(leaveRepo.fetchLeave(leaveId: leaveId)) + .thenAnswer((_) async => pastLeave); expectLater( userLeaveDetailBloc.stream, emitsInOrder([ @@ -99,7 +97,8 @@ void main() { test( 'Emits loading state and error state if leaveId is not matched with any document reference and found null from firestore', () { - when(leaveRepo.fetchLeave(leaveId: leaveId)).thenAnswer((_) async => null); + when(leaveRepo.fetchLeave(leaveId: leaveId)) + .thenAnswer((_) async => null); userLeaveDetailBloc.add(FetchLeaveDetailEvent(leaveId: leaveId)); expectLater( userLeaveDetailBloc.stream, @@ -118,12 +117,16 @@ void main() { }); test('Emit failure state if leave canceled', () { - when(leaveRepo.updateLeaveStatus(leaveId: leaveId, status: LeaveStatus.cancelled)).thenThrow(Exception('error')); + when(leaveRepo.updateLeaveStatus( + leaveId: leaveId, status: LeaveStatus.cancelled)) + .thenThrow(Exception('error')); userLeaveDetailBloc.add(CancelLeaveApplicationEvent(leaveId: leaveId)); expectLater( userLeaveDetailBloc.stream, - emitsInOrder( - [UserLeaveDetailLoadingState(), UserLeaveDetailErrorState(error: firestoreFetchDataError)])); + emitsInOrder([ + UserLeaveDetailLoadingState(), + UserLeaveDetailErrorState(error: firestoreFetchDataError) + ])); }); }); } diff --git a/test/unit_test/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_bloc_test.dart b/test/unit_test/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_bloc_test.dart index d4ee3093..bf7e2644 100644 --- a/test/unit_test/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_bloc_test.dart +++ b/test/unit_test/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_bloc_test.dart @@ -3,6 +3,7 @@ import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:projectunity/data/core/exception/error_const.dart'; import 'package:projectunity/data/core/utils/bloc_status.dart'; +import 'package:projectunity/data/model/leave_count.dart'; import 'package:projectunity/data/provider/user_state.dart'; import 'package:projectunity/data/repo/leave_repo.dart'; import 'package:projectunity/data/services/space_service.dart'; @@ -21,7 +22,7 @@ void main() { UserLeaveCountState loadingState = const UserLeaveCountState( status: Status.loading, - usedLeavesCounts: 0, + usedLeavesCounts: LeaveCounts(), leavePercentage: 0, error: null); @@ -47,25 +48,25 @@ void main() { userLeaveCountBloc.state, const UserLeaveCountState( status: Status.initial, - usedLeavesCounts: 0, + usedLeavesCounts: LeaveCounts(), leavePercentage: 0, error: null)); }); test( 'emits loading state and success state after add FetchUserLeaveCountEvent respectively', () { - userLeaveCountBloc.add(FetchLeaveCountEvent()); + userLeaveCountBloc.add(FetchLeaveCountEvent()); when(userStateNotifier.employeeId).thenReturn(employeeId); when(userStateNotifier.currentSpaceId).thenReturn("space-id"); - when(leaveRepo.getUserUsedLeaves(uid: employeeId)) - .thenAnswer((_) async => 7); + when(leaveRepo.getUserUsedLeaves(uid: employeeId)).thenAnswer( + (_) async => const LeaveCounts(urgentLeaves: 2, casualLeaves: 5)); when(spaceService.getPaidLeaves(spaceId: 'space-id')) .thenAnswer((_) async => 12); const UserLeaveCountState successState = UserLeaveCountState( status: Status.success, - usedLeavesCounts: 7, + usedLeavesCounts: LeaveCounts(urgentLeaves: 2, casualLeaves: 5), leavePercentage: 7 / 12, error: null); expectLater(userLeaveCountBloc.stream, @@ -77,13 +78,13 @@ void main() { when(userStateNotifier.employeeId).thenReturn('Ca 1044'); when(userStateNotifier.currentSpaceId).thenReturn('space-id'); - when(leaveRepo.getUserUsedLeaves(uid: 'Ca 1044')) - .thenAnswer((_) async => 7); + when(leaveRepo.getUserUsedLeaves(uid: 'Ca 1044')).thenAnswer( + (_) async => const LeaveCounts(urgentLeaves: 2, casualLeaves: 5)); when(spaceService.getPaidLeaves(spaceId: 'space-id')) .thenThrow(Exception('error')); const UserLeaveCountState errorState = UserLeaveCountState( status: Status.success, - usedLeavesCounts: 0, + usedLeavesCounts: LeaveCounts(urgentLeaves: 0, casualLeaves: 0), leavePercentage: 0, error: firestoreFetchDataError); expectLater( diff --git a/test/unit_test/user/members/detail/user_employee_detail_bloc_test.dart b/test/unit_test/user/members/detail/user_employee_detail_bloc_test.dart index a1b6f889..f853da52 100644 --- a/test/unit_test/user/members/detail/user_employee_detail_bloc_test.dart +++ b/test/unit_test/user/members/detail/user_employee_detail_bloc_test.dart @@ -19,7 +19,7 @@ void main() { Leave upcomingApproveLeave = Leave( leaveId: 'leaveId', uid: 'uid', - type: LeaveType.annualLeave, + type: LeaveType.casualLeave, startDate: DateTime.now().add(const Duration(days: 2)).dateOnly, endDate: DateTime.now().add(const Duration(days: 1)).dateOnly, total: 2,