Skip to content

Commit 497e7eb

Browse files
committed
replace getSelectorForContent with getHarnessLoaderForContent
1 parent 72bceae commit 497e7eb

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

src/cdk/testing/component-harness.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ export interface LocatorFactory {
143143
locatorForAll<T extends ComponentHarness>(
144144
harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): AsyncFactoryFn<T[]>;
145145

146+
/**
147+
* Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`.
148+
* @param selector The selector for the root element.
149+
* @return A `HarnessLoader` rooted at the first element matching the given selector.
150+
* @throws If no matching element is found for the given selector.
151+
*/
152+
harnessLoaderFor(selector: string): Promise<HarnessLoader>;
153+
154+
/**
155+
* Gets a `HarnessLoader` instance for an element under the root of this `LocatorFactory`
156+
* @param selector The selector for the root element.
157+
* @return A `HarnessLoader` rooted at the first element matching the given selector, or null if
158+
* no matching element is found.
159+
*/
160+
harnessLoaderForOptional(selector: string): Promise<HarnessLoader | null>;
161+
162+
/**
163+
* Gets a list of `HarnessLoader` instances, one for each matching element.
164+
* @param selector The selector for the root element.
165+
* @return A list of `HarnessLoader`, one rooted at each element matching the given selector.
166+
*/
167+
harnessLoaderForAll(selector: string): Promise<HarnessLoader[]>;
168+
146169
/**
147170
* Flushes change detection and async tasks.
148171
* In most cases it should not be necessary to call this manually. However, there may be some edge

src/cdk/testing/harness-environment.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
8282
};
8383
}
8484

85+
// Implemented as part of the `LocatorFactory` interface.
86+
async harnessLoaderFor(selector: string): Promise<HarnessLoader> {
87+
return this.createEnvironment(await this._assertElementFound(selector));
88+
}
89+
90+
// Implemented as part of the `LocatorFactory` interface.
91+
async harnessLoaderForOptional(selector: string): Promise<HarnessLoader | null> {
92+
const elements = await this.getAllRawElements(selector);
93+
return elements[0] && this.createEnvironment(elements[0]);
94+
}
95+
96+
// Implemented as part of the `LocatorFactory` interface.
97+
async harnessLoaderForAll(selector: string): Promise<HarnessLoader[]> {
98+
const elements = await this.getAllRawElements(selector);
99+
return elements.map(element => this.createEnvironment(element));
100+
}
101+
85102
// Implemented as part of the `HarnessLoader` interface.
86103
getHarness<T extends ComponentHarness>(
87104
harnessType: ComponentHarnessConstructor<T> | HarnessPredicate<T>): Promise<T> {

src/material/tabs/testing/shared.spec.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectAsyncError} from '@angular/cdk/private/testing';
2-
import {HarnessLoader} from '@angular/cdk/testing';
2+
import {ComponentHarness, HarnessLoader} from '@angular/cdk/testing';
33
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
44
import {Component} from '@angular/core';
55
import {ComponentFixture, TestBed} from '@angular/core/testing';
@@ -96,12 +96,12 @@ export function runHarnessTests(
9696
expect(await tabs[2].getAriaLabelledby()).toBe('tabLabelId');
9797
});
9898

99-
it('should be able to get content element of active tab', async () => {
99+
it('should be able to get harness loader for content element of active tab', async () => {
100100
const tabGroup = await loader.getHarness(tabGroupHarness);
101101
const tabs = await tabGroup.getTabs();
102-
const contentSelector = await tabs[0].getSelectorForContent();
103-
const contentEl = document.querySelector(contentSelector) as HTMLElement;
104-
expect(contentEl.innerText.trim()).toBe('Content 1');
102+
const tabContentLoader = await tabs[0].getHarnessLoaderForContent();
103+
const tabContentHarness = await tabContentLoader.getHarness(TestTabContentHarness);
104+
expect(await (await tabContentHarness.host()).text()).toBe('Content 1');
105105
});
106106

107107
it('should be able to get disabled state of tab', async () => {
@@ -153,15 +153,23 @@ export function runHarnessTests(
153153
@Component({
154154
template: `
155155
<mat-tab-group>
156-
<mat-tab label="First" aria-label="First tab">Content 1</mat-tab>
157-
<mat-tab label="Second" aria-label="Second tab">Content 2</mat-tab>
156+
<mat-tab label="First" aria-label="First tab">
157+
<span class="test-tab-content">Content 1</span>
158+
</mat-tab>
159+
<mat-tab label="Second" aria-label="Second tab">
160+
<span class="test-tab-content">Content 2</span>
161+
</mat-tab>
158162
<mat-tab label="Third" aria-labelledby="tabLabelId" [disabled]="isDisabled">
159163
<ng-template matTabLabel>Third</ng-template>
160-
Content 3
164+
<span class="test-tab-content">Content 3</span>
161165
</mat-tab>
162166
</mat-tab-group>
163167
`
164168
})
165169
class TabGroupHarnessTest {
166170
isDisabled = false;
167171
}
172+
173+
class TestTabContentHarness extends ComponentHarness {
174+
static hostSelector = '.test-tab-content';
175+
}

src/material/tabs/testing/tab-harness.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate, TestElement} from '@angular/cdk/testing';
9+
import {ComponentHarness, HarnessLoader, HarnessPredicate} from '@angular/cdk/testing';
1010
import {TabHarnessFilters} from './tab-harness-filters';
1111

1212
/**
@@ -25,8 +25,6 @@ export class MatTabHarness extends ComponentHarness {
2525
(harness, label) => HarnessPredicate.stringMatches(harness.getLabel(), label));
2626
}
2727

28-
private _rootLocatorFactory = this.documentRootLocatorFactory();
29-
3028
/** Gets the label of the tab. */
3129
async getLabel(): Promise<string> {
3230
return (await this.host()).text();
@@ -62,10 +60,15 @@ export class MatTabHarness extends ComponentHarness {
6260
await (await this.host()).click();
6361
}
6462

65-
/** Gets a selector that can be used to locate the tab's content element. */
66-
async getSelectorForContent(): Promise<string> {
63+
async getHarnessLoaderForContent(): Promise<HarnessLoader> {
64+
const contentId = await this._getContentId();
65+
return this.documentRootLocatorFactory().harnessLoaderFor(`#${contentId}`);
66+
}
67+
68+
/** Gets the element id for the content of the current tab. */
69+
private async _getContentId(): Promise<string> {
6770
const hostEl = await this.host();
6871
// Tabs never have an empty "aria-controls" attribute.
69-
return '#' + await hostEl.getAttribute('aria-controls');
72+
return (await hostEl.getAttribute('aria-controls'))!;
7073
}
7174
}

0 commit comments

Comments
 (0)