Skip to content

Commit 04c2bd0

Browse files
authored
refactor(material/timepicker): switch tests away from fakeAsync (#33266)
Reworks the timepicker tests to no longer use `fakeAsync`.
1 parent 50e281a commit 04c2bd0

1 file changed

Lines changed: 48 additions & 49 deletions

File tree

src/material/timepicker/timepicker.spec.ts

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ViewEncapsulation,
88
ChangeDetectionStrategy,
99
} from '@angular/core';
10-
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
10+
import {ComponentFixture, TestBed} from '@angular/core/testing';
1111
import {DateAdapter, MATERIAL_ANIMATIONS, provideNativeDateAdapter} from '../core';
1212
import {
1313
clearElement,
@@ -45,7 +45,7 @@ describe('MatTimepicker', () => {
4545
beforeEach(() => configureTestingModule());
4646

4747
describe('value selection', () => {
48-
it('should only change the time part of the selected date', fakeAsync(() => {
48+
it('should only change the time part of the selected date', async () => {
4949
const fixture = TestBed.createComponent(StandaloneTimepicker);
5050
fixture.componentInstance.value.set(new Date(2024, 0, 15, 0, 0, 0));
5151
fixture.detectChanges();
@@ -54,7 +54,7 @@ describe('MatTimepicker', () => {
5454

5555
getOptions()[3].click();
5656
fixture.detectChanges();
57-
flush();
57+
await fixture.whenStable();
5858

5959
const value = fixture.componentInstance.input.value()!;
6060
expect(value).toBeTruthy();
@@ -64,9 +64,9 @@ describe('MatTimepicker', () => {
6464
expect(adapter.getHours(value)).toBe(1);
6565
expect(adapter.getMinutes(value)).toBe(30);
6666
expect(adapter.getSeconds(value)).toBe(0);
67-
}));
67+
});
6868

69-
it('should accept the selected value and close the panel when clicking an option', fakeAsync(() => {
69+
it('should accept the selected value and close the panel when clicking an option', async () => {
7070
const fixture = TestBed.createComponent(StandaloneTimepicker);
7171
const input = getInput(fixture);
7272
fixture.detectChanges();
@@ -79,7 +79,7 @@ describe('MatTimepicker', () => {
7979

8080
getOptions()[1].click();
8181
fixture.detectChanges();
82-
flush();
82+
await fixture.whenStable();
8383

8484
expect(getPanel()).toBeFalsy();
8585
expect(input.value).toBe('12:30 AM');
@@ -91,9 +91,9 @@ describe('MatTimepicker', () => {
9191
value: jasmine.any(Date),
9292
}),
9393
);
94-
}));
94+
});
9595

96-
it('should support two-way binding on the `value` input', fakeAsync(() => {
96+
it('should support two-way binding on the `value` input', async () => {
9797
const fixture = TestBed.createComponent(TimepickerTwoWayBinding);
9898
const input = getInput(fixture);
9999
fixture.detectChanges();
@@ -115,14 +115,14 @@ describe('MatTimepicker', () => {
115115
// Propagation from host down to input
116116
fixture.componentInstance.value.set(createTime(13, 37));
117117
fixture.detectChanges();
118-
flush();
118+
await fixture.whenStable();
119119
value = inputInstance.value()!;
120120
expect(adapter.getHours(value)).toBe(13);
121121
expect(adapter.getMinutes(value)).toBe(37);
122122
expectSameTime(fixture.componentInstance.value(), value);
123-
}));
123+
});
124124

125-
it('should emit the `selected` event if the option being clicked was selected already', fakeAsync(() => {
125+
it('should emit the `selected` event if the option being clicked was selected already', async () => {
126126
const fixture = TestBed.createComponent(StandaloneTimepicker);
127127
fixture.componentInstance.value.set(new Date(2024, 0, 15, 2, 30, 0));
128128
fixture.detectChanges();
@@ -132,7 +132,7 @@ describe('MatTimepicker', () => {
132132

133133
getOptions()[getActiveOptionIndex()].click();
134134
fixture.detectChanges();
135-
flush();
135+
await fixture.whenStable();
136136

137137
expect(getPanel()).toBeFalsy();
138138
expect(fixture.componentInstance.selectedSpy).toHaveBeenCalledTimes(1);
@@ -142,7 +142,7 @@ describe('MatTimepicker', () => {
142142
value: jasmine.any(Date),
143143
}),
144144
);
145-
}));
145+
});
146146
});
147147

148148
describe('input behavior', () => {
@@ -343,44 +343,44 @@ describe('MatTimepicker', () => {
343343
expect(getPanel()).toBeFalsy();
344344
});
345345

346-
it('should close the timepicker when clicking outside', fakeAsync(() => {
346+
it('should close the timepicker when clicking outside', async () => {
347347
const fixture = TestBed.createComponent(StandaloneTimepicker);
348348
fixture.detectChanges();
349349
getInput(fixture).click();
350350
fixture.detectChanges();
351351
expect(getPanel()).toBeTruthy();
352352
document.body.click();
353353
fixture.detectChanges();
354-
flush();
354+
await fixture.whenStable();
355355
expect(getPanel()).toBeFalsy();
356-
}));
356+
});
357357

358-
it('should close the timepicker when tabbing away from the input', fakeAsync(() => {
358+
it('should close the timepicker when tabbing away from the input', async () => {
359359
const fixture = TestBed.createComponent(StandaloneTimepicker);
360360
fixture.detectChanges();
361361
getInput(fixture).click();
362362
fixture.detectChanges();
363363
expect(getPanel()).toBeTruthy();
364364
dispatchKeyboardEvent(getInput(fixture), 'keydown', TAB);
365365
fixture.detectChanges();
366-
flush();
366+
await fixture.whenStable();
367367
expect(getPanel()).toBeFalsy();
368-
}));
368+
});
369369

370-
it('should close the timepicker when pressing escape', fakeAsync(() => {
370+
it('should close the timepicker when pressing escape', async () => {
371371
const fixture = TestBed.createComponent(StandaloneTimepicker);
372372
fixture.detectChanges();
373373
getInput(fixture).click();
374374
fixture.detectChanges();
375375
expect(getPanel()).toBeTruthy();
376376
const event = dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
377377
fixture.detectChanges();
378-
flush();
378+
await fixture.whenStable();
379379
expect(getPanel()).toBeFalsy();
380380
expect(event.defaultPrevented).toBe(true);
381-
}));
381+
});
382382

383-
it('should emit events on open/close', fakeAsync(() => {
383+
it('should emit events on open/close', async () => {
384384
const fixture = TestBed.createComponent(StandaloneTimepicker);
385385
fixture.detectChanges();
386386
const {openedSpy, closedSpy} = fixture.componentInstance;
@@ -394,10 +394,10 @@ describe('MatTimepicker', () => {
394394

395395
document.body.click();
396396
fixture.detectChanges();
397-
flush();
397+
await fixture.whenStable();
398398
expect(openedSpy).toHaveBeenCalledTimes(1);
399399
expect(closedSpy).toHaveBeenCalledTimes(1);
400-
}));
400+
});
401401

402402
it('should clean up the overlay if it is open on destroy', () => {
403403
const fixture = TestBed.createComponent(StandaloneTimepicker);
@@ -409,17 +409,17 @@ describe('MatTimepicker', () => {
409409
expect(getPanel()).toBeFalsy();
410410
});
411411

412-
it('should be able to open and close the panel programmatically', fakeAsync(() => {
412+
it('should be able to open and close the panel programmatically', async () => {
413413
const fixture = TestBed.createComponent(StandaloneTimepicker);
414414
fixture.detectChanges();
415415
fixture.componentInstance.timepicker.open();
416416
fixture.detectChanges();
417417
expect(getPanel()).toBeTruthy();
418418
fixture.componentInstance.timepicker.close();
419419
fixture.detectChanges();
420-
flush();
420+
await fixture.whenStable();
421421
expect(getPanel()).toBeFalsy();
422-
}));
422+
});
423423

424424
it('should focus the input when opened programmatically', () => {
425425
const fixture = TestBed.createComponent(StandaloneTimepicker);
@@ -431,7 +431,7 @@ describe('MatTimepicker', () => {
431431
expect(document.activeElement).toBe(input);
432432
});
433433

434-
it('should expose the current open state', fakeAsync(() => {
434+
it('should expose the current open state', async () => {
435435
const fixture = TestBed.createComponent(StandaloneTimepicker);
436436
fixture.detectChanges();
437437
const timepicker = fixture.componentInstance.timepicker;
@@ -441,12 +441,12 @@ describe('MatTimepicker', () => {
441441
expect(timepicker.isOpen()).toBe(true);
442442
timepicker.close();
443443
fixture.detectChanges();
444-
flush();
444+
await fixture.whenStable();
445445
expect(timepicker.isOpen()).toBe(false);
446-
}));
446+
});
447447

448448
// Note: this will be a type checking error, but we check it just in case for JIT mode.
449-
it('should do nothing if trying to open a timepicker without an input', fakeAsync(() => {
449+
it('should do nothing if trying to open a timepicker without an input', async () => {
450450
const fixture = TestBed.createComponent(TimepickerWithoutInput);
451451
fixture.detectChanges();
452452
fixture.componentInstance.timepicker.open();
@@ -456,11 +456,10 @@ describe('MatTimepicker', () => {
456456
expect(() => {
457457
fixture.componentInstance.timepicker.close();
458458
fixture.detectChanges();
459-
flush();
460459
}).not.toThrow();
461-
}));
460+
});
462461

463-
it('should be able to reopen the panel when closed by a scroll strategy', fakeAsync(() => {
462+
it('should be able to reopen the panel when closed by a scroll strategy', async () => {
464463
const scrolledSubject = new Subject();
465464

466465
TestBed.resetTestingModule();
@@ -482,12 +481,12 @@ describe('MatTimepicker', () => {
482481
expect(getPanel()).toBeTruthy();
483482
scrolledSubject.next();
484483
fixture.detectChanges();
485-
flush();
484+
await fixture.whenStable();
486485
expect(getPanel()).toBeFalsy();
487486
fixture.componentInstance.timepicker.open();
488487
fixture.detectChanges();
489488
expect(getPanel()).toBeTruthy();
490-
}));
489+
});
491490

492491
it('should be able to opt out of opening on click', () => {
493492
const fixture = TestBed.createComponent(StandaloneTimepicker);
@@ -822,7 +821,7 @@ describe('MatTimepicker', () => {
822821
expect(event.defaultPrevented).toBe(true);
823822
});
824823

825-
it('should select the active option and close when pressing enter', fakeAsync(() => {
824+
it('should select the active option and close when pressing enter', async () => {
826825
const fixture = TestBed.createComponent(StandaloneTimepicker);
827826
const input = getInput(fixture);
828827
fixture.detectChanges();
@@ -842,7 +841,7 @@ describe('MatTimepicker', () => {
842841

843842
const event = dispatchKeyboardEvent(input, 'keydown', ENTER);
844843
fixture.detectChanges();
845-
flush();
844+
await fixture.whenStable();
846845

847846
expect(input.value).toBe('1:30 AM');
848847
expectSameTime(fixture.componentInstance.input.value(), createTime(1, 30));
@@ -855,7 +854,7 @@ describe('MatTimepicker', () => {
855854
value: jasmine.any(Date),
856855
}),
857856
);
858-
}));
857+
});
859858

860859
it('should not navigate using the left/right arrow keys', () => {
861860
const fixture = TestBed.createComponent(StandaloneTimepicker);
@@ -1040,7 +1039,7 @@ describe('MatTimepicker', () => {
10401039
expect(fixture.componentInstance.control.touched).toBe(true);
10411040
});
10421041

1043-
it('should mark the control as touched when the panel is closed', fakeAsync(() => {
1042+
it('should mark the control as touched when the panel is closed', async () => {
10441043
const fixture = TestBed.createComponent(TimepickerWithForms);
10451044
fixture.detectChanges();
10461045
expect(fixture.componentInstance.control.touched).toBe(false);
@@ -1051,9 +1050,9 @@ describe('MatTimepicker', () => {
10511050

10521051
document.body.click();
10531052
fixture.detectChanges();
1054-
flush();
1053+
await fixture.whenStable();
10551054
expect(fixture.componentInstance.control.touched).toBe(true);
1056-
}));
1055+
});
10571056

10581057
it('should not set the `required` error if there is no valid value in the input', () => {
10591058
const fixture = TestBed.createComponent(TimepickerWithForms);
@@ -1071,7 +1070,7 @@ describe('MatTimepicker', () => {
10711070
expect(control.errors?.['required']).toBeFalsy();
10721071
});
10731072

1074-
it('should set an error if the user enters an invalid time string', fakeAsync(() => {
1073+
it('should set an error if the user enters an invalid time string', () => {
10751074
const fixture = TestBed.createComponent(TimepickerWithForms);
10761075
const control = fixture.componentInstance.control;
10771076
const input = getInput(fixture);
@@ -1115,9 +1114,9 @@ describe('MatTimepicker', () => {
11151114
fixture.detectChanges();
11161115
expect(control.errors?.['matTimepickerParse']).toBeFalsy();
11171116
expectSameTime(control.value, createTime(12, 10));
1118-
}));
1117+
});
11191118

1120-
it('should set an error if the user enters a time earlier than the minimum', fakeAsync(() => {
1119+
it('should set an error if the user enters a time earlier than the minimum', async () => {
11211120
const fixture = TestBed.createComponent(TimepickerWithForms);
11221121
const control = fixture.componentInstance.control;
11231122
const input = getInput(fixture);
@@ -1138,9 +1137,9 @@ describe('MatTimepicker', () => {
11381137
fixture.componentInstance.min.set(createTime(11, 0));
11391138
fixture.detectChanges();
11401139
expect(control.errors?.['matTimepickerMin']).toBeFalsy();
1141-
}));
1140+
});
11421141

1143-
it('should set an error if the user enters a time later than the maximum', fakeAsync(() => {
1142+
it('should set an error if the user enters a time later than the maximum', async () => {
11441143
const fixture = TestBed.createComponent(TimepickerWithForms);
11451144
const control = fixture.componentInstance.control;
11461145
const input = getInput(fixture);
@@ -1161,7 +1160,7 @@ describe('MatTimepicker', () => {
11611160
fixture.componentInstance.max.set(createTime(13, 0));
11621161
fixture.detectChanges();
11631162
expect(control.errors?.['matTimepickerMax']).toBeFalsy();
1164-
}));
1163+
});
11651164

11661165
it('should mark the input as disabled when the form control is disabled', () => {
11671166
const fixture = TestBed.createComponent(TimepickerWithForms);

0 commit comments

Comments
 (0)