Skip to content

Commit 85a4c93

Browse files
committed
TT-85 fix: refactor code and improve tests
1 parent 29da434 commit 85a4c93

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

src/app/modules/shared/components/details-fields/details-fields.component.spec.ts

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ describe('DetailsFieldsComponent', () => {
7676
description: '',
7777
technology: '',
7878
};
79-
const dateTest = moment().format('YYYY-MM-DD');
80-
const endHourTest = moment().format('HH:mm:ss');
81-
const endDateTest = new Date(`${dateTest}T${endHourTest.trim()}`);
82-
const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
83-
const startDateTest = new Date(`${dateTest}T${startHourTest.trim()}`);
8479

8580
beforeEach(waitForAsync(() => {
8681
TestBed.configureTestingModule({
@@ -106,8 +101,8 @@ describe('DetailsFieldsComponent', () => {
106101
project_id: 'id',
107102
activity_id: '',
108103
uri: 'ticketUri',
109-
start_date: startDateTest,
110-
end_date: endDateTest,
104+
start_date: null,
105+
end_date: null,
111106
description: '',
112107
technologies: [],
113108
id: 'xyz'
@@ -218,6 +213,7 @@ describe('DetailsFieldsComponent', () => {
218213

219214
it('should emit saveEntry event', () => {
220215
spyOn(component.saveEntry, 'emit');
216+
component.entryToEdit = { ...entryToEdit };
221217
component.entryForm.setValue({
222218
project_id: 'p1',
223219
project_name: 'p-name',
@@ -299,6 +295,7 @@ describe('DetailsFieldsComponent', () => {
299295
component.goingToWorkOnThis = true;
300296
spyOn(component.saveEntry, 'emit');
301297

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

304301
component.onSubmit();
@@ -315,26 +312,71 @@ describe('DetailsFieldsComponent', () => {
315312
},
316313
shouldRestartEntry: false
317314
};
318-
319315
expect(component.saveEntry.emit).toHaveBeenCalledWith(data);
320316
});
321317

322-
it('onSubmit an entry without change hours, should not modify the start_date and end_date ', () => {
323-
component.entryToEdit = {...entryToEdit, description: 'test', };
318+
it('should not modify the start_date when start_hour has not been modified', () => {
319+
const dateTest = moment().format('YYYY-MM-DD');
320+
const startHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
321+
const expectedStartDate = new Date(`${dateTest}T${startHourTest.trim()}`);
322+
323+
component.entryToEdit = {...entryToEdit, start_date: expectedStartDate };
324324
fixture.componentInstance.ngOnChanges();
325325

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

329-
component.onSubmit();
328+
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
329+
});
330+
331+
it('should modify the start_date when start_hour has been modified', () => {
332+
const dateTest = moment().format('YYYY-MM-DD');
333+
const startHourTest = moment().format('HH:mm:ss');
334+
const startDate = new Date(`${dateTest}T${startHourTest.trim()}`);
335+
336+
component.entryToEdit = {...entryToEdit, start_date: startDate };
337+
fixture.componentInstance.ngOnChanges();
338+
339+
const updatedStartDate = moment().subtract(1, 'hours');
340+
const updatedStartHour = updatedStartDate.format('HH:mm');
341+
component.entryForm.patchValue({start_hour: updatedStartHour});
342+
343+
const expectedStartDate = moment(updatedStartDate).seconds(0).millisecond(0).toISOString();
344+
expect(component.startDateToSubmit()).toEqual(expectedStartDate);
345+
});
330346

331-
expect(component.starDateValue).toEqual(startHourValue);
332-
expect(component.endDateValue).toEqual(endHourValue);
347+
it('should not modify the end_date when end_hour has not been modified', () => {
348+
const dateTest = moment().format('YYYY-MM-DD');
349+
const endtHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
350+
const expectedEndDate = new Date(`${dateTest}T${endtHourTest.trim()}`);
351+
352+
component.entryToEdit = {...entryToEdit, end_date: expectedEndDate };
353+
fixture.componentInstance.ngOnChanges();
354+
355+
component.entryForm.patchValue({ description: 'test' });
356+
357+
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
333358
});
334359

360+
it('should modify the end_date when end_hour has been modified', () => {
361+
const dateTest = moment().format('YYYY-MM-DD');
362+
const endHourTest = moment().format('HH:mm:ss');
363+
const endDate = new Date(`${dateTest}T${endHourTest.trim()}`);
364+
365+
component.entryToEdit = {...entryToEdit, end_date: endDate };
366+
fixture.componentInstance.ngOnChanges();
367+
368+
const updatedEndDate = moment().subtract(1, 'hours');
369+
const updatedEndHour = updatedEndDate.format('HH:mm');
370+
component.entryForm.patchValue({end_hour: updatedEndHour});
371+
372+
const expectedEndDate = moment(updatedEndDate).seconds(0).millisecond(0).toISOString();
373+
expect(component.endDateToSubmit()).toEqual(expectedEndDate);
374+
});
375+
335376
it('displays error message when the date selected is in the future', () => {
336377
spyOn(toastrServiceStub, 'error');
337378

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

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

402+
component.entryToEdit = { ...entryToEdit };
359403
const futureDate = moment().add(1, 'days').format(DATE_FORMAT_YEAR);
360404
const currentDate = moment().format(DATE_FORMAT_YEAR);
361405
component.entryForm.setValue({ ...formValues, start_date: currentDate, end_date: futureDate });

src/app/modules/shared/components/details-fields/details-fields.component.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,6 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
219219
return result;
220220
}
221221

222-
timeEntryIsInTheFuture(){
223-
const startDate = this.entryForm.value.start_date;
224-
const endDate = this.entryForm.value.end_date;
225-
const isStartDateInTheFuture = moment(startDate).isAfter(moment());
226-
const isEndDateInTheFuture = moment(endDate).isAfter(moment());
227-
return isStartDateInTheFuture || isEndDateInTheFuture;
228-
}
229-
230222
onSubmit() {
231223
if (this.entryForm.invalid) {
232224
this.toastrService.warning('Make sure to select a project and activity');
@@ -250,7 +242,11 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
250242
delete entry.end_date;
251243
}
252244

253-
if (this.timeEntryIsInTheFuture()) {
245+
const isStartDateInTheFuture = moment(startDateToSubmit).isAfter(moment());
246+
const isEndDateInTheFuture = moment(endDateToSubmit).isAfter(moment());
247+
const timeEntryIsInTheFuture = isStartDateInTheFuture || isEndDateInTheFuture;
248+
249+
if (timeEntryIsInTheFuture) {
254250
this.toastrService.error('You cannot start a time-entry in the future');
255251
return;
256252
}

0 commit comments

Comments
 (0)