Skip to content

test: reduce reliance on static queries #20405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cdk/accordion/accordion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('CdkAccordion', () => {

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

Expand All @@ -77,6 +78,6 @@ class SetOfItems {
</cdk-accordion-item>
</cdk-accordion>`})
class NestedItems {
@ViewChild('outerItem', {static: true}) outerItem: CdkAccordionItem;
@ViewChild('innerItem', {static: true}) innerItem: CdkAccordionItem;
@ViewChild('outerItem') outerItem: CdkAccordionItem;
@ViewChild('innerItem') innerItem: CdkAccordionItem;
}
4 changes: 3 additions & 1 deletion src/cdk/overlay/fullscreen-overlay-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('FullscreenOverlayContainer', () => {

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

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

const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);
fixture.detectChanges();
const overlayRef = overlay.create();

overlayRef.attach(fixture.componentInstance.templatePortal);
Expand All @@ -114,7 +116,7 @@ describe('FullscreenOverlayContainer', () => {
providers: [Overlay],
})
class TestComponentWithTemplatePortals {
@ViewChild(CdkPortal, {static: true}) templatePortal: CdkPortal;
@ViewChild(CdkPortal) templatePortal: CdkPortal;

constructor(public viewContainerRef: ViewContainerRef) { }
}
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/overlay/overlay-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('OverlayContainer', () => {

it('should remove the overlay container element from the DOM on destruction', () => {
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals);

fixture.detectChanges();
const overlayRef = overlay.create();
overlayRef.attach(fixture.componentInstance.templatePortal);
fixture.detectChanges();
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('OverlayContainer', () => {
providers: [Overlay],
})
class TestComponentWithTemplatePortals {
@ViewChild(CdkPortal, {static: true}) templatePortal: CdkPortal;
@ViewChild(CdkPortal) templatePortal: CdkPortal;

constructor(public viewContainerRef: ViewContainerRef) { }
}
Expand Down
12 changes: 7 additions & 5 deletions src/cdk/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('Portals', () => {

beforeEach(() => {
fixture = TestBed.createComponent(PortalTestApp);
fixture.detectChanges();

inject([ComponentFactoryResolver], (cfr: ComponentFactoryResolver) => {
componentFactoryResolver = cfr;
Expand Down Expand Up @@ -348,6 +349,7 @@ describe('Portals', () => {
fixture.destroy();

const unboundFixture = TestBed.createComponent(UnboundPortalTestApp);
unboundFixture.detectChanges();

// Note: calling `detectChanges` here will cause a false positive.
// What we're testing is attaching before the first CD cycle.
Expand Down Expand Up @@ -717,10 +719,10 @@ class ArbitraryViewContainerRefComponent {
})
class PortalTestApp {
@ViewChildren(CdkPortal) portals: QueryList<CdkPortal>;
@ViewChild(CdkPortalOutlet, {static: true}) portalOutlet: CdkPortalOutlet;
@ViewChild('templateRef', {read: TemplateRef, static: true}) templateRef: TemplateRef<any>;
@ViewChild('domPortalContent', {static: true}) domPortalContent: ElementRef<HTMLElement>;
@ViewChild('alternateContainer', {read: ViewContainerRef, static: true})
@ViewChild(CdkPortalOutlet) portalOutlet: CdkPortalOutlet;
@ViewChild('templateRef', {read: TemplateRef}) templateRef: TemplateRef<any>;
@ViewChild('domPortalContent') domPortalContent: ElementRef<HTMLElement>;
@ViewChild('alternateContainer', {read: ViewContainerRef})
alternateContainer: ViewContainerRef;

selectedPortal: Portal<any>|undefined;
Expand Down Expand Up @@ -757,7 +759,7 @@ class PortalTestApp {
`,
})
class UnboundPortalTestApp {
@ViewChild(CdkPortalOutlet, {static: true}) portalOutlet: CdkPortalOutlet;
@ViewChild(CdkPortalOutlet) portalOutlet: CdkPortalOutlet;
}

// Create a real (non-test) NgModule as a workaround for
Expand Down
9 changes: 4 additions & 5 deletions src/cdk/text-field/autofill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ describe('AutofillMonitor', () => {
autofillMonitor = afm;
fixture = TestBed.createComponent(Inputs);
testComponent = fixture.componentInstance;
fixture.detectChanges();

for (const input of [testComponent.input1, testComponent.input2, testComponent.input3]) {
spyOn(input.nativeElement, 'addEventListener');
spyOn(input.nativeElement, 'removeEventListener');
}

fixture.detectChanges();
}));

