diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts index 5d0e45571..9e39ed8e9 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts @@ -245,19 +245,25 @@ describe('DetailsFieldsComponent', () => { expect(component.saveEntry.emit).toHaveBeenCalledWith(data); }); - it('when the current entry is not running, then the end hour input should be rendered', () => { + it('when the current entry is not running, then the end date and end hour inputs should be rendered', () => { component.goingToWorkOnThis = false; fixture.detectChanges(); + const endDateInput = fixture.debugElement.nativeElement.querySelector('#end_date'); const endHourInput = fixture.debugElement.nativeElement.querySelector('#end_hour'); + + expect(endDateInput).toBeDefined(); expect(endHourInput).toBeDefined(); }); - it('when the current entry is running, then the end hour input should not be rendered', () => { + it('when the current entry is running, then the end date and end hour inputs should not be rendered', () => { component.goingToWorkOnThis = true; fixture.detectChanges(); + const endDateInput = fixture.debugElement.nativeElement.querySelector('#end_date'); const endHourInput = fixture.debugElement.nativeElement.querySelector('#end_hour'); + + expect(endDateInput).toBeNull(); expect(endHourInput).toBeNull(); }); @@ -316,22 +322,22 @@ describe('DetailsFieldsComponent', () => { }); it('should not modify the start_date when start_hour has not been modified', () => { - const dateTest = moment().format('YYYY-MM-DD'); - const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss'); - const expectedStartDate = new Date(`${dateTest}T${startHourTest.trim()}`); + const currentDate = moment().format('YYYY-MM-DD'); + const startHour = moment().subtract(3, 'hours').format('HH:mm:ss'); + const expectedStartDate = new Date(`${currentDate}T${startHour.trim()}`); component.entryToEdit = {...entryToEdit, start_date: expectedStartDate }; fixture.componentInstance.ngOnChanges(); component.entryForm.patchValue({ description: 'test' }); - expect(component.startDateToSubmit()).toEqual(expectedStartDate); + expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate); }); it('should modify the start_date when start_hour has been modified', () => { - const dateTest = moment().format('YYYY-MM-DD'); - const startHourTest = moment().format('HH:mm:ss'); - const startDate = new Date(`${dateTest}T${startHourTest.trim()}`); + const currentDate = moment().format('YYYY-MM-DD'); + const startHour = moment().format('HH:mm:ss'); + const startDate = new Date(`${currentDate}T${startHour.trim()}`); component.entryToEdit = {...entryToEdit, start_date: startDate }; fixture.componentInstance.ngOnChanges(); @@ -341,26 +347,26 @@ describe('DetailsFieldsComponent', () => { component.entryForm.patchValue({start_hour: updatedStartHour}); const expectedStartDate = moment(updatedStartDate).seconds(0).millisecond(0).toISOString(); - expect(component.startDateToSubmit()).toEqual(expectedStartDate); + expect(component.dateToSubmit('start_date', 'start_hour')).toEqual(expectedStartDate); }); it('should not modify the end_date when end_hour has not been modified', () => { - const dateTest = moment().format('YYYY-MM-DD'); - const endtHourTest = moment().subtract(3, 'hours').format('HH:mm:ss'); - const expectedEndDate = new Date(`${dateTest}T${endtHourTest.trim()}`); + const currentDate = moment().format('YYYY-MM-DD'); + const endHour = moment().subtract(3, 'hours').format('HH:mm:ss'); + const expectedEndDate = new Date(`${currentDate}T${endHour.trim()}`); component.entryToEdit = {...entryToEdit, end_date: expectedEndDate }; fixture.componentInstance.ngOnChanges(); component.entryForm.patchValue({ description: 'test' }); - expect(component.endDateToSubmit()).toEqual(expectedEndDate); + expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate); }); it('should modify the end_date when end_hour has been modified', () => { - const dateTest = moment().format('YYYY-MM-DD'); - const endHourTest = moment().format('HH:mm:ss'); - const endDate = new Date(`${dateTest}T${endHourTest.trim()}`); + const currentDate = moment().format('YYYY-MM-DD'); + const endHour = moment().format('HH:mm:ss'); + const endDate = new Date(`${currentDate}T${endHour.trim()}`); component.entryToEdit = {...entryToEdit, end_date: endDate }; fixture.componentInstance.ngOnChanges(); @@ -370,7 +376,7 @@ describe('DetailsFieldsComponent', () => { component.entryForm.patchValue({end_hour: updatedEndHour}); const expectedEndDate = moment(updatedEndDate).seconds(0).millisecond(0).toISOString(); - expect(component.endDateToSubmit()).toEqual(expectedEndDate); + expect(component.dateToSubmit('end_date', 'end_hour')).toEqual(expectedEndDate); }); it('displays error message when the date selected is in the future', () => { diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.ts b/src/app/modules/shared/components/details-fields/details-fields.component.ts index 516395b74..13b4b9d32 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.ts @@ -133,7 +133,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { activity_id: this.entryToEdit.activity_id, description: this.entryToEdit.description, start_date: formatDate(get(this.entryToEdit, 'start_date', ''), DATE_FORMAT, 'en'), - end_date: formatDate(get(this.entryToEdit, 'end_date'), DATE_FORMAT, 'en'), + end_date: formatDate(get(this.entryToEdit, 'end_date', ''), DATE_FORMAT, 'en'), start_hour: formatDate(get(this.entryToEdit, 'start_date', '00:00'), 'HH:mm', 'en'), end_hour: formatDate(get(this.entryToEdit, 'end_date', '00:00'), 'HH:mm', 'en'), uri: this.entryToEdit.uri, @@ -197,25 +197,14 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { this.closeModal?.nativeElement?.click(); } - startDateToSubmit(){ - const startDate = this.entryForm.value.start_date; - const initialStartDate = this.entryToEdit.start_date; - const updatedStartDate = new Date(`${startDate}T${this.entryForm.value.start_hour.trim()}`).toISOString(); - const initialStartHour = formatDate(get(this.entryToEdit, 'start_date', '00:00'), 'HH:mm', 'en'); - const updatedStartHour = this.entryForm.value.start_hour; - const startHourHasNotChanged = updatedStartHour === initialStartHour; - const result = startHourHasNotChanged ? initialStartDate : updatedStartDate; - return result; - } - - endDateToSubmit(){ - const endDate = this.entryForm.value.end_date; - const initialEndDate = this.entryToEdit.end_date; - const updatedEndDate = new Date(`${endDate}T${this.entryForm.value.end_hour.trim()}`).toISOString(); - const initialEndHour = formatDate(get(this.entryToEdit, 'end_date', '00:00'), 'HH:mm', 'en'); - const updatedEndHour = this.entryForm.value.end_hour; - const endDateHasNotChanged = updatedEndHour === initialEndHour; - const result = endDateHasNotChanged ? initialEndDate : updatedEndDate; + dateToSubmit(date, hour){ + const entryFormDate = this.entryForm.value[date]; + const updatedHour = this.entryForm.value[hour]; + const initialDate = this.entryToEdit[date]; + const updatedDate = new Date(`${entryFormDate}T${updatedHour.trim()}`).toISOString(); + const initialHour = formatDate(get(this.entryToEdit, date, '00:00'), 'HH:mm', 'en'); + const dateHasNotChanged = updatedHour === initialHour; + const result = dateHasNotChanged ? initialDate : updatedDate; return result; } @@ -225,8 +214,8 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { return; } - const startDateToSubmit = this.startDateToSubmit(); - const endDateToSubmit = this.endDateToSubmit(); + const startDateToSubmit = this.dateToSubmit('start_date', 'start_hour'); + const endDateToSubmit = this.dateToSubmit('end_date', 'end_hour'); const entry = { project_id: this.entryForm.value.project_id, @@ -257,7 +246,10 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { onGoingToWorkOnThisChange(event: any) { this.goingToWorkOnThis = event.currentTarget.checked; if (!this.goingToWorkOnThis) { - this.entryForm.patchValue({ end_hour: formatDate(new Date(), 'HH:mm:ss', 'en') }); + this.entryForm.patchValue({ + end_date: formatDate(get(this.entryToEdit, 'start_date', ''), DATE_FORMAT, 'en'), + end_hour: formatDate(get(this.entryToEdit, 'start_date', '00:00'), 'HH:mm', 'en') + }); } this.shouldRestartEntry = !this.entryToEdit?.running && this.goingToWorkOnThis; }