From a8faff1a51e42c0f4b72451cb3a8d856d7ebf0fa Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 1 Oct 2020 09:47:13 +0300 Subject: [PATCH] refactor(cdk/testing): simplify harnesses extending ContentContainerComponentHarness with a different root element We have some test harnesses that extend `ContentContainerComponentHarness`, but have to override all methods, because their content is in a different element. These changes clean up such usages by having a protected method for retrieving the harness root element that can be overwritten when necessary. --- src/cdk/testing/component-harness.ts | 24 ++++++++++++------- .../mdc-menu/testing/menu-harness.ts | 20 +--------------- .../mdc-tabs/testing/tab-harness.ts | 22 ++++------------- src/material/menu/testing/menu-harness.ts | 21 +--------------- src/material/stepper/testing/step-harness.ts | 21 +--------------- src/material/tabs/testing/tab-harness.ts | 22 ++++------------- tools/public_api_guard/cdk/testing.d.ts | 1 + .../material/menu/testing.d.ts | 5 +--- .../material/stepper/testing.d.ts | 5 +--- .../material/tabs/testing.d.ts | 5 +--- 10 files changed, 31 insertions(+), 115 deletions(-) diff --git a/src/cdk/testing/component-harness.ts b/src/cdk/testing/component-harness.ts index cd38c9060ddb..865b47c037cc 100644 --- a/src/cdk/testing/component-harness.ts +++ b/src/cdk/testing/component-harness.ts @@ -384,20 +384,28 @@ export abstract class ComponentHarness { export abstract class ContentContainerComponentHarness extends ComponentHarness implements HarnessLoader { - getChildLoader(selector: S): Promise { - return this.locatorFactory.harnessLoaderFor(selector); + async getChildLoader(selector: S): Promise { + return (await this.getRootHarnessLoader()).getChildLoader(selector); } - getAllChildLoaders(selector: S): Promise { - return this.locatorFactory.harnessLoaderForAll(selector); + async getAllChildLoaders(selector: S): Promise { + return (await this.getRootHarnessLoader()).getAllChildLoaders(selector); } - getHarness(query: HarnessQuery): Promise { - return this.locatorFor(query)(); + async getHarness(query: HarnessQuery): Promise { + return (await this.getRootHarnessLoader()).getHarness(query); } - getAllHarnesses(query: HarnessQuery): Promise { - return this.locatorForAll(query)(); + async getAllHarnesses(query: HarnessQuery): Promise { + return (await this.getRootHarnessLoader()).getAllHarnesses(query); + } + + /** + * Gets the root harness loader from which to start + * searching for content contained by this harness. + */ + protected async getRootHarnessLoader(): Promise { + return this.locatorFactory.rootHarnessLoader(); } } diff --git a/src/material-experimental/mdc-menu/testing/menu-harness.ts b/src/material-experimental/mdc-menu/testing/menu-harness.ts index a356df38c22f..5d860627135e 100644 --- a/src/material-experimental/mdc-menu/testing/menu-harness.ts +++ b/src/material-experimental/mdc-menu/testing/menu-harness.ts @@ -11,7 +11,6 @@ import { ContentContainerComponentHarness, HarnessLoader, HarnessPredicate, - HarnessQuery, TestElement, TestKey, } from '@angular/cdk/testing'; @@ -127,24 +126,7 @@ export class MatMenuHarness extends ContentContainerComponentHarness { return menu.clickItem(...subItemFilters as [Omit]); } - async getChildLoader(selector: string): Promise { - return (await this._getPanelLoader()).getChildLoader(selector); - } - - async getAllChildLoaders(selector: string): Promise { - return (await this._getPanelLoader()).getAllChildLoaders(selector); - } - - async getHarness(query: HarnessQuery): Promise { - return (await this._getPanelLoader()).getHarness(query); - } - - async getAllHarnesses(query: HarnessQuery): Promise { - return (await this._getPanelLoader()).getAllHarnesses(query); - } - - /** Gets the element id for the content of the current step. */ - private async _getPanelLoader(): Promise { + protected async getRootHarnessLoader(): Promise { const panelId = await this._getPanelId(); return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`); } diff --git a/src/material-experimental/mdc-tabs/testing/tab-harness.ts b/src/material-experimental/mdc-tabs/testing/tab-harness.ts index bfeaadd57f88..ef162227c4c1 100644 --- a/src/material-experimental/mdc-tabs/testing/tab-harness.ts +++ b/src/material-experimental/mdc-tabs/testing/tab-harness.ts @@ -7,11 +7,9 @@ */ import { - ComponentHarness, ContentContainerComponentHarness, HarnessLoader, HarnessPredicate, - HarnessQuery, } from '@angular/cdk/testing'; import {TabHarnessFilters} from './tab-harness-filters'; @@ -78,24 +76,12 @@ export class MatTabHarness extends ContentContainerComponentHarness { * @breaking-change 12.0.0 */ async getHarnessLoaderForContent(): Promise { - const contentId = await this._getContentId(); - return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`); - } - - async getChildLoader(selector: string): Promise { - return (await this.getHarnessLoaderForContent()).getChildLoader(selector); - } - - async getAllChildLoaders(selector: string): Promise { - return (await this.getHarnessLoaderForContent()).getAllChildLoaders(selector); - } - - async getHarness(query: HarnessQuery): Promise { - return (await this.getHarnessLoaderForContent()).getHarness(query); + return this.getRootHarnessLoader(); } - async getAllHarnesses(query: HarnessQuery): Promise { - return (await this.getHarnessLoaderForContent()).getAllHarnesses(query); + protected async getRootHarnessLoader(): Promise { + const contentId = await this._getContentId(); + return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`); } /** Gets the element id for the content of the current tab. */ diff --git a/src/material/menu/testing/menu-harness.ts b/src/material/menu/testing/menu-harness.ts index 2914dab4566f..978b3521278d 100644 --- a/src/material/menu/testing/menu-harness.ts +++ b/src/material/menu/testing/menu-harness.ts @@ -7,11 +7,9 @@ */ import { - ComponentHarness, ContentContainerComponentHarness, HarnessLoader, HarnessPredicate, - HarnessQuery, TestElement, TestKey, } from '@angular/cdk/testing'; @@ -127,24 +125,7 @@ export class MatMenuHarness extends ContentContainerComponentHarness { return menu.clickItem(...subItemFilters as [Omit]); } - async getChildLoader(selector: string): Promise { - return (await this._getPanelLoader()).getChildLoader(selector); - } - - async getAllChildLoaders(selector: string): Promise { - return (await this._getPanelLoader()).getAllChildLoaders(selector); - } - - async getHarness(query: HarnessQuery): Promise { - return (await this._getPanelLoader()).getHarness(query); - } - - async getAllHarnesses(query: HarnessQuery): Promise { - return (await this._getPanelLoader()).getAllHarnesses(query); - } - - /** Gets the element id for the content of the current step. */ - private async _getPanelLoader(): Promise { + protected async getRootHarnessLoader(): Promise { const panelId = await this._getPanelId(); return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`); } diff --git a/src/material/stepper/testing/step-harness.ts b/src/material/stepper/testing/step-harness.ts index 2d1881a26260..9568c9af3d0c 100644 --- a/src/material/stepper/testing/step-harness.ts +++ b/src/material/stepper/testing/step-harness.ts @@ -10,8 +10,6 @@ import { ContentContainerComponentHarness, HarnessPredicate, HarnessLoader, - ComponentHarness, - HarnessQuery, } from '@angular/cdk/testing'; import {StepHarnessFilters} from './step-harness-filters'; @@ -91,24 +89,7 @@ export class MatStepHarness extends ContentContainerComponentHarness { await (await this.host()).click(); } - async getChildLoader(selector: string): Promise { - return (await this._getContentLoader()).getChildLoader(selector); - } - - async getAllChildLoaders(selector: string): Promise { - return (await this._getContentLoader()).getAllChildLoaders(selector); - } - - async getHarness(query: HarnessQuery): Promise { - return (await this._getContentLoader()).getHarness(query); - } - - async getAllHarnesses(query: HarnessQuery): Promise { - return (await this._getContentLoader()).getAllHarnesses(query); - } - - /** Gets the harness loader for the content of the current step. */ - private async _getContentLoader(): Promise { + protected async getRootHarnessLoader(): Promise { const contentId = await (await this.host()).getAttribute('aria-controls'); return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`); } diff --git a/src/material/tabs/testing/tab-harness.ts b/src/material/tabs/testing/tab-harness.ts index 608d14c2e152..b18c032cd558 100644 --- a/src/material/tabs/testing/tab-harness.ts +++ b/src/material/tabs/testing/tab-harness.ts @@ -7,11 +7,9 @@ */ import { - ComponentHarness, ContentContainerComponentHarness, HarnessLoader, HarnessPredicate, - HarnessQuery, } from '@angular/cdk/testing'; import {TabHarnessFilters} from './tab-harness-filters'; @@ -78,24 +76,12 @@ export class MatTabHarness extends ContentContainerComponentHarness { * @breaking-change 12.0.0 */ async getHarnessLoaderForContent(): Promise { - const contentId = await this._getContentId(); - return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`); - } - - async getChildLoader(selector: string): Promise { - return (await this.getHarnessLoaderForContent()).getChildLoader(selector); - } - - async getAllChildLoaders(selector: string): Promise { - return (await this.getHarnessLoaderForContent()).getAllChildLoaders(selector); - } - - async getHarness(query: HarnessQuery): Promise { - return (await this.getHarnessLoaderForContent()).getHarness(query); + return this.getRootHarnessLoader(); } - async getAllHarnesses(query: HarnessQuery): Promise { - return (await this.getHarnessLoaderForContent()).getAllHarnesses(query); + protected async getRootHarnessLoader(): Promise { + const contentId = await this._getContentId(); + return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`); } /** Gets the element id for the content of the current tab. */ diff --git a/tools/public_api_guard/cdk/testing.d.ts b/tools/public_api_guard/cdk/testing.d.ts index 36db8f9ba98a..6d7e5912f8c2 100644 --- a/tools/public_api_guard/cdk/testing.d.ts +++ b/tools/public_api_guard/cdk/testing.d.ts @@ -38,6 +38,7 @@ export declare abstract class ContentContainerComponentHarness(query: HarnessQuery): Promise; getChildLoader(selector: S): Promise; getHarness(query: HarnessQuery): Promise; + protected getRootHarnessLoader(): Promise; } export interface ElementDimensions { diff --git a/tools/public_api_guard/material/menu/testing.d.ts b/tools/public_api_guard/material/menu/testing.d.ts index 16b43b1e94fd..501a2eef5ce6 100644 --- a/tools/public_api_guard/material/menu/testing.d.ts +++ b/tools/public_api_guard/material/menu/testing.d.ts @@ -3,11 +3,8 @@ export declare class MatMenuHarness extends ContentContainerComponentHarness, ...subItemFilters: Omit[]): Promise; close(): Promise; focus(): Promise; - getAllChildLoaders(selector: string): Promise; - getAllHarnesses(query: HarnessQuery): Promise; - getChildLoader(selector: string): Promise; - getHarness(query: HarnessQuery): Promise; getItems(filters?: Omit): Promise; + protected getRootHarnessLoader(): Promise; getTriggerText(): Promise; isDisabled(): Promise; isFocused(): Promise; diff --git a/tools/public_api_guard/material/stepper/testing.d.ts b/tools/public_api_guard/material/stepper/testing.d.ts index 9b90af85241e..27423655b0c7 100644 --- a/tools/public_api_guard/material/stepper/testing.d.ts +++ b/tools/public_api_guard/material/stepper/testing.d.ts @@ -1,11 +1,8 @@ export declare class MatStepHarness extends ContentContainerComponentHarness { - getAllChildLoaders(selector: string): Promise; - getAllHarnesses(query: HarnessQuery): Promise; getAriaLabel(): Promise; getAriaLabelledby(): Promise; - getChildLoader(selector: string): Promise; - getHarness(query: HarnessQuery): Promise; getLabel(): Promise; + protected getRootHarnessLoader(): Promise; hasErrors(): Promise; isCompleted(): Promise; isOptional(): Promise; diff --git a/tools/public_api_guard/material/tabs/testing.d.ts b/tools/public_api_guard/material/tabs/testing.d.ts index 41c7f0713dcd..1c9675f1baa6 100644 --- a/tools/public_api_guard/material/tabs/testing.d.ts +++ b/tools/public_api_guard/material/tabs/testing.d.ts @@ -7,14 +7,11 @@ export declare class MatTabGroupHarness extends ComponentHarness { } export declare class MatTabHarness extends ContentContainerComponentHarness { - getAllChildLoaders(selector: string): Promise; - getAllHarnesses(query: HarnessQuery): Promise; getAriaLabel(): Promise; getAriaLabelledby(): Promise; - getChildLoader(selector: string): Promise; - getHarness(query: HarnessQuery): Promise; getHarnessLoaderForContent(): Promise; getLabel(): Promise; + protected getRootHarnessLoader(): Promise; getTextContent(): Promise; isDisabled(): Promise; isSelected(): Promise;