From 3bcb865104c93ce2cfd3636e08a42aff70316d88 Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Tue, 5 Mar 2024 17:53:04 +0530 Subject: [PATCH] Add unit test for user leaves screen --- lib/main.dart | 1 - .../bloc/leaves/user_leave_bloc.dart | 13 +- .../user_leave_count_bloc_test.dart | 12 +- .../bloc/leaves/user_leave_bloc_test.dart | 131 ++++++++++++------ 4 files changed, 102 insertions(+), 55 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index b8ec969d..ff0a474a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,7 +15,6 @@ import 'package:projectunity/ui/app.dart'; import 'package:projectunity/ui/widget/error/error_screen.dart'; import 'data/core/utils/const/app_const.dart'; import 'data/configs/scroll_behavior.dart'; -import 'data/bloc/network/network_connection_bloc.dart'; import 'data/di/service_locator.dart'; import 'package:flutter_web_plugins/url_strategy.dart'; import 'firebase_options.dart'; diff --git a/lib/ui/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc.dart b/lib/ui/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc.dart index 3c109101..3bcaa8d0 100644 --- a/lib/ui/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc.dart +++ b/lib/ui/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc.dart @@ -69,7 +69,7 @@ class UserLeaveBloc extends Bloc { emit(state.copyWith( fetchMoreDataStatus: Status.success, casualLeaves: - casualLeaves.groupByMonth((leave) => leave.appliedOn))); + casualLeaves.groupByMonth((leave) => leave.startDate))); } else if (event.leaveType == LeaveType.urgentLeave && !_isLoadedMaxUrgent) { emit(state.copyWith(fetchMoreDataStatus: Status.loading)); @@ -78,7 +78,7 @@ class UserLeaveBloc extends Bloc { emit(state.copyWith( fetchMoreDataStatus: Status.success, casualLeaves: - casualLeaves.groupByMonth((leave) => leave.appliedOn))); + casualLeaves.groupByMonth((leave) => leave.startDate))); } } on Exception { emit(state.copyWith( @@ -118,7 +118,10 @@ class UserLeaveBloc extends Bloc { Future _updateLeave( UpdateLeave event, Emitter emit) async { final leave = await _leaveRepo.fetchLeave(leaveId: event.leaveId); - final leaves = state.casualLeaves.values.merge(); + final leaves = [ + ...state.casualLeaves.values.merge(), + ...state.urgentLeaves.values.merge() + ]; leaves.removeWhereAndAdd( leave, (element) => element.leaveId == leave?.leaveId); List casualLeaves = leaves @@ -128,7 +131,7 @@ class UserLeaveBloc extends Bloc { .where((element) => element.type == LeaveType.urgentLeave) .toList(); emit(state.copyWith( - casualLeaves: casualLeaves.groupByMonth((leave) => leave.appliedOn), - urgentLeaves: urgentLeaves.groupByMonth((leave) => leave.appliedOn))); + casualLeaves: casualLeaves.groupByMonth((leave) => leave.startDate), + urgentLeaves: urgentLeaves.groupByMonth((leave) => leave.startDate))); } } 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 0cc4499d..1639412a 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 @@ -6,18 +6,16 @@ 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'; import 'package:projectunity/ui/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_bloc.dart'; import 'package:projectunity/ui/user/leaves/leaves_screen/bloc/leave_count/user_leave_count_state.dart'; import 'package:projectunity/ui/user/leaves/leaves_screen/bloc/leave_count/user_leave_cout_event.dart'; import 'user_leave_count_bloc_test.mocks.dart'; -@GenerateMocks([LeaveRepo, UserStateNotifier, SpaceService]) +@GenerateMocks([LeaveRepo, UserStateNotifier]) void main() { late LeaveRepo leaveRepo; late UserStateNotifier userStateNotifier; - late SpaceService spaceService; late UserLeaveCountBloc userLeaveCountBloc; UserLeaveCountState loadingState = const UserLeaveCountState( @@ -28,7 +26,6 @@ void main() { setUp(() { leaveRepo = MockLeaveRepo(); userStateNotifier = MockUserStateNotifier(); - spaceService = MockSpaceService(); userLeaveCountBloc = UserLeaveCountBloc(leaveRepo, userStateNotifier); }); @@ -56,8 +53,6 @@ void main() { when(userStateNotifier.currentSpaceId).thenReturn("space-id"); 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, @@ -72,10 +67,9 @@ void main() { when(userStateNotifier.employeeId).thenReturn('Ca 1044'); when(userStateNotifier.currentSpaceId).thenReturn('space-id'); - when(leaveRepo.getUserUsedLeaves(uid: 'Ca 1044')).thenAnswer( - (_) async => const LeaveCounts(urgentLeaves: 2, casualLeaves: 5)); - when(spaceService.getPaidLeaves(spaceId: 'space-id')) + when(leaveRepo.getUserUsedLeaves(uid: 'Ca 1044')) .thenThrow(Exception('error')); + const UserLeaveCountState errorState = UserLeaveCountState( status: Status.success, usedLeavesCounts: LeaveCounts(urgentLeaves: 0, casualLeaves: 0), diff --git a/test/unit_test/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc_test.dart b/test/unit_test/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc_test.dart index c20dc1f6..9e2f0c33 100644 --- a/test/unit_test/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc_test.dart +++ b/test/unit_test/user/leaves/leaves_screen/bloc/leaves/user_leave_bloc_test.dart @@ -25,10 +25,10 @@ void main() { const String employeeId = 'CA 1044'; DateTime today = DateTime.now().dateOnly; - Leave initialLeave = Leave( + Leave casualLeave = Leave( leaveId: 'Leave Id', uid: "user id", - type: LeaveType.urgentLeave, + type: LeaveType.casualLeave, startDate: today.add(const Duration(days: 1)), endDate: today.add(const Duration(days: 2)), total: 2, @@ -43,7 +43,7 @@ void main() { Leave initialLeaveWithChange = Leave( leaveId: 'Leave Id', uid: "user id", - type: LeaveType.urgentLeave, + type: LeaveType.casualLeave, startDate: today.add(const Duration(days: 1)), endDate: today.add(const Duration(days: 2)), total: 2, @@ -55,7 +55,7 @@ void main() { LeaveDayDuration.firstHalfLeave ]); - Leave moreLeave = Leave( + Leave urgentLeave = Leave( leaveId: 'Leave-Id', uid: "user id", type: LeaveType.urgentLeave, @@ -87,14 +87,21 @@ void main() { const UserLeaveState( fetchMoreDataStatus: Status.initial, casualLeaves: {}, + urgentLeaves: {}, error: null, status: Status.initial)); }); test('Load initial leave success test', () { when(userStateNotifier.employeeId).thenReturn(employeeId); - when(leaveRepo.leaves(uid: employeeId)).thenAnswer((_) async => - PaginatedLeaves(leaves: [initialLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.casualLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [casualLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.urgentLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [urgentLeave], lastDoc: lastDoc)); bloc.add(LoadInitialUserLeaves()); expectLater( bloc.stream, @@ -102,14 +109,22 @@ void main() { const UserLeaveState(status: Status.loading), UserLeaveState( status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), ])); }); test('Load initial leave failure test', () { when(userStateNotifier.employeeId).thenReturn(employeeId); - when(leaveRepo.leaves(uid: employeeId)).thenThrow(Exception('error')); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.casualLeave)) + .thenThrow(Exception('error')); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.urgentLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [urgentLeave], lastDoc: lastDoc)); bloc.add(LoadInitialUserLeaves()); expectLater( bloc.stream, @@ -121,13 +136,13 @@ void main() { }); test('Add applied leaves on leave list test', () { - when(leaveRepo.fetchLeave(leaveId: initialLeave.leaveId)) - .thenAnswer((realInvocation) async => initialLeave); - bloc.add(UpdateLeave(leaveId: initialLeave.leaveId)); + when(leaveRepo.fetchLeave(leaveId: casualLeave.leaveId)) + .thenAnswer((realInvocation) async => casualLeave); + bloc.add(UpdateLeave(leaveId: casualLeave.leaveId)); expectLater( bloc.stream, emits(UserLeaveState( - casualLeaves: [initialLeave] + casualLeaves: [casualLeave] .groupByMonth((element) => element.appliedOn)))); }); }); @@ -146,10 +161,16 @@ void main() { await bloc.close(); }); - test('Load initial leave success test', () { + test('Load initial leave success test for casualLeave', () { when(userStateNotifier.employeeId).thenReturn(employeeId); - when(leaveRepo.leaves(uid: employeeId)).thenAnswer((_) async => - PaginatedLeaves(leaves: [initialLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.casualLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [casualLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.urgentLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [urgentLeave], lastDoc: lastDoc)); bloc.add(LoadInitialUserLeaves()); expectLater( bloc.stream, @@ -157,15 +178,24 @@ void main() { const UserLeaveState(status: Status.loading), UserLeaveState( status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), ])); }); - test('fetch more data leave success test', () { - when(leaveRepo.leaves(uid: employeeId, lastDoc: lastDoc)).thenAnswer( - (_) async => - PaginatedLeaves(leaves: [moreLeave], lastDoc: moreDataLastDoc)); + test('fetch more data leave success test for Casual leave', () { + when(leaveRepo.leaves( + uid: employeeId, + lastDoc: lastDoc, + leaveType: LeaveType.casualLeave)) + .thenAnswer((_) async => PaginatedLeaves( + leaves: [casualLeave], lastDoc: moreDataLastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.urgentLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [casualLeave], lastDoc: lastDoc)); bloc.add(FetchMoreUserLeaves(LeaveType.casualLeave)); expectLater( bloc.stream, @@ -173,27 +203,33 @@ void main() { UserLeaveState( fetchMoreDataStatus: Status.loading, status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), UserLeaveState( fetchMoreDataStatus: Status.success, status: Status.success, - casualLeaves: [initialLeave, moreLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave, casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), ])); }); test('Update leaves on list test', () { - when(leaveRepo.fetchLeave(leaveId: initialLeave.leaveId)) - .thenAnswer((realInvocation) async => initialLeaveWithChange); - bloc.add(UpdateLeave(leaveId: initialLeave.leaveId)); + when(leaveRepo.fetchLeave(leaveId: initialLeaveWithChange.leaveId)) + .thenAnswer((_) async => initialLeaveWithChange); + bloc.add(UpdateLeave(leaveId: casualLeave.leaveId)); expectLater( bloc.stream, emits(UserLeaveState( fetchMoreDataStatus: Status.success, status: Status.success, - casualLeaves: [moreLeave, initialLeaveWithChange] - .groupByMonth((element) => element.appliedOn)))); + casualLeaves: [initialLeaveWithChange] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)))); }); }); @@ -212,8 +248,14 @@ void main() { test('Load initial leave success test', () { when(userStateNotifier.employeeId).thenReturn(employeeId); - when(leaveRepo.leaves(uid: employeeId)).thenAnswer((_) async => - PaginatedLeaves(leaves: [initialLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.casualLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [casualLeave], lastDoc: lastDoc)); + when(leaveRepo.leaves( + uid: employeeId, leaveType: LeaveType.urgentLeave)) + .thenAnswer((_) async => + PaginatedLeaves(leaves: [urgentLeave], lastDoc: lastDoc)); bloc.add(LoadInitialUserLeaves()); expectLater( bloc.stream, @@ -221,13 +263,18 @@ void main() { const UserLeaveState(status: Status.loading), UserLeaveState( status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), ])); }); test('fetch more data leave failure test', () { - when(leaveRepo.leaves(uid: employeeId, lastDoc: lastDoc)) + when(leaveRepo.leaves( + uid: employeeId, + lastDoc: lastDoc, + leaveType: LeaveType.casualLeave)) .thenThrow(Exception('error')); bloc.add(FetchMoreUserLeaves(LeaveType.casualLeave)); expectLater( @@ -236,14 +283,18 @@ void main() { UserLeaveState( fetchMoreDataStatus: Status.loading, status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), UserLeaveState( fetchMoreDataStatus: Status.error, error: firestoreFetchDataError, status: Status.success, - casualLeaves: [initialLeave] - .groupByMonth((element) => element.appliedOn)), + casualLeaves: [casualLeave] + .groupByMonth((element) => element.startDate), + urgentLeaves: [urgentLeave] + .groupByMonth((element) => element.startDate)), ])); }); });