afterEach(() => {
Expand Down Expand Up @@ -217,9 +216,9 @@ describe('cdkAutofill', () => {
})
class Inputs {
// Cast to `any` so we can stub out some methods in the tests.
@ViewChild('input1', {static: true}) input1: ElementRef<any>;
@ViewChild('input2', {static: true}) input2: ElementRef<any>;
@ViewChild('input3', {static: true}) input3: ElementRef<any>;
@ViewChild('input1') input1: ElementRef<any>;
@ViewChild('input2') input2: ElementRef<any>;
@ViewChild('input3') input3: ElementRef<any>;
}

@Component({
Expand Down
12 changes: 6 additions & 6 deletions src/material-experimental/column-resize/column-resize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,36 +254,36 @@ abstract class BaseTestComponentRtl extends BaseTestComponent {

@Component({template: getTableTemplate(false)})
class MatResizeTest extends BaseTestComponent {
@ViewChild(MatColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
@ViewChild(MatColumnResize) columnResize: AbstractMatColumnResize;
}

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

@Component({template: getTableTemplate(true)})
class MatResizeDefaultTest extends BaseTestComponent {
@ViewChild(MatDefaultEnabledColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
@ViewChild(MatDefaultEnabledColumnResize) columnResize: AbstractMatColumnResize;
}

@Component({template: getTableTemplate(true)})
class MatResizeDefaultRtlTest extends BaseTestComponentRtl {
@ViewChild(MatDefaultEnabledColumnResize, {static: true}) columnResize: AbstractMatColumnResize;
@ViewChild(MatDefaultEnabledColumnResize) columnResize: AbstractMatColumnResize;
}

@Component({template: getFlexTemplate(false)})
class MatResizeFlexTest extends BaseTestComponent {
@ViewChild(MatColumnResizeFlex, {static: true}) columnResize: AbstractMatColumnResize;
@ViewChild(MatColumnResizeFlex) columnResize: AbstractMatColumnResize;
}

@Component({template: getFlexTemplate(true)})
class MatResizeDefaultFlexTest extends BaseTestComponent {
@ViewChild(MatDefaultEnabledColumnResizeFlex, {static: true})
@ViewChild(MatDefaultEnabledColumnResizeFlex)
columnResize: AbstractMatColumnResize;
}

@Component({template: getFlexTemplate(true)})
class MatResizeDefaultFlexRtlTest extends BaseTestComponentRtl {
@ViewChild(MatDefaultEnabledColumnResizeFlex, {static: true})
@ViewChild(MatDefaultEnabledColumnResizeFlex)
columnResize: AbstractMatColumnResize;
}

Expand Down
18 changes: 9 additions & 9 deletions src/material-experimental/mdc-table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ class MatTableApp {
columnsToRender = ['column_a', 'column_b', 'column_c'];
isFourthRow = (i: number, _rowData: TestData) => i == 3;

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatTable) table: MatTable<TestData>;
}

@Component({
Expand Down Expand Up @@ -669,7 +669,7 @@ class StickyTableApp {
dataSource = new FakeDataSource();
columnsToRender = ['column_a'];

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatTable) table: MatTable<TestData>;
}


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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatSort, {static: true}) sort: MatSort;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSortHeader) sortHeader: MatSortHeader;

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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatSort, {static: true}) sort: MatSort;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatSort) sort: MatSort;

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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatPaginator) paginator: MatPaginator;

constructor() {
this.underlyingDataSource.data = [];
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-tabs/tab-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class SimpleTabBodyApp implements AfterContentInit {
origin: number | null;

@ViewChild(MatTabBody) tabBody: MatTabBody;
@ViewChild(TemplateRef, {static: true}) template: TemplateRef<any>;
@ViewChild(TemplateRef) template: TemplateRef<any>;

constructor(private _viewContainerRef: ViewContainerRef) { }

Expand Down
4 changes: 2 additions & 2 deletions src/material/table/table-data-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('MatTableDataSource', () => {

beforeEach(() => {
fixture = TestBed.createComponent(MatSortApp);
sort = fixture.componentInstance.sort;
fixture.detectChanges();
sort = fixture.componentInstance.sort;
});

/** Test the data source's `sortData` function. */
Expand Down Expand Up @@ -58,5 +58,5 @@ describe('MatTableDataSource', () => {
template: `<div matSort matSortDirection="asc"></div>`
})
class MatSortApp {
@ViewChild(MatSort, {static: true}) sort: MatSort;
@ViewChild(MatSort) sort: MatSort;
}
20 changes: 10 additions & 10 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ class MatTableApp {
columnsToRender = ['column_a', 'column_b', 'column_c'];
isFourthRow = (i: number, _rowData: TestData) => i == 3;

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatTable) table: MatTable<TestData>;
}

@Component({
Expand Down Expand Up @@ -668,7 +668,7 @@ class NativeHtmlTableApp {
dataSource: FakeDataSource | null = new FakeDataSource();
columnsToRender = ['column_a', 'column_b', 'column_c'];

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatTable) table: MatTable<TestData>;
}

@Component({
Expand Down Expand Up @@ -736,7 +736,7 @@ class StickyTableApp {
dataSource = new FakeDataSource();
columnsToRender = ['column_a'];

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatTable) table: MatTable<TestData>;
}


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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatSort, {static: true}) sort: MatSort;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSortHeader) sortHeader: MatSortHeader;

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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatSort, {static: true}) sort: MatSort;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatSort) sort: MatSort;

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

@ViewChild(MatTable, {static: true}) table: MatTable<TestData>;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatTable) table: MatTable<TestData>;
@ViewChild(MatPaginator) paginator: MatPaginator;

constructor() {
this.underlyingDataSource.data = [];
Expand Down
2 changes: 1 addition & 1 deletion src/material/tabs/tab-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class SimpleTabBodyApp implements AfterContentInit {
origin: number | null;

@ViewChild(MatTabBody) tabBody: MatTabBody;
@ViewChild(TemplateRef, {static: true}) template: TemplateRef<any>;
@ViewChild(TemplateRef) template: TemplateRef<any>;

constructor(private _viewContainerRef: ViewContainerRef) { }

Expand Down