Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
TT-85 fix: refactor code of time fields
  • Loading branch information
PaulRC-ioet committed Dec 23, 2020
commit 1685b88bfca0c7f7c73c7c95f5e316e39e053c0a
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,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 +341,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 +370,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 @@ -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