Skip to content

Commit

Permalink
Add unit test for user leaves screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s committed Mar 5, 2024
1 parent 1671d8c commit 3bcb865
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 55 deletions.
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class UserLeaveBloc extends Bloc<UserLeavesEvents, UserLeaveState> {
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));
Expand All @@ -78,7 +78,7 @@ class UserLeaveBloc extends Bloc<UserLeavesEvents, UserLeaveState> {
emit(state.copyWith(
fetchMoreDataStatus: Status.success,
casualLeaves:
casualLeaves.groupByMonth((leave) => leave.appliedOn)));
casualLeaves.groupByMonth((leave) => leave.startDate)));
}
} on Exception {
emit(state.copyWith(
Expand Down Expand Up @@ -118,7 +118,10 @@ class UserLeaveBloc extends Bloc<UserLeavesEvents, UserLeaveState> {
Future<void> _updateLeave(
UpdateLeave event, Emitter<UserLeaveState> 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<Leave> casualLeaves = leaves
Expand All @@ -128,7 +131,7 @@ class UserLeaveBloc extends Bloc<UserLeavesEvents, UserLeaveState> {
.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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,7 +26,6 @@ void main() {
setUp(() {
leaveRepo = MockLeaveRepo();
userStateNotifier = MockUserStateNotifier();
spaceService = MockSpaceService();
userLeaveCountBloc = UserLeaveCountBloc(leaveRepo, userStateNotifier);
});

Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -55,7 +55,7 @@ void main() {
LeaveDayDuration.firstHalfLeave
]);

Leave moreLeave = Leave(
Leave urgentLeave = Leave(
leaveId: 'Leave-Id',
uid: "user id",
type: LeaveType.urgentLeave,
Expand Down Expand Up @@ -87,29 +87,44 @@ 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,
emitsInOrder([
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,
Expand All @@ -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))));
});
});
Expand All @@ -146,54 +161,75 @@ 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,
emitsInOrder([
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,
emitsInOrder([
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))));
});
});

Expand All @@ -212,22 +248,33 @@ 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,
emitsInOrder([
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(
Expand All @@ -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)),
]));
});
});
Expand Down

0 comments on commit 3bcb865

Please sign in to comment.