Skip to content

Commit c999b2e

Browse files
committed
fix(material/timepicker): revert to setTimeout(0) with proper test handling
1 parent 6859e49 commit c999b2e

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/material/timepicker/timepicker.spec.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('MatTimepicker', () => {
4646

4747
getOptions()[3].click();
4848
fixture.detectChanges();
49-
flushMicrotasks();
5049
flush();
5150

5251
const value = fixture.componentInstance.input.value()!;
@@ -72,7 +71,6 @@ describe('MatTimepicker', () => {
7271

7372
getOptions()[1].click();
7473
fixture.detectChanges();
75-
flushMicrotasks();
7674
flush();
7775

7876
expect(getPanel()).toBeFalsy();
@@ -126,7 +124,6 @@ describe('MatTimepicker', () => {
126124

127125
getOptions()[getActiveOptionIndex()].click();
128126
fixture.detectChanges();
129-
flushMicrotasks();
130127
flush();
131128

132129
expect(getPanel()).toBeFalsy();
@@ -164,7 +161,6 @@ describe('MatTimepicker', () => {
164161
fixture.detectChanges();
165162
getOptions()[3].click(); // Select 1:30 AM
166163
fixture.detectChanges();
167-
flushMicrotasks(); // Wait for Promise.resolve().then() to complete
168164
flush();
169165

170166
expect(formControlValue).toBeTruthy();
@@ -871,7 +867,6 @@ describe('MatTimepicker', () => {
871867

872868
const event = dispatchKeyboardEvent(input, 'keydown', ENTER);
873869
fixture.detectChanges();
874-
flushMicrotasks();
875870
flush();
876871

877872
expect(input.value).toBe('1:30 AM');
@@ -965,7 +960,7 @@ describe('MatTimepicker', () => {
965960
expect(control.dirty).toBe(true);
966961
});
967962

968-
it('should propagate value selected from the panel to the form control', () => {
963+
it('should propagate value selected from the panel to the form control', fakeAsync(() => {
969964
const fixture = TestBed.createComponent(TimepickerWithForms);
970965
const control = fixture.componentInstance.control;
971966
fixture.detectChanges();
@@ -976,11 +971,11 @@ describe('MatTimepicker', () => {
976971
fixture.detectChanges();
977972
getOptions()[5].click();
978973
fixture.detectChanges();
979-
flushMicrotasks();
974+
flush();
980975

981976
expectSameTime(control.value, createTime(2, 30));
982977
expect(control.dirty).toBe(true);
983-
});
978+
}));
984979

985980
it('should format values assigned to the input through the form control', () => {
986981
const fixture = TestBed.createComponent(TimepickerWithForms);
@@ -1003,7 +998,7 @@ describe('MatTimepicker', () => {
1003998
expect(input.value).toBe('10:10 AM');
1004999
});
10051000

1006-
it('should not change the control if the same value is selected from the dropdown', () => {
1001+
it('should not change the control if the same value is selected from the dropdown', fakeAsync(() => {
10071002
const fixture = TestBed.createComponent(TimepickerWithForms);
10081003
const control = fixture.componentInstance.control;
10091004
control.setValue(createTime(2, 30));
@@ -1017,13 +1012,13 @@ describe('MatTimepicker', () => {
10171012
fixture.detectChanges();
10181013
getOptions()[5].click();
10191014
fixture.detectChanges();
1020-
flushMicrotasks();
1015+
flush();
10211016

10221017
expectSameTime(control.value, createTime(2, 30));
10231018
expect(control.dirty).toBe(false);
10241019
expect(spy).not.toHaveBeenCalled();
10251020
subscription.unsubscribe();
1026-
});
1021+
}));
10271022

10281023
it('should not propagate programmatic changes to the form control', () => {
10291024
const fixture = TestBed.createComponent(TimepickerWithForms);

src/material/timepicker/timepicker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
297297
}
298298
});
299299
// Emit the selected event after the current execution cycle to ensure the form control is updated first
300-
Promise.resolve().then(() => {
300+
setTimeout(() => {
301301
this.selected.emit({value: option.value, source: this});
302-
});
302+
}, 0);
303303
this._input()?.focus();
304304
}
305305

0 commit comments

Comments
 (0)