Skip to content

Commit

Permalink
Fix Upcoming Events
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s committed Mar 21, 2024
1 parent 427f68e commit eeff1ae
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 87 deletions.
72 changes: 36 additions & 36 deletions lib/data/di/service_locator.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/data/repo/employee_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EmployeeRepo {

List<Employee> get allEmployees {
if (_employeeController.hasValue) {
return _employeeController.stream.value;
return _employeeController.value;
}
return [];
}
Expand Down
12 changes: 5 additions & 7 deletions lib/ui/shared/events/bloc/celebrations_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CelebrationEvent, CelebrationsState> {
final EmployeeRepo _employeeRepo;
final EmployeeService _employeeService;
List<Employee> employees = [];
List<Event> allBirthdayEvents = [];
List<Event> allAnniversaryEvents = [];
List<Event> currentWeekBday = [];
List<Event> currentWeekAnniversaries = [];

CelebrationsBloc(this._employeeRepo) : super(const CelebrationsState()) {
CelebrationsBloc(this._employeeService) : super(const CelebrationsState()) {
on<FetchCelebrations>(_fetchEvent);
on<ShowBirthdaysEvent>(_showAllBirthdays);
on<ShowAnniversariesEvent>(_showAllAnniversaries);
Expand All @@ -30,10 +30,8 @@ class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {
FetchCelebrations event, Emitter<CelebrationsState> 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<Employee> allEmployees = await _employeeService.getEmployees();
employees = allEmployees.map((e) {
if (e.dateOfBirth != null) {
final birthdate = e.dateOfBirth!.convertToUpcomingDay();
final Event event = Event(
Expand Down
22 changes: 12 additions & 10 deletions test/unit_test/shared/events/celebration_event_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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", () {
Expand All @@ -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,
Expand All @@ -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());

Expand All @@ -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());

Expand Down
Loading

0 comments on commit eeff1ae

Please sign in to comment.