Skip to content

Commit a8f6341

Browse files
committed
test(material/timepicker): use date adapter for time comparisons
Attemps to deflake the timepicker tests by using the date adapter for comparisons.
1 parent 39b6dbf commit a8f6341

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/material/timepicker/timepicker.spec.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('MatTimepicker', () => {
7373

7474
expect(getPanel()).toBeFalsy();
7575
expect(input.value).toBe('12:30 AM');
76-
expect(fixture.componentInstance.input.value()).toEqual(createTime(0, 30));
76+
expectSameTime(fixture.componentInstance.input.value(), createTime(0, 30));
7777
expect(fixture.componentInstance.selectedSpy).toHaveBeenCalledTimes(1);
7878
expect(fixture.componentInstance.selectedSpy).toHaveBeenCalledWith(
7979
jasmine.objectContaining({
@@ -91,7 +91,7 @@ describe('MatTimepicker', () => {
9191

9292
// Initial value
9393
expect(fixture.componentInstance.value).toBeTruthy();
94-
expect(inputInstance.value()).toEqual(fixture.componentInstance.value());
94+
expectSameTime(inputInstance.value(), fixture.componentInstance.value());
9595

9696
// Propagation from input back to host
9797
clearElement(input);
@@ -100,7 +100,7 @@ describe('MatTimepicker', () => {
100100
let value = inputInstance.value()!;
101101
expect(adapter.getHours(value)).toBe(11);
102102
expect(adapter.getMinutes(value)).toBe(15);
103-
expect(fixture.componentInstance.value()).toEqual(value);
103+
expectSameTime(fixture.componentInstance.value(), value);
104104

105105
// Propagation from host down to input
106106
fixture.componentInstance.value.set(createTime(13, 37));
@@ -109,7 +109,7 @@ describe('MatTimepicker', () => {
109109
value = inputInstance.value()!;
110110
expect(adapter.getHours(value)).toBe(13);
111111
expect(adapter.getMinutes(value)).toBe(37);
112-
expect(value).toEqual(fixture.componentInstance.value());
112+
expectSameTime(fixture.componentInstance.value(), value);
113113
}));
114114

115115
it('should emit the `selected` event if the option being clicked was selected already', fakeAsync(() => {
@@ -169,7 +169,7 @@ describe('MatTimepicker', () => {
169169

170170
// The user's value shouldn't be overwritten.
171171
expect(input.value).toBe('13:37');
172-
expect(fixture.componentInstance.input.value()).toEqual(createTime(13, 37));
172+
expectSameTime(fixture.componentInstance.input.value(), createTime(13, 37));
173173
});
174174

175175
it('should parse invalid time string', () => {
@@ -251,14 +251,14 @@ describe('MatTimepicker', () => {
251251
typeInElement(input, '2:10 PM');
252252
fixture.detectChanges();
253253
expect(input.value).toBe('2:10 PM');
254-
expect(inputInstance.value()).toEqual(new Date(...dateParts, 14, 10, 0));
254+
expectSameTime(inputInstance.value(), new Date(...dateParts, 14, 10, 0));
255255
});
256256

257257
it('should not accept an invalid `min` value', () => {
258258
const fixture = TestBed.createComponent(StandaloneTimepicker);
259259
fixture.componentInstance.min.set(createTime(13, 45));
260260
fixture.detectChanges();
261-
expect(fixture.componentInstance.input.min()).toEqual(createTime(13, 45));
261+
expectSameTime(fixture.componentInstance.input.min(), createTime(13, 45));
262262

263263
fixture.componentInstance.min.set(adapter.invalid());
264264
fixture.detectChanges();
@@ -269,7 +269,7 @@ describe('MatTimepicker', () => {
269269
const fixture = TestBed.createComponent(StandaloneTimepicker);
270270
fixture.componentInstance.max.set(createTime(13, 45));
271271
fixture.detectChanges();
272-
expect(fixture.componentInstance.input.max()).toEqual(createTime(13, 45));
272+
expectSameTime(fixture.componentInstance.input.max(), createTime(13, 45));
273273

274274
fixture.componentInstance.max.set(adapter.invalid());
275275
fixture.detectChanges();
@@ -280,14 +280,14 @@ describe('MatTimepicker', () => {
280280
const fixture = TestBed.createComponent(StandaloneTimepicker);
281281
fixture.componentInstance.min.set('1:45 PM');
282282
fixture.detectChanges();
283-
expect(fixture.componentInstance.input.min()).toEqual(createTime(13, 45));
283+
expectSameTime(fixture.componentInstance.input.min(), createTime(13, 45));
284284
});
285285

286286
it('should accept a valid time string as the `max`', () => {
287287
const fixture = TestBed.createComponent(StandaloneTimepicker);
288288
fixture.componentInstance.max.set('1:45 PM');
289289
fixture.detectChanges();
290-
expect(fixture.componentInstance.input.max()).toEqual(createTime(13, 45));
290+
expectSameTime(fixture.componentInstance.input.max(), createTime(13, 45));
291291
});
292292

293293
it('should throw if multiple inputs are associated with a timepicker', () => {
@@ -788,7 +788,7 @@ describe('MatTimepicker', () => {
788788
flush();
789789

790790
expect(input.value).toBe('1:30 AM');
791-
expect(fixture.componentInstance.input.value()).toEqual(createTime(1, 30));
791+
expectSameTime(fixture.componentInstance.input.value(), createTime(1, 30));
792792
expect(getPanel()).toBeFalsy();
793793
expect(event.defaultPrevented).toBeTrue();
794794
expect(fixture.componentInstance.selectedSpy).toHaveBeenCalledTimes(1);
@@ -868,7 +868,7 @@ describe('MatTimepicker', () => {
868868

869869
typeInElement(input, '1:37 PM');
870870
fixture.detectChanges();
871-
expect(control.value).toEqual(createTime(13, 37));
871+
expectSameTime(control.value, createTime(13, 37));
872872
expect(control.dirty).toBe(true);
873873
expect(control.touched).toBe(false);
874874

@@ -890,7 +890,7 @@ describe('MatTimepicker', () => {
890890
getOptions()[5].click();
891891
fixture.detectChanges();
892892

893-
expect(control.value).toEqual(createTime(2, 30));
893+
expectSameTime(control.value, createTime(2, 30));
894894
expect(control.dirty).toBe(true);
895895
});
896896

@@ -930,7 +930,7 @@ describe('MatTimepicker', () => {
930930
getOptions()[5].click();
931931
fixture.detectChanges();
932932

933-
expect(control.value).toEqual(createTime(2, 30));
933+
expectSameTime(control.value, createTime(2, 30));
934934
expect(control.dirty).toBe(false);
935935
expect(spy).not.toHaveBeenCalled();
936936
subscription.unsubscribe();
@@ -946,7 +946,7 @@ describe('MatTimepicker', () => {
946946
fixture.componentInstance.input.value.set(createTime(12, 0));
947947
fixture.detectChanges();
948948

949-
expect(control.value).toEqual(createTime(13, 37));
949+
expectSameTime(control.value, createTime(13, 37));
950950
expect(control.dirty).toBe(false);
951951
});
952952

@@ -1002,7 +1002,7 @@ describe('MatTimepicker', () => {
10021002
typeInElement(input, '10:10 AM');
10031003
fixture.detectChanges();
10041004
expect(control.errors?.['matTimepickerParse']).toBeFalsy();
1005-
expect(control.value).toEqual(createTime(10, 10));
1005+
expectSameTime(control.value, createTime(10, 10));
10061006

10071007
clearElement(input);
10081008
typeInElement(input, 'not a valid date');
@@ -1034,7 +1034,7 @@ describe('MatTimepicker', () => {
10341034
typeInElement(input, '12:10 PM');
10351035
fixture.detectChanges();
10361036
expect(control.errors?.['matTimepickerParse']).toBeFalsy();
1037-
expect(control.value).toEqual(createTime(12, 10));
1037+
expectSameTime(control.value, createTime(12, 10));
10381038
}));
10391039

10401040
it('should set an error if the user enters a time earlier than the minimum', fakeAsync(() => {
@@ -1052,7 +1052,7 @@ describe('MatTimepicker', () => {
10521052
typeInElement(input, '11:59 AM');
10531053
fixture.detectChanges();
10541054
expect(control.errors?.['matTimepickerMin']).toBeTruthy();
1055-
expect(control.value).toEqual(createTime(11, 59));
1055+
expectSameTime(control.value, createTime(11, 59));
10561056

10571057
// Change the minimum so the value becomes valid.
10581058
fixture.componentInstance.min.set(createTime(11, 0));
@@ -1075,7 +1075,7 @@ describe('MatTimepicker', () => {
10751075
typeInElement(input, '12:01 PM');
10761076
fixture.detectChanges();
10771077
expect(control.errors?.['matTimepickerMax']).toBeTruthy();
1078-
expect(control.value).toEqual(createTime(12, 1));
1078+
expectSameTime(control.value, createTime(12, 1));
10791079

10801080
// Change the maximum so the value becomes valid.
10811081
fixture.componentInstance.max.set(createTime(13, 0));
@@ -1197,6 +1197,12 @@ describe('MatTimepicker', () => {
11971197
});
11981198
});
11991199

1200+
function expectSameTime(one: Date | null, two: Date | null): void {
1201+
expect(adapter.sameTime(one, two))
1202+
.withContext(`Expected ${one} to be same time as ${two}`)
1203+
.toBe(true);
1204+
}
1205+
12001206
function configureTestingModule(additionalProviders: Provider[] = []): void {
12011207
TestBed.configureTestingModule({
12021208
imports: [NoopAnimationsModule],

0 commit comments

Comments
 (0)