Skip to content

Commit

Permalink
Add unit test for celebrations bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s committed Mar 13, 2024
1 parent 24e4f4d commit 5988197
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 138 deletions.
11 changes: 10 additions & 1 deletion ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
"images" : [
{
"filename" : "appstore.png",
"idiom" : "universal"
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
Expand Down
5 changes: 3 additions & 2 deletions lib/data/core/extensions/date_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ extension DateExtension on DateTime {
String toMonthYear() {
return DateFormat("MMMM, yyyy").format(this);
}

String toDateWithoutYear(BuildContext context) {
final today= DateUtils.dateOnly(DateTime.now());
if(isAtSameMomentAs(today)){
final today = DateUtils.dateOnly(DateTime.now());
if (isAtSameMomentAs(today)) {
return context.l10n.dateFormatter_today;
}
return DateFormat("MMMM d, EEE").format(this);
Expand Down
6 changes: 6 additions & 0 deletions lib/data/core/mixin/input_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ mixin InputValidationMixin {
email.length >= 4 &&
email.contains('@') &&
email.contains('.');

bool validPhoneNumber(String? number) {
String pattern = r'(^(?:[+0]9)?[0-9]{10}$)';
RegExp regExp = RegExp(pattern);
return number != null && regExp.hasMatch(number);
}
}
17 changes: 9 additions & 8 deletions lib/data/core/utils/date_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,23 @@ class DateFormatter {
}
}

String showBirthdays({required DateTime dateTime,required String name}){
String showBirthdays({required DateTime dateTime, required String name}) {
final today = DateTime.now().dateOnly;
if(dateTime.dateOnly.isAtSameMomentAs(today)){
if (dateTime.dateOnly.isAtSameMomentAs(today)) {
return _localization.present_birthday_text(name);
}else{
} else {
return "${_localization.upcoming_birthday_text(name)} ${getDateRepresentation(dateTime)}🎉";
}
}

String showAnniversaries({required DateTime dateTime,required String name, int? number}){
String showAnniversaries(
{required DateTime dateTime, required String name, int? number}) {
final today = DateTime.now().dateOnly;
final difference= dateTime.difference(today);
int yearDifference= (difference.inDays/365).floor();
if(dateTime.dateOnly.isAtSameMomentAs(today)){
final difference = dateTime.difference(today);
int yearDifference = (difference.inDays / 365).floor();
if (dateTime.dateOnly.isAtSameMomentAs(today)) {
return _localization.present_anniversary_text(name, yearDifference);
}else{
} else {
return "${_localization.upcoming_anniversary_text(name, yearDifference)} ${getDateRepresentation(dateTime)}🎉";
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/data/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@
"fill_require_details_error": "Please fill required details!",
"invalid_date_selection_error": "Please select valid date",
"apply_leave_minimum_one_hour_error": "Please apply leave for minimum half day",
"invalid_mobile_number_error":"Please enter valid phone number",
"add_member_employee_exists_error": "Member already exists!",
"leave_already_applied_error_message": "Leave request has been already applied!",
"provide_required_information": "Please provide the required information to continue",
Expand Down
5 changes: 2 additions & 3 deletions lib/ui/admin/home/home_screen/admin_home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class AdminHomeScreenPage extends StatelessWidget {
create: (context) =>
getIt<WhoIsOutCardBloc>()..add(FetchWhoIsOutCardLeaves())),
BlocProvider(
create: (context) => getIt<CelebrationsBloc>()
..add(FetchCelebrations(DateTime.now()))),
create: (context) =>
getIt<CelebrationsBloc>()..add(FetchCelebrations())),
],
child: const AdminHomeScreen(),
);
Expand Down Expand Up @@ -84,7 +84,6 @@ class _AdminHomeScreenState extends State<AdminHomeScreen> {
children: [
const WhoIsOutCard(),
const EventCard(),

const SizedBox(
height: 20,
),
Expand Down
1 change: 0 additions & 1 deletion lib/ui/admin/home/home_screen/widget/request_list.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localization.dart';
import 'package:go_router/go_router.dart';
import 'package:projectunity/data/core/extensions/context_extension.dart';
import 'package:sticky_headers/sticky_headers.dart';
Expand Down
49 changes: 26 additions & 23 deletions lib/ui/shared/events/bloc/celebrations_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {
List<Employee> employees = [];
List<Event> allBirthdayEvents = [];
List<Event> allAnniversaryEvents = [];
List<Event> currentWeekBday=[];
List<Event> currentWeekAnniversaries=[];
List<Event> currentWeekBday = [];
List<Event> currentWeekAnniversaries = [];

CelebrationsBloc(this._employeeRepo)
: super(CelebrationsState(currentWeek: DateTime.now())) {
CelebrationsBloc(this._employeeRepo) : super(const CelebrationsState()) {
on<FetchCelebrations>(_fetchEvent);
on<ShowBirthdaysEvent>(_showAllBirthdays);
on<ShowAnniversariesEvent>(_showAllAnniversaries);
}

Future<void> _fetchEvent(FetchCelebrations event,
Emitter<CelebrationsState> emit) async {
Future<void> _fetchEvent(
FetchCelebrations event, Emitter<CelebrationsState> emit) async {
try {
emit(state.copyWith(status: Status.loading));
employees = _employeeRepo.allEmployees
Expand All @@ -37,33 +36,37 @@ class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {
employees = employees.map((e) {
if (e.dateOfBirth != null) {
final birthdate = e.dateOfBirth!.convertToUpcomingDay();
final Event event =
Event(name: e.name, dateTime: birthdate, imageUrl: e.imageUrl);
final Event event = Event(
name: e.name,
dateTime: DateUtils.dateOnly(birthdate),
imageUrl: e.imageUrl);
allBirthdayEvents.add(event);
}
final joiningDate = e.dateOfJoining.convertToUpcomingDay();
final Event event =
Event(name: e.name, dateTime: joiningDate, imageUrl: e.imageUrl);
final Event event = Event(
name: e.name,
dateTime: DateUtils.dateOnly(joiningDate),
imageUrl: e.imageUrl);
allAnniversaryEvents.add(event);
return e;
}).toList();
allBirthdayEvents.sort((a, b) => a.dateTime.compareTo(b.dateTime));
allAnniversaryEvents.sort((a, b) => a.dateTime.compareTo(b.dateTime));


currentWeekBday = _getBirthdays();
currentWeekAnniversaries = _getAnniversaries();
emit(state
.copyWith(status: Status.success, birthdays: currentWeekBday,
anniversaries:currentWeekAnniversaries));
currentWeekAnniversaries = _getAnniversaries();
emit(state.copyWith(
status: Status.success,
birthdays: currentWeekBday,
anniversaries: currentWeekAnniversaries));
} on Exception {
emit(
state.copyWith(status: Status.error, error: firestoreFetchDataError));
}
}

void _showAllBirthdays(ShowBirthdaysEvent event,
Emitter<CelebrationsState> emit) {
void _showAllBirthdays(
ShowBirthdaysEvent event, Emitter<CelebrationsState> emit) {
bool showAllBirthdays = !state.showAllBdays;
if (showAllBirthdays) {
emit(state.copyWith(
Expand All @@ -74,8 +77,8 @@ class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {
}
}

void _showAllAnniversaries(ShowAnniversariesEvent event,
Emitter<CelebrationsState> emit) {
void _showAllAnniversaries(
ShowAnniversariesEvent event, Emitter<CelebrationsState> emit) {
bool allAnniversaries = !state.showAllAnniversaries;
if (allAnniversaries) {
emit(state.copyWith(
Expand All @@ -90,16 +93,16 @@ class CelebrationsBloc extends Bloc<CelebrationEvent, CelebrationsState> {

List<Event> _getBirthdays() {
final List<Event> birthdays = allBirthdayEvents.where((event) {
return event.dateTime.isDateInCurrentWeek(
DateUtils.dateOnly(DateTime.now()));
return event.dateTime
.isDateInCurrentWeek(DateUtils.dateOnly(DateTime.now()));
}).toList();
return birthdays;
}

List<Event> _getAnniversaries() {
final List<Event> anniversaries = allAnniversaryEvents.where((event) {
return event.dateTime.isDateInCurrentWeek(
DateUtils.dateOnly(DateTime.now()));
return event.dateTime
.isDateInCurrentWeek(DateUtils.dateOnly(DateTime.now()));
}).toList();
return anniversaries;
}
Expand Down
12 changes: 5 additions & 7 deletions lib/ui/shared/events/bloc/celebrations_event.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
abstract class CelebrationEvent{}
abstract class CelebrationEvent {}

class FetchCelebrations extends CelebrationEvent{
final DateTime dateTime;
FetchCelebrations( this.dateTime);
}
class FetchCelebrations extends CelebrationEvent {}

class ShowBirthdaysEvent extends CelebrationEvent{}
class ShowAnniversariesEvent extends CelebrationEvent{}
class ShowBirthdaysEvent extends CelebrationEvent {}

class ShowAnniversariesEvent extends CelebrationEvent {}
21 changes: 9 additions & 12 deletions lib/ui/shared/events/bloc/celebrations_state.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../../../data/core/utils/bloc_status.dart';
import '../../../../data/model/employee/employee.dart';
import '../model/event.dart';
part 'celebrations_state.freezed.dart';

@freezed
class CelebrationsState with _$CelebrationsState{
const factory CelebrationsState({
@Default(Status.initial) Status status,
required DateTime currentWeek,
@Default(false) bool showAllBdays,
@Default(false) bool showAllAnniversaries,
@Default([]) List<Event> birthdays,
@Default([]) List<Event> anniversaries,
String? error
})= _CelebrationsState;
}
class CelebrationsState with _$CelebrationsState {
const factory CelebrationsState(
{@Default(Status.initial) Status status,
@Default(false) bool showAllBdays,
@Default(false) bool showAllAnniversaries,
@Default([]) List<Event> birthdays,
@Default([]) List<Event> anniversaries,
String? error}) = _CelebrationsState;
}
24 changes: 1 addition & 23 deletions lib/ui/shared/events/bloc/celebrations_state.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$CelebrationsState {
Status get status => throw _privateConstructorUsedError;
DateTime get currentWeek => throw _privateConstructorUsedError;
bool get showAllBdays => throw _privateConstructorUsedError;
bool get showAllAnniversaries => throw _privateConstructorUsedError;
List<Event> get birthdays => throw _privateConstructorUsedError;
Expand All @@ -37,7 +36,6 @@ abstract class $CelebrationsStateCopyWith<$Res> {
@useResult
$Res call(
{Status status,
DateTime currentWeek,
bool showAllBdays,
bool showAllAnniversaries,
List<Event> birthdays,
Expand All @@ -59,7 +57,6 @@ class _$CelebrationsStateCopyWithImpl<$Res, $Val extends CelebrationsState>
@override
$Res call({
Object? status = null,
Object? currentWeek = null,
Object? showAllBdays = null,
Object? showAllAnniversaries = null,
Object? birthdays = null,
Expand All @@ -71,10 +68,6 @@ class _$CelebrationsStateCopyWithImpl<$Res, $Val extends CelebrationsState>
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as Status,
currentWeek: null == currentWeek
? _value.currentWeek
: currentWeek // ignore: cast_nullable_to_non_nullable
as DateTime,
showAllBdays: null == showAllBdays
? _value.showAllBdays
: showAllBdays // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -109,7 +102,6 @@ abstract class _$$CelebrationsStateImplCopyWith<$Res>
@useResult
$Res call(
{Status status,
DateTime currentWeek,
bool showAllBdays,
bool showAllAnniversaries,
List<Event> birthdays,
Expand All @@ -129,7 +121,6 @@ class __$$CelebrationsStateImplCopyWithImpl<$Res>
@override
$Res call({
Object? status = null,
Object? currentWeek = null,
Object? showAllBdays = null,
Object? showAllAnniversaries = null,
Object? birthdays = null,
Expand All @@ -141,10 +132,6 @@ class __$$CelebrationsStateImplCopyWithImpl<$Res>
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as Status,
currentWeek: null == currentWeek
? _value.currentWeek
: currentWeek // ignore: cast_nullable_to_non_nullable
as DateTime,
showAllBdays: null == showAllBdays
? _value.showAllBdays
: showAllBdays // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -174,7 +161,6 @@ class __$$CelebrationsStateImplCopyWithImpl<$Res>
class _$CelebrationsStateImpl implements _CelebrationsState {
const _$CelebrationsStateImpl(
{this.status = Status.initial,
required this.currentWeek,
this.showAllBdays = false,
this.showAllAnniversaries = false,
final List<Event> birthdays = const [],
Expand All @@ -187,8 +173,6 @@ class _$CelebrationsStateImpl implements _CelebrationsState {
@JsonKey()
final Status status;
@override
final DateTime currentWeek;
@override
@JsonKey()
final bool showAllBdays;
@override
Expand Down Expand Up @@ -217,7 +201,7 @@ class _$CelebrationsStateImpl implements _CelebrationsState {

@override
String toString() {
return 'CelebrationsState(status: $status, currentWeek: $currentWeek, showAllBdays: $showAllBdays, showAllAnniversaries: $showAllAnniversaries, birthdays: $birthdays, anniversaries: $anniversaries, error: $error)';
return 'CelebrationsState(status: $status, showAllBdays: $showAllBdays, showAllAnniversaries: $showAllAnniversaries, birthdays: $birthdays, anniversaries: $anniversaries, error: $error)';
}

@override
Expand All @@ -226,8 +210,6 @@ class _$CelebrationsStateImpl implements _CelebrationsState {
(other.runtimeType == runtimeType &&
other is _$CelebrationsStateImpl &&
(identical(other.status, status) || other.status == status) &&
(identical(other.currentWeek, currentWeek) ||
other.currentWeek == currentWeek) &&
(identical(other.showAllBdays, showAllBdays) ||
other.showAllBdays == showAllBdays) &&
(identical(other.showAllAnniversaries, showAllAnniversaries) ||
Expand All @@ -243,7 +225,6 @@ class _$CelebrationsStateImpl implements _CelebrationsState {
int get hashCode => Object.hash(
runtimeType,
status,
currentWeek,
showAllBdays,
showAllAnniversaries,
const DeepCollectionEquality().hash(_birthdays),
Expand All @@ -261,7 +242,6 @@ class _$CelebrationsStateImpl implements _CelebrationsState {
abstract class _CelebrationsState implements CelebrationsState {
const factory _CelebrationsState(
{final Status status,
required final DateTime currentWeek,
final bool showAllBdays,
final bool showAllAnniversaries,
final List<Event> birthdays,
Expand All @@ -271,8 +251,6 @@ abstract class _CelebrationsState implements CelebrationsState {
@override
Status get status;
@override
DateTime get currentWeek;
@override
bool get showAllBdays;
@override
bool get showAllAnniversaries;
Expand Down
Loading

0 comments on commit 5988197

Please sign in to comment.