Skip to content

Commit d937d24

Browse files
authored
fix: TT-311 Can not update entries fixed (#722)
* fix: TT-311 Can not update entries fixed * code-smell: TT-311 Code smells suggested by Sonarcloud fixed * fix: TT-311 Fix tests that fails on different timezone * fix: TT-311 Spelling error on method fixed
1 parent 3b8af2f commit d937d24

File tree

2 files changed

+104
-26
lines changed

2 files changed

+104
-26
lines changed

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

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ describe('DetailsFieldsComponent', () => {
141141
uri: 'ticketUri',
142142
start_date: '',
143143
end_date: '',
144-
start_hour: '00:00:10',
145-
end_hour: '00:00:11',
144+
start_hour: '00:00',
145+
end_hour: '00:00',
146146
description: '',
147147
technology: '',
148148
};
@@ -176,22 +176,23 @@ describe('DetailsFieldsComponent', () => {
176176
expect(component.saveEntry.emit).toHaveBeenCalledTimes(0);
177177
});
178178

179-
[{ actionType: EntryActionTypes.CREATE_ENTRY_SUCCESS }, { actionType: EntryActionTypes.UPDATE_ENTRY_SUCCESS }].forEach(
180-
(param) => {
181-
it(`cleanForm after an action type ${param.actionType} is received`, () => {
182-
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
183-
const action = {
184-
type: param.actionType,
185-
};
186-
spyOn(component, 'cleanForm');
179+
[
180+
{ actionType: EntryActionTypes.CREATE_ENTRY_SUCCESS },
181+
{ actionType: EntryActionTypes.UPDATE_ENTRY_SUCCESS },
182+
].forEach((param) => {
183+
it(`cleanForm after an action type ${param.actionType} is received`, () => {
184+
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
185+
const action = {
186+
type: param.actionType,
187+
};
188+
spyOn(component, 'cleanForm');
187189

188-
component.ngOnInit();
189-
actionSubject.next(action);
190+
component.ngOnInit();
191+
actionSubject.next(action);
190192

191-
expect(component.cleanForm).toHaveBeenCalled();
192-
});
193-
}
194-
);
193+
expect(component.cleanForm).toHaveBeenCalled();
194+
});
195+
});
195196

196197
it('on cleanFieldsForm the project_id and project_name should be kept', () => {
197198
const entryFormValueExpected = {
@@ -295,8 +296,8 @@ describe('DetailsFieldsComponent', () => {
295296
uri: '',
296297
start_date: '2020-02-05',
297298
end_date: '2020-02-05',
298-
start_hour: '00:00:01',
299-
end_hour: '00:01:01',
299+
start_hour: '00:00',
300+
end_hour: '00:01',
300301
description: '',
301302
technology: '',
302303
});
@@ -309,8 +310,8 @@ describe('DetailsFieldsComponent', () => {
309310
activity_id: 'a1',
310311
technologies: [],
311312
description: '',
312-
start_date: new Date('2020-02-05T00:00:01').toISOString(),
313-
end_date: new Date('2020-02-05T00:01:01').toISOString(),
313+
start_date: new Date('2020-02-05T00:00:00').toISOString(),
314+
end_date: new Date('2020-02-05T00:01:00').toISOString(),
314315
uri: '',
315316
timezone_offset: new Date().getTimezoneOffset(),
316317
},
@@ -408,7 +409,7 @@ describe('DetailsFieldsComponent', () => {
408409
activity_id: 'a1',
409410
technologies: [],
410411
description: '',
411-
start_date: new Date('2020-06-11T00:00:10').toISOString(),
412+
start_date: new Date('2020-06-11T00:00:00').toISOString(),
412413
uri: 'ticketUri',
413414
timezone_offset: new Date().getTimezoneOffset(),
414415
},
@@ -652,6 +653,58 @@ describe('DetailsFieldsComponent', () => {
652653
componentToTest.onSubmit();
653654
expect(toastrServiceStub.error).not.toHaveBeenCalled();
654655
});
656+
657+
it('Should return a date in ISO format given a date, hour, minute, second and milisecond', () => {
658+
const fakeDates = [
659+
{
660+
date: '2021-04-20',
661+
hourAndMinutes: '10:00',
662+
seconds: 20,
663+
miliseconds: 100,
664+
},
665+
{
666+
date: '2020-09-21',
667+
hourAndMinutes: '08:00',
668+
seconds: 10,
669+
miliseconds: 100,
670+
},
671+
{
672+
date: '2021-04-15',
673+
hourAndMinutes: '23:00',
674+
seconds: 0,
675+
miliseconds: 0,
676+
},
677+
{
678+
date: '2019-03-29',
679+
hourAndMinutes: '12:00',
680+
seconds: 30,
681+
miliseconds: 400,
682+
},
683+
];
684+
685+
const expectedISODates = [
686+
new Date('2021-04-20T10:00:20.100').toISOString(),
687+
new Date('2020-09-21T08:00:10.100').toISOString(),
688+
new Date('2021-04-15T23:00:00.000').toISOString(),
689+
new Date('2019-03-29T12:00:30.400').toISOString(),
690+
];
691+
692+
fakeDates.forEach(({ date, hourAndMinutes, seconds, miliseconds }, fakeDateIndex) => {
693+
const dateInISOFormat = component.getDateISOFormat(date, hourAndMinutes, seconds, miliseconds);
694+
expect(dateInISOFormat).toBe(expectedISODates[fakeDateIndex]);
695+
});
696+
});
697+
698+
it('should return a number in ISO format given a normal number', () => {
699+
const numbersForTest = [1, 2, 3, 4, 20, 30, 40, 32, 45];
700+
701+
const expectedISOFormatNumbers = ['01', '02', '03', '04', '20', '30', '40', '32', '45'];
702+
703+
numbersForTest.forEach((currentNumber, numberIndex) => {
704+
const numberinISOFormat = component.getNumberInISOFormat(currentNumber);
705+
expect(numberinISOFormat).toBe(expectedISOFormatNumbers[numberIndex]);
706+
});
707+
});
655708
/*
656709
TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component,
657710
and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here:

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,37 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
264264
return startDate >= endDate;
265265
}
266266

267-
dateToSubmit(date, hour) {
268-
const entryFormDate = this.entryForm.value[date];
269-
const updatedHour = this.entryForm.value[hour];
270-
const updatedDate = new Date(`${entryFormDate}T${updatedHour.trim()}`).toISOString();
267+
getDateISOFormat(date: string, hourAndMinutes: string, seconds: number, miliseconds: number): string {
268+
hourAndMinutes = hourAndMinutes.trim();
269+
const secondsInISOFormat: string = this.getNumberInISOFormat(seconds);
270+
const milisecondsInISOFormat: string = this.getNumberInISOFormat(miliseconds);
271+
const ISOFormat = `${date}T${hourAndMinutes}:${secondsInISOFormat}.${milisecondsInISOFormat}`;
272+
return new Date(ISOFormat).toISOString();
273+
}
274+
275+
getNumberInISOFormat(numberToFormat: number): string {
276+
const limitSingleNumber = 9;
277+
const isNumberGreaterThanLimitNumber = numberToFormat > limitSingleNumber;
278+
return isNumberGreaterThanLimitNumber ? numberToFormat.toString() : `0${numberToFormat}`;
279+
}
280+
281+
dateToSubmit(date: string, hour: string) {
282+
const timeEntryISODate: string = get(this.entryToEdit, date);
283+
let seconds = 0;
284+
let miliseconds = 0;
285+
286+
if (timeEntryISODate) {
287+
const timeEntryDate: Date = new Date(timeEntryISODate);
288+
seconds = timeEntryDate.getSeconds();
289+
miliseconds = timeEntryDate.getMilliseconds();
290+
}
291+
292+
const newEntryDate: string = this.entryForm.value[date];
293+
const newEntryHour: string = this.entryForm.value[hour];
294+
295+
const updatedDate: string = this.getDateISOFormat(newEntryDate, newEntryHour, seconds, miliseconds);
271296
const initialDate = get(this.entryToEdit, date, updatedDate);
272-
const dateHasNotChanged = (initialDate === updatedDate);
297+
const dateHasNotChanged = initialDate === updatedDate;
273298
const result = dateHasNotChanged ? initialDate : updatedDate;
274299
return result;
275300
}

0 commit comments

Comments
 (0)