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
Next Next commit
TT-85 fix: refactor code and improve tests
  • Loading branch information
PaulRC-ioet committed Dec 22, 2020
commit 85a4c931855d30da05b2d8db1c514b857cf6af26
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ describe('DetailsFieldsComponent', () => {
description: '',
technology: '',
};
const dateTest = moment().format('YYYY-MM-DD');
const endHourTest = moment().format('HH:mm:ss');
const endDateTest = new Date(`${dateTest}T${endHourTest.trim()}`);
const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
const startDateTest = new Date(`${dateTest}T${startHourTest.trim()}`);

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -106,8 +101,8 @@ describe('DetailsFieldsComponent', () => {
project_id: 'id',
activity_id: '',
uri: 'ticketUri',
start_date: startDateTest,
end_date: endDateTest,
start_date: null,
end_date: null,
description: '',
technologies: [],
id: 'xyz'
Expand Down Expand Up @@ -218,6 +213,7 @@ describe('DetailsFieldsComponent', () => {

it('should emit saveEntry event', () => {
spyOn(component.saveEntry, 'emit');
component.entryToEdit = { ...entryToEdit };
component.entryForm.setValue({
project_id: 'p1',
project_name: 'p-name',
Expand Down Expand Up @@ -299,6 +295,7 @@ describe('DetailsFieldsComponent', () => {
component.goingToWorkOnThis = true;
spyOn(component.saveEntry, 'emit');

component.entryToEdit = { ...entryToEdit };
component.entryForm.setValue({ ...formValues, start_date: '2020-06-11', end_date: '2020-06-11' });

component.onSubmit();
Expand All @@ -315,26 +312,71 @@ describe('DetailsFieldsComponent', () => {
},
shouldRestartEntry: false
};

expect(component.saveEntry.emit).toHaveBeenCalledWith(data);
});

it('onSubmit an entry without change hours, should not modify the start_date and end_date ', () => {
component.entryToEdit = {...entryToEdit, description: 'test', };
it('should not modify the start_date when start_hour has not been modified', () => {
const dateTest = moment().format('YYYY-MM-DD');
Copy link
Collaborator

Choose a reason for hiding this comment

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

change:
dateTest to currentDate
startHourTest to startHour

I think is necessary use other name of variable per example dateTest to dateCurrent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok it's changed

const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
const expectedStartDate = new Date(`${dateTest}T${startHourTest.trim()}`);

component.entryToEdit = {...entryToEdit, start_date: expectedStartDate };
fixture.componentInstance.ngOnChanges();

const startHourValue = moment().subtract(3, 'hours').format('HH:mm');
const endHourValue = moment().format('HH:mm');
component.entryForm.patchValue({ description: 'test' });

component.onSubmit();
expect(component.startDateToSubmit()).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()}`);

component.entryToEdit = {...entryToEdit, start_date: startDate };
fixture.componentInstance.ngOnChanges();

const updatedStartDate = moment().subtract(1, 'hours');
const updatedStartHour = updatedStartDate.format('HH:mm');
component.entryForm.patchValue({start_hour: updatedStartHour});

const expectedStartDate = moment(updatedStartDate).seconds(0).millisecond(0).toISOString();
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
});

expect(component.starDateValue).toEqual(startHourValue);
expect(component.endDateValue).toEqual(endHourValue);
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()}`);

component.entryToEdit = {...entryToEdit, end_date: expectedEndDate };
fixture.componentInstance.ngOnChanges();

component.entryForm.patchValue({ description: 'test' });

expect(component.endDateToSubmit()).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()}`);

component.entryToEdit = {...entryToEdit, end_date: endDate };
fixture.componentInstance.ngOnChanges();

const updatedEndDate = moment().subtract(1, 'hours');
const updatedEndHour = updatedEndDate.format('HH:mm');
component.entryForm.patchValue({end_hour: updatedEndHour});

const expectedEndDate = moment(updatedEndDate).seconds(0).millisecond(0).toISOString();
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
});

it('displays error message when the date selected is in the future', () => {
spyOn(toastrServiceStub, 'error');

component.entryToEdit = { ...entryToEdit };
const futureDate = moment().add(1, 'days').format(DATE_FORMAT_YEAR);
component.entryForm.setValue({ ...formValues, start_date: futureDate, end_date: futureDate });
component.onSubmit();
Expand All @@ -345,6 +387,7 @@ describe('DetailsFieldsComponent', () => {
it('when start_date is in the future and end_date is OK then throws an error', () => {
spyOn(toastrServiceStub, 'error');

component.entryToEdit = { ...entryToEdit };
const futureDate = moment().add(1, 'days').format(DATE_FORMAT_YEAR);
const currentDate = moment().format(DATE_FORMAT_YEAR);
component.entryForm.setValue({ ...formValues, start_date: futureDate, end_date: currentDate });
Expand All @@ -356,6 +399,7 @@ describe('DetailsFieldsComponent', () => {
it('when start_date is OK and end_date is in the future then throws an error future', () => {
spyOn(toastrServiceStub, 'error');

component.entryToEdit = { ...entryToEdit };
const futureDate = moment().add(1, 'days').format(DATE_FORMAT_YEAR);
const currentDate = moment().format(DATE_FORMAT_YEAR);
component.entryForm.setValue({ ...formValues, start_date: currentDate, end_date: futureDate });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
return result;
}

timeEntryIsInTheFuture(){
const startDate = this.entryForm.value.start_date;
const endDate = this.entryForm.value.end_date;
const isStartDateInTheFuture = moment(startDate).isAfter(moment());
const isEndDateInTheFuture = moment(endDate).isAfter(moment());
return isStartDateInTheFuture || isEndDateInTheFuture;
}

onSubmit() {
if (this.entryForm.invalid) {
this.toastrService.warning('Make sure to select a project and activity');
Expand All @@ -250,7 +242,11 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
delete entry.end_date;
}

if (this.timeEntryIsInTheFuture()) {
const isStartDateInTheFuture = moment(startDateToSubmit).isAfter(moment());
const isEndDateInTheFuture = moment(endDateToSubmit).isAfter(moment());
const timeEntryIsInTheFuture = isStartDateInTheFuture || isEndDateInTheFuture;

if (timeEntryIsInTheFuture) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I remember we made a change to extract timeEntryIsInTheFuture into a function. I guess that caused issues in existing tests and you ended up doing it like this. Am I right, or is it an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just tried the function of not allowing a "time entry" in the future and it allowed me to add. It ignores the way we put it so I left it as it was. but with the tests, there was no problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok. Then, keep in mind that, when we may want to retake that little refactor.

this.toastrService.error('You cannot start a time-entry in the future');
return;
}
Expand Down