Skip to content

Commit 8d337ef

Browse files
committed
feat(material-experimental): option to filter tab-group harness by selected tab
Follow-up that addresses feedback from the original tab-group harness PR. See: #16728#discussion_r314819393
1 parent 37086fd commit 8d337ef

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/material-experimental/mdc-tabs/harness/tab-group-harness-filters.ts

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

9-
export type TabGroupHarnessFilters = {};
9+
export type TabGroupHarnessFilters = {
10+
selectedTabLabel?: string|RegExp;
11+
};

src/material-experimental/mdc-tabs/harness/tab-group-harness.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ function runTests() {
4343
expect(tabGroups.length).toBe(1);
4444
});
4545

46+
it('should load harness for tab-group with selected tab label', async () => {
47+
const tabGroups = await loader.getAllHarnesses(tabGroupHarness.with({
48+
selectedTabLabel: 'First',
49+
}));
50+
expect(tabGroups.length).toBe(1);
51+
});
52+
53+
it('should load harness for tab-group with matching tab label regex', async () => {
54+
const tabGroups = await loader.getAllHarnesses(tabGroupHarness.with({
55+
selectedTabLabel: /f.*st/,
56+
}));
57+
expect(tabGroups.length).toBe(1);
58+
});
59+
4660
it('should be able to get tabs of tab-group', async () => {
4761
const tabGroup = await loader.getHarness(tabGroupHarness);
4862
const tabs = await tabGroup.getTabs();

src/material-experimental/mdc-tabs/harness/tab-group-harness.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ export class MatTabGroupHarness extends ComponentHarness {
2121
* Gets a `HarnessPredicate` that can be used to search for a radio-button with
2222
* specific attributes.
2323
* @param options Options for narrowing the search
24+
* - `selectedTabLabel` finds a tab-group with a selected tab that matches the
25+
* specified tab label.
2426
* @return a `HarnessPredicate` configured with the given options.
2527
*/
2628
static with(options: TabGroupHarnessFilters = {}): HarnessPredicate<MatTabGroupHarness> {
27-
return new HarnessPredicate(MatTabGroupHarness);
29+
return new HarnessPredicate(MatTabGroupHarness)
30+
.addOption('selectedTabLabel', options.selectedTabLabel, async (harness, label) => {
31+
const selectedTab = await harness.getSelectedTab();
32+
return HarnessPredicate.stringMatches(await selectedTab.getLabel(), label);
33+
});
2834
}
2935

3036
private _tabs = this.locatorForAll(MatTabHarness);
@@ -39,12 +45,10 @@ export class MatTabGroupHarness extends ComponentHarness {
3945
const tabs = await this.getTabs();
4046
const isSelected = await Promise.all(tabs.map(t => t.isSelected()));
4147
for (let i = 0; i < tabs.length; i++) {
42-
if (isSelected[i]) {
48+
if (isSelected[i]) {
4349
return tabs[i];
4450
}
4551
}
4652
throw new Error('No selected tab could be found.');
4753
}
4854
}
49-
50-

0 commit comments

Comments
 (0)