Skip to content

Commit 26bfd68

Browse files
author
Andrés Soto
committed
refactor: TT-319 select first day of the month when select on calendar
1 parent 0125bfa commit 26bfd68

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

src/app/modules/time-entries/components/calendar/calendar.component.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ describe('CalendarComponent', () => {
200200
it('emit current date and call navigationEnable when call handleChangeDateEvent', () => {
201201
const calendarView: CalendarView = CalendarView.Month;
202202
const fakeValueEmit = {
203-
date: currentDate.toDate(),
204-
calendarView
203+
date: currentDate.toDate()
205204
};
206205
spyOn(component, 'navigationEnable');
207206
spyOn(component.changeDate, 'emit');
@@ -222,6 +221,14 @@ describe('CalendarComponent', () => {
222221
expect(component.calendarView).toEqual(fakeCalendarView);
223222
});
224223

224+
it('emit calendarView Day when call changeCalendarView', () => {
225+
const fakeCalendarView: CalendarView = CalendarView.Day;
226+
component.calendarView = CalendarView.Month;
227+
spyOn(component.changeView, 'emit');
228+
component.changeCalendarView(fakeCalendarView);
229+
expect(component.changeView.emit).toHaveBeenCalledWith({ calendarView: fakeCalendarView });
230+
});
231+
225232
it('set srcoll to current time marker in calendarView when is call scrollToCurrentTimeMarker', () => {
226233
const fakeCalendarView: CalendarView = CalendarView.Week;
227234
spyOn(component, 'scrollToCurrentTimeMarker');

src/app/modules/time-entries/components/calendar/calendar.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class CalendarComponent implements OnInit {
4444
@Output() changeDate: EventEmitter<any> = new EventEmitter<{
4545
date: Date;
4646
}>();
47+
@Output() changeView: EventEmitter<any> = new EventEmitter<{
48+
calendarView: CalendarView;
49+
}>();
4750

4851
initialDate: Date;
4952
previusDate: Date;
@@ -109,7 +112,7 @@ export class CalendarComponent implements OnInit {
109112
const date = this.currentDate;
110113
this.isToday = this.isVisibleForCurrentDate();
111114
this.navigationEnable(this.calendarView);
112-
this.changeDate.emit({ date, calendarView: this.calendarView });
115+
this.changeDate.emit({ date });
113116
}
114117

115118
changeCalendarView(calendarView: CalendarView) {
@@ -119,6 +122,7 @@ export class CalendarComponent implements OnInit {
119122
this.referenceChangeDetector.detectChanges();
120123
this.scrollToCurrentTimeMarker();
121124
}
125+
this.changeView.emit({ calendarView });
122126
}
123127

124128
navigationEnable(calendarView: CalendarView) {

src/app/modules/time-entries/pages/time-entries.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
[currentDate]="selectedDate.toDate()"
3535
[calendarView]="calendarView"
3636
(changeDate)="changeDate($event)"
37+
(changeView)="changeView($event)"
3738
(viewModal)="editEntry($event.id)"
3839
(deleteTimeEntry)="openModal($event.timeEntry)"
3940
>

src/app/modules/time-entries/pages/time-entries.component.spec.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ describe('TimeEntriesComponent', () => {
561561
const dateMoment: moment.Moment = moment().month(monthIndex).year(year);
562562
jasmine.clock().mockDate(dateMoment.toDate());
563563

564+
component.currentMonth = monthIndex;
564565
component.dateSelected(eventData);
565566

566567
expect(component.selectedDate).toEqual(dateMoment);
@@ -569,10 +570,8 @@ describe('TimeEntriesComponent', () => {
569570
it('set date in selectedDate when call changeDate and selectedDate.month() is same to incoming date', () => {
570571
const incomingDate = new Date('2021-06-07');
571572
const incomingMoment: moment.Moment = moment(incomingDate);
572-
const calendarView: CalendarView = CalendarView.Month;
573573
const eventData = {
574-
date: incomingDate,
575-
calendarView
574+
date: incomingDate
576575
};
577576
spyOn(component, 'dateSelected');
578577
component.selectedDate = moment(incomingMoment).subtract(1, 'day');
@@ -586,10 +585,8 @@ describe('TimeEntriesComponent', () => {
586585
it('call dateSelected when call changeDate and selectedDate.month() is different to incoming date', () => {
587586
const incomingDate = new Date('2021-01-07');
588587
const incomingMoment: moment.Moment = moment(incomingDate);
589-
const calendarView: CalendarView = CalendarView.Month;
590588
const eventData = {
591-
date: incomingDate,
592-
calendarView
589+
date: incomingDate
593590
};
594591
const selectedDate = {
595592
monthIndex: incomingMoment.month(),
@@ -603,6 +600,40 @@ describe('TimeEntriesComponent', () => {
603600
expect(component.dateSelected).toHaveBeenCalledWith(selectedDate);
604601
});
605602

603+
it('change component selectedDate to be the first day of the month when call dateSelected', () => {
604+
const actualMoment: moment.Moment = moment(new Date('2021-01-07'));
605+
const selectedMoment: moment.Moment = moment(new Date('2021-05-13'));
606+
const firstDayMoment: moment.Moment = selectedMoment.startOf('month');
607+
const eventDate = {
608+
monthIndex: selectedMoment.month(),
609+
year: selectedMoment.year()
610+
};
611+
component.currentMonth = actualMoment.month();
612+
component.selectedDate = selectedMoment;
613+
spyOn(component, 'dateSelected');
614+
component.dateSelected(eventDate);
615+
expect(component.selectedDate).toBe(firstDayMoment);
616+
});
617+
618+
it('change component calendarView from Month to Day when call changeView', () => {
619+
const fakeCalendarView: CalendarView = CalendarView.Day;
620+
const eventView = {
621+
calendarView: fakeCalendarView
622+
};
623+
component.calendarView = CalendarView.Month;
624+
component.changeView(eventView);
625+
expect(component.calendarView).toBe(fakeCalendarView);
626+
});
627+
628+
it('change component calendarView to Month if undefined when call changeView', () => {
629+
component.calendarView = CalendarView.Week;
630+
const eventView = {
631+
calendarView: undefined
632+
};
633+
component.changeView(eventView);
634+
expect(component.calendarView).toBe(CalendarView.Month);
635+
});
636+
606637
it('not view button onDisplayModeChange when isFeatureToggleCalendarActive is false', () => {
607638
component.isFeatureToggleCalendarActive = false;
608639

src/app/modules/time-entries/pages/time-entries.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
4040
selectedMonthAsText: string;
4141
isActiveEntryOverlapping = false;
4242
calendarView: CalendarView = CalendarView.Month;
43+
currentMonth = moment().month();
4344
readonly NO_DATA_MESSAGE: string = 'No data available in table';
4445
constructor(
4546
private store: Store<EntryState>,
@@ -182,9 +183,12 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
182183
this.selectedMonthAsText = moment().month(event.monthIndex).format('MMMM');
183184
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonth, this.selectedYear));
184185
this.selectedDate = moment().month(event.monthIndex).year(event.year);
186+
if (this.currentMonth !== event.monthIndex){
187+
this.selectedDate = this.selectedDate.startOf('month');
188+
}
185189
}
186190

187-
changeDate(event: { date: Date, calendarView: CalendarView }){
191+
changeDate(event: { date: Date }){
188192
const newDate: moment.Moment = moment(event.date);
189193
if (this.selectedDate.month() !== newDate.month()){
190194
const monthSelected = newDate.month();
@@ -196,7 +200,10 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
196200
this.dateSelected(selectedDate);
197201
}
198202
this.selectedDate = newDate;
199-
this.calendarView = event.calendarView;
203+
}
204+
205+
changeView(event: { calendarView: CalendarView }){
206+
this.calendarView = event.calendarView || CalendarView.Month;
200207
}
201208

202209
openModal(item: any) {

0 commit comments

Comments
 (0)