Skip to content

Commit 169ebc4

Browse files
authored
test: reduce reliance on static queries (#20405)
When Ivy issues were first being resolved in the repo, we accumulated a lot of static queries in tests which we generally shouldn't be depending on. These changes clean up most of the ones that don't require much refactoring to clean up. I'll make some follow-up changes for the trickier ones once these changes land.
1 parent 6d1a259 commit 169ebc4

File tree

11 files changed

+48
-44
lines changed

11 files changed

+48
-44
lines changed

src/cdk/accordion/accordion.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('CdkAccordion', () => {
5353

5454
it('should not register nested items to the same accordion', () => {
5555
const fixture = TestBed.createComponent(NestedItems);
56+
fixture.detectChanges();
5657
const innerItem = fixture.componentInstance.innerItem;
5758
const outerItem = fixture.componentInstance.outerItem;
5859

@@ -77,6 +78,6 @@ class SetOfItems {
7778
</cdk-accordion-item>
7879
</cdk-accordion>`})
7980
class NestedItems {
80-
@ViewChild('outerItem', {static: true}) outerItem: CdkAccordionItem;
81-
@ViewChild('innerItem', {static: true}) innerItem: CdkAccordionItem;
81+
@ViewChild('outerItem') outerItem: CdkAccordionItem;
82+
@ViewChild('innerItem') innerItem: CdkAccordionItem;
8283
}

src/cdk/overlay/fullscreen-overlay-container.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe('FullscreenOverlayContainer', () => {
7070

7171
it('should open an overlay inside a fullscreen element and move it to the body', () => {
7272
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
73+
fixture.detectChanges();
7374
const overlayRef = overlay.create();
7475
const fullscreenElement = fakeDocument.fullscreenElement;
7576

@@ -91,6 +92,7 @@ describe('FullscreenOverlayContainer', () => {
9192
fakeDocument.fullscreenElement = null;
9293

9394
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
95+
fixture.detectChanges();
9496
const overlayRef = overlay.create();
9597

9698
overlayRef.attach(fixture.componentInstance.templatePortal);
@@ -114,7 +116,7 @@ describe('FullscreenOverlayContainer', () => {
114116
providers: [Overlay],
115117
})
116118
class TestComponentWithTemplatePortals {
117-
@ViewChild(CdkPortal, {static: true}) templatePortal: CdkPortal;
119+
@ViewChild(CdkPortal) templatePortal: CdkPortal;
118120

119121
constructor(public viewContainerRef: ViewContainerRef) { }
120122
}

src/cdk/overlay/overlay-container.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('OverlayContainer', () => {
2424

2525
it('should remove the overlay container element from the DOM on destruction', () => {
2626
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
27-
27+
fixture.detectChanges();
2828
const overlayRef = overlay.create();
2929
overlayRef.attach(fixture.componentInstance.templatePortal);
3030
fixture.detectChanges();
@@ -101,7 +101,7 @@ describe('OverlayContainer', () => {
101101
providers: [Overlay],
102102
})
103103
class TestComponentWithTemplatePortals {
104-
@ViewChild(CdkPortal, {static: true}) templatePortal: CdkPortal;
104+
@ViewChild(CdkPortal) templatePortal: CdkPortal;
105105

106106
constructor(public viewContainerRef: ViewContainerRef) { }
107107
}

src/cdk/portal/portal.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('Portals', () => {
3737

3838
beforeEach(() => {
3939
fixture = TestBed.createComponent(PortalTestApp);
40+
fixture.detectChanges();
4041

4142
inject([ComponentFactoryResolver], (cfr: ComponentFactoryResolver) => {
4243
componentFactoryResolver = cfr;
@@ -348,6 +349,7 @@ describe('Portals', () => {
348349
fixture.destroy();
349350

350351
const unboundFixture = TestBed.createComponent(UnboundPortalTestApp);
352+
unboundFixture.detectChanges();
351353

352354
// Note: calling `detectChanges` here will cause a false positive.
353355
// What we're testing is attaching before the first CD cycle.
@@ -717,10 +719,10 @@ class ArbitraryViewContainerRefComponent {
717719
})
718720
class PortalTestApp {
719721
@ViewChildren(CdkPortal) portals: QueryList<CdkPortal>;
720-
@ViewChild(CdkPortalOutlet, {static: true}) portalOutlet: CdkPortalOutlet;
721-
@ViewChild('templateRef', {read: TemplateRef, static: true}) templateRef: TemplateRef<any>;
722-
@ViewChild('domPortalContent', {static: true}) domPortalContent: ElementRef<HTMLElement>;
723-
@ViewChild('alternateContainer', {read: ViewContainerRef, static: true})
722+
@ViewChild(CdkPortalOutlet) portalOutlet: CdkPortalOutlet;
723+
@ViewChild('templateRef', {read: TemplateRef}) templateRef: TemplateRef<any>;
724+
@ViewChild('domPortalContent') domPortalContent: ElementRef<HTMLElement>;
725+
@ViewChild('alternateContainer', {read: ViewContainerRef})
724726
alternateContainer: ViewContainerRef;
725727

726728
selectedPortal: Portal<any>|undefined;
@@ -757,7 +759,7 @@ class PortalTestApp {
757759
`,
758760
})
759761
class UnboundPortalTestApp {
760-
@ViewChild(CdkPortalOutlet, {static: true}) portalOutlet: CdkPortalOutlet;
762+
@ViewChild(CdkPortalOutlet) portalOutlet: CdkPortalOutlet;
761763
}
762764

763765
// Create a real (non-test) NgModule as a workaround for

src/cdk/text-field/autofill.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ describe('AutofillMonitor', () => {
3333
autofillMonitor = afm;
3434
fixture = TestBed.createComponent(Inputs);
3535
testComponent = fixture.componentInstance;
36+
fixture.detectChanges();
3637

3738
for (const input of [testComponent.input1, testComponent.input2, testComponent.input3]) {
3839
spyOn(input.nativeElement, 'addEventListener');
3940
spyOn(input.nativeElement, 'removeEventListener');
4041
}
41-
42-
fixture.detectChanges();
4342
}));
4443

4544
afterEach(() => {
@@ -217,9 +216,9 @@ describe('cdkAutofill', () => {
217216
})
218217
class Inputs {
219218
// Cast to `any` so we can stub out some methods in the tests.
220-
@ViewChild('input1', {static: true}) input1: ElementRef<any>;
221-
@ViewChild('input2', {static: true}) input2: ElementRef<any>;
222-
@ViewChild('input3', {static: true}) input3: ElementRef<any>;
219+
@ViewChild('input1') input1: ElementRef<any>;
220+
@ViewChild('input2') input2: ElementRef<any>;
221+
@ViewChild('input3') input3: ElementRef<any>;
223222
}
224223

225224
@Component({

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,36 +254,36 @@ abstract class BaseTestComponentRtl extends BaseTestComponent {
254254

255255
@Component({template: getTableTemplate(false)})
256256
class MatResizeTest extends BaseTestComponent {
257-
@ViewChild(MatColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
257+
@ViewChild(MatColumnResize) columnResize: AbstractMatColumnResize;
258258
}
259259

260260
@Component({template: getTableTemplate(false), changeDetection: ChangeDetectionStrategy.OnPush})
261261
class MatResizeOnPushTest extends MatResizeTest {}
262262

263263
@Component({template: getTableTemplate(true)})
264264
class MatResizeDefaultTest extends BaseTestComponent {
265-
@ViewChild(MatDefaultEnabledColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
265+
@ViewChild(MatDefaultEnabledColumnResize) columnResize: AbstractMatColumnResize;
266266
}
267267

268268
@Component({template: getTableTemplate(true)})
269269
class MatResizeDefaultRtlTest extends BaseTestComponentRtl {
270-
@ViewChild(MatDefaultEnabledColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
270+
@ViewChild(MatDefaultEnabledColumnResize) columnResize: AbstractMatColumnResize;
271271
}
272272

273273
@Component({template: getFlexTemplate(false)})
274274
class MatResizeFlexTest extends BaseTestComponent {
275-
@ViewChild(MatColumnResizeFlex, {static: true}) columnResize: AbstractMatColumnResize;
275+
@ViewChild(MatColumnResizeFlex) columnResize: AbstractMatColumnResize;
276276
}
277277

278278
@Component({template: getFlexTemplate(true)})
279279
class MatResizeDefaultFlexTest extends BaseTestComponent {
280-
@ViewChild(MatDefaultEnabledColumnResizeFlex, {static: true})
280+
@ViewChild(MatDefaultEnabledColumnResizeFlex)
281281
columnResize: AbstractMatColumnResize;
282282
}
283283

284284
@Component({template: getFlexTemplate(true)})
285285
class MatResizeDefaultFlexRtlTest extends BaseTestComponentRtl {
286-
@ViewChild(MatDefaultEnabledColumnResizeFlex, {static: true})
286+
@ViewChild(MatDefaultEnabledColumnResizeFlex)
287287
columnResize: AbstractMatColumnResize;
288288
}
289289

src/material-experimental/mdc-table/table.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ class MatTableApp {
598598
columnsToRender = ['column_a', 'column_b', 'column_c'];
599599
isFourthRow = (i: number, _rowData: TestData) => i == 3;
600600

601-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
601+
@ViewChild(MatTable) table: MatTable<TestData>;
602602
}
603603

604604
@Component({
@@ -669,7 +669,7 @@ class StickyTableApp {
669669
dataSource = new FakeDataSource();
670670
columnsToRender = ['column_a'];
671671

672-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
672+
@ViewChild(MatTable) table: MatTable<TestData>;
673673
}
674674

675675

@@ -736,9 +736,9 @@ class ArrayDataSourceMatTableApp implements AfterViewInit {
736736
dataSource = new MatTableDataSource<TestData>();
737737
columnsToRender = ['column_a', 'column_b', 'column_c'];
738738

739-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
740-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
741-
@ViewChild(MatSort, {static: true}) sort: MatSort;
739+
@ViewChild(MatTable) table: MatTable<TestData>;
740+
@ViewChild(MatPaginator) paginator: MatPaginator;
741+
@ViewChild(MatSort) sort: MatSort;
742742
@ViewChild(MatSortHeader) sortHeader: MatSortHeader;
743743

744744
constructor() {
@@ -789,8 +789,8 @@ class MatTableWithSortApp implements OnInit {
789789
dataSource = new MatTableDataSource<TestData>();
790790
columnsToRender = ['column_a', 'column_b', 'column_c'];
791791

792-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
793-
@ViewChild(MatSort, {static: true}) sort: MatSort;
792+
@ViewChild(MatTable) table: MatTable<TestData>;
793+
@ViewChild(MatSort) sort: MatSort;
794794

795795
constructor() {
796796
this.underlyingDataSource.data = [];
@@ -840,8 +840,8 @@ class MatTableWithPaginatorApp implements OnInit {
840840
dataSource = new MatTableDataSource<TestData>();
841841
columnsToRender = ['column_a', 'column_b', 'column_c'];
842842

843-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
844-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
843+
@ViewChild(MatTable) table: MatTable<TestData>;
844+
@ViewChild(MatPaginator) paginator: MatPaginator;
845845

846846
constructor() {
847847
this.underlyingDataSource.data = [];

src/material-experimental/mdc-tabs/tab-body.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class SimpleTabBodyApp implements AfterContentInit {
193193
origin: number | null;
194194

195195
@ViewChild(MatTabBody) tabBody: MatTabBody;
196-
@ViewChild(TemplateRef, {static: true}) template: TemplateRef<any>;
196+
@ViewChild(TemplateRef) template: TemplateRef<any>;
197197

198198
constructor(private _viewContainerRef: ViewContainerRef) { }
199199

src/material/table/table-data-source.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('MatTableDataSource', () => {
2020

2121
beforeEach(() => {
2222
fixture = TestBed.createComponent(MatSortApp);
23-
sort = fixture.componentInstance.sort;
2423
fixture.detectChanges();
24+
sort = fixture.componentInstance.sort;
2525
});
2626

2727
/** Test the data source's `sortData` function. */
@@ -58,5 +58,5 @@ describe('MatTableDataSource', () => {
5858
template: `<div matSort matSortDirection="asc"></div>`
5959
})
6060
class MatSortApp {
61-
@ViewChild(MatSort, {static: true}) sort: MatSort;
61+
@ViewChild(MatSort) sort: MatSort;
6262
}

src/material/table/table.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ class MatTableApp {
635635
columnsToRender = ['column_a', 'column_b', 'column_c'];
636636
isFourthRow = (i: number, _rowData: TestData) => i == 3;
637637

638-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
638+
@ViewChild(MatTable) table: MatTable<TestData>;
639639
}
640640

641641
@Component({
@@ -668,7 +668,7 @@ class NativeHtmlTableApp {
668668
dataSource: FakeDataSource | null = new FakeDataSource();
669669
columnsToRender = ['column_a', 'column_b', 'column_c'];
670670

671-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
671+
@ViewChild(MatTable) table: MatTable<TestData>;
672672
}
673673

674674
@Component({
@@ -736,7 +736,7 @@ class StickyTableApp {
736736
dataSource = new FakeDataSource();
737737
columnsToRender = ['column_a'];
738738

739-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
739+
@ViewChild(MatTable) table: MatTable<TestData>;
740740
}
741741

742742

@@ -803,9 +803,9 @@ class ArrayDataSourceMatTableApp implements AfterViewInit {
803803
dataSource = new MatTableDataSource<TestData>();
804804
columnsToRender = ['column_a', 'column_b', 'column_c'];
805805

806-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
807-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
808-
@ViewChild(MatSort, {static: true}) sort: MatSort;
806+
@ViewChild(MatTable) table: MatTable<TestData>;
807+
@ViewChild(MatPaginator) paginator: MatPaginator;
808+
@ViewChild(MatSort) sort: MatSort;
809809
@ViewChild(MatSortHeader) sortHeader: MatSortHeader;
810810

811811
constructor() {
@@ -856,8 +856,8 @@ class MatTableWithSortApp implements OnInit {
856856
dataSource = new MatTableDataSource<TestData>();
857857
columnsToRender = ['column_a', 'column_b', 'column_c'];
858858

859-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
860-
@ViewChild(MatSort, {static: true}) sort: MatSort;
859+
@ViewChild(MatTable) table: MatTable<TestData>;
860+
@ViewChild(MatSort) sort: MatSort;
861861

862862
constructor() {
863863
this.underlyingDataSource.data = [];
@@ -907,8 +907,8 @@ class MatTableWithPaginatorApp implements OnInit {
907907
dataSource = new MatTableDataSource<TestData>();
908908
columnsToRender = ['column_a', 'column_b', 'column_c'];
909909

910-
@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
911-
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
910+
@ViewChild(MatTable) table: MatTable<TestData>;
911+
@ViewChild(MatPaginator) paginator: MatPaginator;
912912

913913
constructor() {
914914
this.underlyingDataSource.data = [];

src/material/tabs/tab-body.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class SimpleTabBodyApp implements AfterContentInit {
193193
origin: number | null;
194194

195195
@ViewChild(MatTabBody) tabBody: MatTabBody;
196-
@ViewChild(TemplateRef, {static: true}) template: TemplateRef<any>;
196+
@ViewChild(TemplateRef) template: TemplateRef<any>;
197197

198198
constructor(private _viewContainerRef: ViewContainerRef) { }
199199

0 commit comments

Comments
 (0)