Skip to content

Commit 2f675b2

Browse files
committed
TT-103 test: Fix moment flaky tests
1 parent 2dab504 commit 2f675b2

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ describe('DetailsFieldsComponent', () => {
7777
technology: '',
7878
};
7979

80+
const mockCurrentDate = '2020-12-01T12:00:00';
81+
8082
beforeEach(waitForAsync(() => {
8183
TestBed.configureTestingModule({
8284
declarations: [DetailsFieldsComponent, TechnologiesComponent],
@@ -343,14 +345,12 @@ describe('DetailsFieldsComponent', () => {
343345
});
344346

345347
it('should modify the start_date when start_hour has been modified', () => {
346-
const currentDate = moment().format('YYYY-MM-DD');
347-
const startHour = moment().format('HH:mm:ss');
348-
const startDate = new Date(`${currentDate}T${startHour.trim()}`);
348+
const startDate = new Date(mockCurrentDate);
349349

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

353-
const updatedStartDate = moment().subtract(1, 'hours');
353+
const updatedStartDate = moment(startDate).subtract(1, 'hours');
354354
const updatedStartHour = updatedStartDate.format('HH:mm');
355355
component.entryForm.patchValue({start_hour: updatedStartHour});
356356

@@ -372,14 +372,12 @@ describe('DetailsFieldsComponent', () => {
372372
});
373373

374374
it('should modify the end_date when end_hour has been modified', () => {
375-
const currentDate = moment().format('YYYY-MM-DD');
376-
const endHour = moment().format('HH:mm:ss');
377-
const endDate = new Date(`${currentDate}T${endHour.trim()}`);
375+
const endDate = new Date(mockCurrentDate);
378376

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

382-
const updatedEndDate = moment().subtract(1, 'hours');
380+
const updatedEndDate = moment(endDate).subtract(1, 'hours');
383381
const updatedEndHour = updatedEndDate.format('HH:mm');
384382
component.entryForm.patchValue({end_hour: updatedEndHour});
385383

src/app/modules/shared/components/month-picker/month-picker.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('MonthPickerComponent', () => {
3030
const year = new Date().getFullYear();
3131

3232
spyOn(component.dateSelected, 'emit');
33-
expect(component.dateSelected.emit({ monthIndex: month, year: year }));
33+
expect(component.dateSelected.emit({ monthIndex: month, year }));
3434
});
3535

3636

src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ describe('EntryFieldsComponent', () => {
2929
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { },
3030
warning: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
3131
};
32-
const lastDate = moment().format('YYYY-MM-DD');
33-
const startHourTest = moment().subtract(5, 'hours').format('HH:mm:ss');
34-
const endHourTest = moment().subtract(3, 'hours').format('HH:mm:ss');
32+
const mockDate = '2020-12-01T12:00:00';
33+
const lastDate = moment(mockDate).format(DATE_FORMAT_YEAR);
34+
const startHourTest = moment(mockDate).subtract(5, 'hours').format('HH:mm:ss');
35+
const endHourTest = moment(mockDate).subtract(3, 'hours').format('HH:mm:ss');
3536
const lastStartHourEntryEntered = new Date(`${lastDate}T${startHourTest.trim()}`).toISOString();
3637
const lastEndHourEntryEntered = new Date(`${lastDate}T${endHourTest.trim()}`).toISOString();
3738

@@ -95,8 +96,8 @@ describe('EntryFieldsComponent', () => {
9596
project_id: 'project-id-15',
9697
description: 'description for active entry',
9798
uri: 'abc',
98-
start_date : moment().format(DATE_FORMAT_YEAR),
99-
start_hour : moment().format('HH:mm'),
99+
start_date : moment(mockDate).format(DATE_FORMAT_YEAR),
100+
start_hour : moment(mockDate).format('HH:mm'),
100101
};
101102

102103
beforeEach(waitForAsync(() => {
@@ -144,16 +145,20 @@ describe('EntryFieldsComponent', () => {
144145
uri: entryDataForm.uri,
145146
activity_id: entryDataForm.activity_id,
146147
start_hour: formatDate(entry.start_date, 'HH:mm', 'en'),
147-
start_date : moment().format(DATE_FORMAT_YEAR),
148+
start_date : moment(mockDate).format(DATE_FORMAT_YEAR),
148149
}
149150
);
150151
expect(component.selectedTechnologies).toEqual([]);
151152
});
152153

153154
it('displays error message when the date selected is in the future', () => {
154-
component.newData = entry;
155-
component.activeEntry = entry ;
156-
component.setDataToUpdate(entry);
155+
const mockEntry = { ...entry,
156+
start_date : moment().format(DATE_FORMAT_YEAR),
157+
start_hour : moment().format('HH:mm')
158+
};
159+
component.newData = mockEntry;
160+
component.activeEntry = mockEntry ;
161+
component.setDataToUpdate(mockEntry);
157162
spyOn(toastrServiceStub, 'error');
158163

159164
const hourInTheFuture = moment().add(1, 'hours').format('HH:mm');
@@ -170,7 +175,7 @@ describe('EntryFieldsComponent', () => {
170175
component.setDataToUpdate(entry);
171176
spyOn(toastrServiceStub, 'error');
172177

173-
const hourInThePast = moment().subtract(6, 'hour').format('HH:mm');
178+
const hourInThePast = moment(mockDate).subtract(6, 'hour').format('HH:mm');
174179
component.entryForm.patchValue({ start_hour : hourInThePast});
175180
component.onUpdateStartHour();
176181

@@ -188,7 +193,7 @@ describe('EntryFieldsComponent', () => {
188193
component.newData = entry;
189194
component.activeEntry = entry ;
190195
component.setDataToUpdate(entry);
191-
const updatedTime = moment().format('HH:mm');
196+
const updatedTime = moment(mockDate).format('HH:mm');
192197
component.entryForm.patchValue({ start_hour : updatedTime});
193198
spyOn(component.entryForm, 'patchValue');
194199
component.cancelTimeInUpdate();
@@ -206,7 +211,7 @@ describe('EntryFieldsComponent', () => {
206211
component.activeEntry = entry ;
207212
component.setDataToUpdate(entry);
208213

209-
const updatedTime = moment().subtract(6, 'hours').format('HH:mm');
214+
const updatedTime = moment(mockDate).subtract(6, 'hours').format('HH:mm');
210215
component.entryForm.patchValue({ start_hour : updatedTime});
211216

212217
spyOn(component.entryForm, 'patchValue');
@@ -221,9 +226,13 @@ describe('EntryFieldsComponent', () => {
221226
});
222227

223228
it('If start hour is in the future, reset to initial start_date in form', () => {
224-
component.newData = entry;
225-
component.activeEntry = entry ;
226-
component.setDataToUpdate(entry);
229+
const mockEntry = { ...entry,
230+
start_date : moment().format(DATE_FORMAT_YEAR),
231+
start_hour : moment().format('HH:mm')
232+
};
233+
component.newData = mockEntry;
234+
component.activeEntry = mockEntry;
235+
component.setDataToUpdate(mockEntry);
227236

228237
const hourInTheFuture = moment().add(1, 'hours').format('HH:mm');
229238
component.entryForm.patchValue({ start_hour : hourInTheFuture});
@@ -240,9 +249,10 @@ describe('EntryFieldsComponent', () => {
240249
});
241250

242251
it('when a start hour is updated, then dispatch UpdateActiveEntry', () => {
243-
component.activeEntry = entry ;
252+
component.activeEntry = entry;
244253
component.setDataToUpdate(entry);
245-
const updatedTime = moment().format('HH:mm');
254+
const updatedTime = moment(mockDate).format('HH:mm');
255+
246256
component.entryForm.patchValue({ start_hour : updatedTime});
247257
spyOn(store, 'dispatch');
248258

@@ -252,9 +262,9 @@ describe('EntryFieldsComponent', () => {
252262
});
253263

254264
it('When start_time is updated, component.last_entry is equal to time entry in the position 1', waitForAsync(() => {
255-
component.activeEntry = entry ;
265+
component.activeEntry = entry;
256266
component.setDataToUpdate(entry);
257-
const updatedTime = moment().format('HH:mm');
267+
const updatedTime = moment(mockDate).format('HH:mm');
258268

259269
component.entryForm.patchValue({ start_hour : updatedTime});
260270
component.onUpdateStartHour();
@@ -263,9 +273,9 @@ describe('EntryFieldsComponent', () => {
263273
}));
264274

265275
it('When start_time is updated for a time entry. UpdateCurrentOrLastEntry action is dispatched', () => {
266-
component.activeEntry = entry ;
276+
component.activeEntry = entry;
267277
component.setDataToUpdate(entry);
268-
const updatedTime = moment().subtract(4, 'hours').format('HH:mm');
278+
const updatedTime = moment(mockDate).subtract(4, 'hours').format('HH:mm');
269279
component.entryForm.patchValue({ start_hour : updatedTime});
270280
spyOn(store, 'dispatch');
271281

src/app/modules/time-entries/pages/time-entries.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ describe('TimeEntriesComponent', () => {
327327
const year = new Date().getFullYear();
328328

329329
spyOn(store, 'dispatch');
330-
component.dateSelected({monthIndex: month, year: year});
330+
component.dateSelected({ monthIndex: month, year });
331331
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.LoadEntries(month + 1, year));
332332
});
333333

0 commit comments

Comments
 (0)