Skip to content

Commit

Permalink
Refactor unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Aug 7, 2023
1 parent b91e9d7 commit d08445c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -34,18 +35,18 @@ void main() {
AdminLeaveDetailsState leaveCountLoadingState =
const AdminLeaveDetailsState(
adminReply: '',
usedLeavesCount: 0.0,
usedLeavesCount: LeaveCounts(),
error: null,
leaveCountStatus: Status.loading,
actionStatus: Status.initial);

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);
Expand All @@ -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);
Expand All @@ -74,7 +75,7 @@ void main() {
AdminLeaveDetailsState responseLoadingState =
const AdminLeaveDetailsState(
adminReply: '',
usedLeavesCount: 0,
usedLeavesCount: LeaveCounts(),
actionStatus: Status.loading,
leaveCountStatus: Status.initial,
error: null);
Expand Down Expand Up @@ -177,7 +178,7 @@ void main() {
leaveId: 'leave-id'));
AdminLeaveDetailsState errorState = const AdminLeaveDetailsState(
adminReply: '',
usedLeavesCount: 0,
usedLeavesCount: LeaveCounts(),
error: firestoreFetchDataError,
actionStatus: Status.error);
expectLater(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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]));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -38,17 +40,18 @@ 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");
when(leaveRepo.generateLeaveId).thenReturn("new-leave-id");
});

test("leave Type change test", () {
leaveRequestBloc
.add(ApplyLeaveChangeLeaveTypeEvent(leaveType: LeaveType.urgentLeave));
leaveRequestBloc.add(
ApplyLeaveChangeLeaveTypeEvent(leaveType: LeaveType.urgentLeave));
expect(
leaveRequestBloc.stream,
emits(ApplyLeaveState(
Expand Down Expand Up @@ -224,6 +227,7 @@ void main() {
dateDuration: currentDayMap.getSelectedLeaveOfTheDays(
startDate: currentDate, endDate: futureDate)))
.thenAnswer((_) async => true);

when(userStateNotifier.userUID).thenReturn('id');
Map<DateTime, LeaveDayDuration> updatedSelectedLeaves =
currentDayMap.getSelectedLeaveOfTheDays(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -487,3 +488,31 @@ class MockNotificationService extends _i1.Mock
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
}

/// 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);
}
29 changes: 16 additions & 13 deletions test/unit_test/user/leaves/detail/user_leave_detail_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]);
});

Expand Down Expand Up @@ -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([
Expand All @@ -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,
Expand All @@ -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)
]));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +22,7 @@ void main() {

UserLeaveCountState loadingState = const UserLeaveCountState(
status: Status.loading,
usedLeavesCounts: 0,
usedLeavesCounts: LeaveCounts(),
leavePercentage: 0,
error: null);

Expand All @@ -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,
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d08445c

Please sign in to comment.