Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
TT-85 fix: refactor code of time fields
  • Loading branch information
PaulRC-ioet authored and josepato87 committed Dec 28, 2020
commit 1424d506e70137723d7c0e48d2b23c78dae852b8
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down Expand Up @@ -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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an opportunity to improve our coverage, testing this line according to Codecov. :)

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;
}
Expand Down