Skip to content

Commit 263dadf

Browse files
authored
test: Make tests fail on errors and fix newly uncovered failures (angular#29110)
1 parent f76f8a8 commit 263dadf

File tree

15 files changed

+146
-86
lines changed

15 files changed

+146
-86
lines changed

src/cdk-experimental/combobox/combobox.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {CdkComboboxPopup} from '@angular/cdk-experimental/combobox/combobox-popup';
2+
import {DOWN_ARROW, ESCAPE} from '@angular/cdk/keycodes';
3+
import {dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing/private';
14
import {
25
Component,
36
DebugElement,
@@ -8,11 +11,8 @@ import {
811
} from '@angular/core';
912
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
1013
import {By} from '@angular/platform-browser';
11-
import {CdkComboboxModule} from './combobox-module';
1214
import {CdkCombobox} from './combobox';
13-
import {dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing/private';
14-
import {DOWN_ARROW, ESCAPE} from '@angular/cdk/keycodes';
15-
import {CdkComboboxPopup} from '@angular/cdk-experimental/combobox/combobox-popup';
15+
import {CdkComboboxModule} from './combobox-module';
1616

1717
describe('Combobox', () => {
1818
describe('with a basic toggle trigger', () => {
@@ -249,10 +249,10 @@ describe('Combobox', () => {
249249
});
250250

251251
it('should throw error when given invalid open action', () => {
252-
const errorSpy = spyOn(console, 'error');
253-
testComponent.actions.set('invalidAction');
254-
fixture.detectChanges();
255-
expect(errorSpy).toHaveBeenCalled();
252+
expect(() => {
253+
testComponent.actions.set('invalidAction');
254+
fixture.detectChanges();
255+
}).toThrowError('invalidAction is not a support open action for CdkCombobox');
256256
});
257257
});
258258

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {CdkVirtualScrollViewport, ScrollingModule} from '@angular/cdk/scrolling';
22
import {Component, Input, ViewChild, ViewEncapsulation} from '@angular/core';
3-
import {waitForAsync, ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
44
import {ScrollingModule as ExperimentalScrollingModule} from './scrolling-module';
55

66
describe('CdkVirtualScrollViewport', () => {
@@ -47,11 +47,13 @@ describe('CdkVirtualScrollViewport', () => {
4747
}));
4848

4949
it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {
50-
testComponent.minBufferPx = 100;
51-
testComponent.maxBufferPx = 99;
52-
const errorSpy = spyOn(console, 'error');
53-
finishInit(fixture);
54-
expect(errorSpy).toHaveBeenCalled();
50+
expect(() => {
51+
testComponent.minBufferPx = 100;
52+
testComponent.maxBufferPx = 99;
53+
finishInit(fixture);
54+
}).toThrowError(
55+
'CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx',
56+
);
5557
}));
5658

5759
// TODO(mmalerba): Add test that it corrects the initial render if it didn't render enough,

src/cdk/listbox/listbox.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,9 @@ describe('CdkOption and CdkListbox', () => {
10581058
});
10591059

10601060
it('should throw on init if the preselected value is invalid', () => {
1061-
const errorSpy = spyOn(console, 'error');
1062-
setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]);
1063-
expect(errorSpy.calls.first().args[1]).toMatch(
1064-
/Listbox has selected values that do not match any of its options./,
1065-
);
1061+
expect(() => {
1062+
setupComponent(ListboxWithInvalidPreselectedFormControl, [ReactiveFormsModule]);
1063+
}).toThrowError('Listbox has selected values that do not match any of its options.');
10661064
});
10671065
});
10681066
});

src/cdk/menu/menu-trigger.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import {Component, ViewChildren, QueryList, ElementRef, ViewChild, Type} from '@angular/core';
1+
import {ENTER, SPACE, TAB} from '@angular/cdk/keycodes';
2+
import {
3+
Component,
4+
ElementRef,
5+
QueryList,
6+
Type,
7+
ViewChild,
8+
ViewChildren,
9+
provideZoneChangeDetection,
10+
} from '@angular/core';
211
import {ComponentFixture, TestBed, fakeAsync, tick, waitForAsync} from '@angular/core/testing';
312
import {By} from '@angular/platform-browser';
413
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
5-
import {TAB, SPACE, ENTER} from '@angular/cdk/keycodes';
6-
import {CdkMenuModule} from './menu-module';
7-
import {CdkMenuItem} from './menu-item';
814
import {CdkMenu} from './menu';
9-
import {CdkMenuTrigger} from './menu-trigger';
1015
import {Menu} from './menu-interface';
16+
import {CdkMenuItem} from './menu-item';
17+
import {CdkMenuModule} from './menu-module';
18+
import {CdkMenuTrigger} from './menu-trigger';
1119

1220
describe('MenuTrigger', () => {
1321
describe('on CdkMenuItem', () => {
@@ -114,6 +122,7 @@ describe('MenuTrigger', () => {
114122
TestBed.configureTestingModule({
115123
imports: [CdkMenuModule],
116124
declarations: [MenuBarWithNestedSubMenus],
125+
providers: [provideZoneChangeDetection()],
117126
}).compileComponents();
118127
}));
119128

src/cdk/menu/menu.spec.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1+
import {TAB} from '@angular/cdk/keycodes';
2+
import {
3+
Component,
4+
ElementRef,
5+
QueryList,
6+
ViewChild,
7+
ViewChildren,
8+
provideZoneChangeDetection,
9+
} from '@angular/core';
110
import {
211
ComponentFixture,
12+
TestBed,
313
fakeAsync,
414
flush,
5-
TestBed,
615
tick,
716
waitForAsync,
817
} from '@angular/core/testing';
9-
import {Component, ElementRef, QueryList, ViewChild, ViewChildren} from '@angular/core';
10-
import {TAB} from '@angular/cdk/keycodes';
18+
import {By} from '@angular/platform-browser';
1119
import {
1220
createMouseEvent,
1321
dispatchEvent,
1422
dispatchKeyboardEvent,
1523
dispatchMouseEvent,
1624
} from '../../cdk/testing/private';
17-
import {By} from '@angular/platform-browser';
1825
import {CdkMenu} from './menu';
19-
import {CdkMenuModule} from './menu-module';
20-
import {CdkMenuItemCheckbox} from './menu-item-checkbox';
2126
import {CdkMenuItem} from './menu-item';
27+
import {CdkMenuItemCheckbox} from './menu-item-checkbox';
28+
import {CdkMenuModule} from './menu-module';
2229

2330
describe('Menu', () => {
2431
describe('as checkbox group', () => {
@@ -138,6 +145,7 @@ describe('Menu', () => {
138145
beforeEach(waitForAsync(() => {
139146
TestBed.configureTestingModule({
140147
imports: [CdkMenuModule, WithComplexNestedMenus],
148+
providers: [provideZoneChangeDetection()],
141149
}).compileComponents();
142150
}));
143151

@@ -329,6 +337,7 @@ describe('Menu', () => {
329337
beforeEach(waitForAsync(() => {
330338
TestBed.configureTestingModule({
331339
imports: [CdkMenuModule, WithComplexNestedMenusOnBottom],
340+
providers: [provideZoneChangeDetection()],
332341
}).compileComponents();
333342
}));
334343

src/components-examples/material/checkbox/checkbox-harness/checkbox-harness-example.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import {ComponentFixture, TestBed} from '@angular/core/testing';
1+
import {HarnessLoader} from '@angular/cdk/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {provideZoneChangeDetection} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
35
import {MatCheckboxHarness} from '@angular/material/checkbox/testing';
4-
import {HarnessLoader} from '@angular/cdk/testing';
56
import {CheckboxHarnessExample} from './checkbox-harness-example';
67

78
describe('CheckboxHarnessExample', () => {
89
let fixture: ComponentFixture<CheckboxHarnessExample>;
910
let loader: HarnessLoader;
1011

1112
beforeEach(() => {
13+
TestBed.configureTestingModule({
14+
providers: [provideZoneChangeDetection()],
15+
});
16+
1217
fixture = TestBed.createComponent(CheckboxHarnessExample);
1318
fixture.detectChanges();
1419
loader = TestbedHarnessEnvironment.loader(fixture);

src/components-examples/material/slide-toggle/slide-toggle-harness/slide-toggle-harness-example.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import {ComponentFixture, TestBed} from '@angular/core/testing';
1+
import {HarnessLoader} from '@angular/cdk/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {provideZoneChangeDetection} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
35
import {MatSlideToggleHarness} from '@angular/material/slide-toggle/testing';
4-
import {HarnessLoader} from '@angular/cdk/testing';
56
import {SlideToggleHarnessExample} from './slide-toggle-harness-example';
67

78
describe('SlideToggleHarnessExample', () => {
89
let fixture: ComponentFixture<SlideToggleHarnessExample>;
910
let loader: HarnessLoader;
1011

1112
beforeEach(() => {
13+
TestBed.configureTestingModule({
14+
providers: [provideZoneChangeDetection()],
15+
});
16+
1217
fixture = TestBed.createComponent(SlideToggleHarnessExample);
1318
fixture.detectChanges();
1419
loader = TestbedHarnessEnvironment.loader(fixture);

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import {
2828
provideZoneChangeDetection,
2929
} from '@angular/core';
3030
import {
31-
waitForAsync,
3231
ComponentFixture,
32+
TestBed,
3333
fakeAsync,
3434
flush,
3535
inject,
36-
TestBed,
3736
tick,
37+
waitForAsync,
3838
} from '@angular/core/testing';
3939
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
4040
import {MatOption, MatOptionSelectionChange} from '@angular/material/core';
@@ -45,15 +45,15 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
4545
import {EMPTY, Observable, Subject, Subscription} from 'rxjs';
4646
import {map, startWith} from 'rxjs/operators';
4747
import {
48-
getMatAutocompleteMissingPanelError,
48+
MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
49+
MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
4950
MatAutocomplete,
5051
MatAutocompleteDefaultOptions,
5152
MatAutocompleteModule,
5253
MatAutocompleteOrigin,
5354
MatAutocompleteSelectedEvent,
5455
MatAutocompleteTrigger,
55-
MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
56-
MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
56+
getMatAutocompleteMissingPanelError,
5757
} from './index';
5858

5959
describe('MDC-based MatAutocomplete', () => {
@@ -1025,6 +1025,9 @@ describe('MDC-based MatAutocomplete', () => {
10251025
it('should disable the input when used with a value accessor and without `matInput`', () => {
10261026
fixture.destroy();
10271027
TestBed.resetTestingModule();
1028+
TestBed.configureTestingModule({
1029+
providers: [provideZoneChangeDetection()],
1030+
});
10281031

10291032
const plainFixture = createComponent(PlainAutocompleteInputWithFormControl);
10301033
plainFixture.detectChanges();

src/material/bottom-sheet/bottom-sheet.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
ViewChild,
2121
ViewContainerRef,
2222
ViewEncapsulation,
23+
provideZoneChangeDetection,
2324
} from '@angular/core';
2425
import {
2526
ComponentFixture,
@@ -61,7 +62,7 @@ describe('MatBottomSheet', () => {
6162
BottomSheetWithInjectedData,
6263
ShadowDomComponent,
6364
],
64-
providers: [{provide: Location, useClass: SpyLocation}],
65+
providers: [provideZoneChangeDetection(), {provide: Location, useClass: SpyLocation}],
6566
}).compileComponents();
6667
}));
6768

@@ -639,6 +640,7 @@ describe('MatBottomSheet', () => {
639640
autoFocus: 'first-tabbable',
640641
});
641642

643+
viewContainerFixture.detectChanges();
642644
await viewContainerFixture.whenStable();
643645

644646
expect(document.activeElement!.tagName)

src/material/datepicker/datepicker.spec.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
UP_ARROW,
1111
} from '@angular/cdk/keycodes';
1212
import {Overlay} from '@angular/cdk/overlay';
13+
import {_supportsShadowDom} from '@angular/cdk/platform';
1314
import {ScrollDispatcher} from '@angular/cdk/scrolling';
1415
import {
1516
createKeyboardEvent,
@@ -21,40 +22,39 @@ import {
2122
} from '@angular/cdk/testing/private';
2223
import {
2324
Component,
25+
Directive,
26+
Provider,
2427
Type,
2528
ViewChild,
26-
Provider,
27-
Directive,
2829
ViewEncapsulation,
2930
provideZoneChangeDetection,
3031
} from '@angular/core';
31-
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
32+
import {ComponentFixture, TestBed, fakeAsync, flush, inject, tick} from '@angular/core/testing';
3233
import {
3334
FormControl,
3435
FormsModule,
36+
NG_VALIDATORS,
3537
NgModel,
3638
ReactiveFormsModule,
3739
Validator,
38-
NG_VALIDATORS,
3940
} from '@angular/forms';
4041
import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core';
4142
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
42-
import {DEC, JAN, JUL, JUN, SEP} from '../testing';
43+
import {MatInputModule} from '@angular/material/input';
4344
import {By} from '@angular/platform-browser';
44-
import {_supportsShadowDom} from '@angular/cdk/platform';
4545
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
4646
import {Subject} from 'rxjs';
47-
import {MatInputModule} from '@angular/material/input';
47+
import {DEC, JAN, JUL, JUN, SEP} from '../testing';
4848
import {MatDatepicker} from './datepicker';
49+
import {DatepickerDropdownPositionX, DatepickerDropdownPositionY} from './datepicker-base';
4950
import {MatDatepickerInput} from './datepicker-input';
5051
import {MatDatepickerToggle} from './datepicker-toggle';
5152
import {
5253
MAT_DATEPICKER_SCROLL_STRATEGY,
54+
MatDateSelectionModel,
5355
MatDatepickerIntl,
5456
MatDatepickerModule,
55-
MatDateSelectionModel,
5657
} from './index';
57-
import {DatepickerDropdownPositionX, DatepickerDropdownPositionY} from './datepicker-base';
5858

5959
describe('MatDatepicker', () => {
6060
const SUPPORTS_INTL = typeof Intl != 'undefined';
@@ -511,6 +511,9 @@ describe('MatDatepicker', () => {
511511

512512
it('should reset the datepicker when it is closed externally', fakeAsync(() => {
513513
TestBed.resetTestingModule();
514+
TestBed.configureTestingModule({
515+
providers: [provideZoneChangeDetection()],
516+
});
514517

515518
const scrolledSubject = new Subject();
516519

@@ -1312,6 +1315,9 @@ describe('MatDatepicker', () => {
13121315

13131316
fixture.destroy();
13141317
TestBed.resetTestingModule();
1318+
TestBed.configureTestingModule({
1319+
providers: [provideZoneChangeDetection()],
1320+
});
13151321
fixture = createComponent(DatepickerWithToggleInShadowDom, [MatNativeDateModule]);
13161322
fixture.detectChanges();
13171323
testComponent = fixture.componentInstance;

src/material/dialog/testing/dialog-opener.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component, Inject} from '@angular/core';
2-
import {fakeAsync, TestBed, flush} from '@angular/core/testing';
3-
import {MatTestDialogOpenerModule, MatTestDialogOpener} from '@angular/material/dialog/testing';
2+
import {TestBed, fakeAsync, flush} from '@angular/core/testing';
43
import {MAT_DIALOG_DATA, MatDialogRef, MatDialogState} from '@angular/material/dialog';
4+
import {MatTestDialogOpener, MatTestDialogOpenerModule} from '@angular/material/dialog/testing';
55
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
66

77
describe('MDC-based MatTestDialogOpener', () => {
@@ -26,12 +26,13 @@ describe('MDC-based MatTestDialogOpener', () => {
2626
);
2727
});
2828

29-
it('should pass data to the component', () => {
29+
it('should pass data to the component', async () => {
3030
const config = {data: 'test'};
3131
const fixture = TestBed.createComponent(
3232
MatTestDialogOpener.withComponent(ExampleComponent, config),
3333
);
3434
fixture.detectChanges();
35+
await fixture.whenStable();
3536
const dialogContainer = document.querySelector('mat-dialog-container');
3637
expect(dialogContainer!.innerHTML).toContain('Data: test');
3738
});

0 commit comments

Comments
 (0)