Skip to content

feat(material-experimental): option to filter tab-group harness by selected tab label #16813

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/

export type TabGroupHarnessFilters = {};
export type TabGroupHarnessFilters = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a doc string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. The boilerplate and other harnesses don't have a doc string for these either.

selectedTabLabel?: string|RegExp;
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ function runTests() {
expect(tabGroups.length).toBe(1);
});

it('should load harness for tab-group with selected tab label', async () => {
const tabGroups = await loader.getAllHarnesses(tabGroupHarness.with({
selectedTabLabel: 'First',
}));
expect(tabGroups.length).toBe(1);
});

it('should load harness for tab-group with matching tab label regex', async () => {
const tabGroups = await loader.getAllHarnesses(tabGroupHarness.with({
selectedTabLabel: /f.*st/i,
}));
expect(tabGroups.length).toBe(1);
});

it('should be able to get tabs of tab-group', async () => {
const tabGroup = await loader.getHarness(tabGroupHarness);
const tabs = await tabGroup.getTabs();
Expand Down
12 changes: 8 additions & 4 deletions src/material-experimental/mdc-tabs/harness/tab-group-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ export class MatTabGroupHarness extends ComponentHarness {
* Gets a `HarnessPredicate` that can be used to search for a radio-button with
* specific attributes.
* @param options Options for narrowing the search
* - `selectedTabLabel` finds a tab-group with a selected tab that matches the
* specified tab label.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: TabGroupHarnessFilters = {}): HarnessPredicate<MatTabGroupHarness> {
return new HarnessPredicate(MatTabGroupHarness);
return new HarnessPredicate(MatTabGroupHarness)
.addOption('selectedTabLabel', options.selectedTabLabel, async (harness, label) => {
const selectedTab = await harness.getSelectedTab();
return HarnessPredicate.stringMatches(await selectedTab.getLabel(), label);
});
}

private _tabs = this.locatorForAll(MatTabHarness);
Expand All @@ -39,12 +45,10 @@ export class MatTabGroupHarness extends ComponentHarness {
const tabs = await this.getTabs();
const isSelected = await Promise.all(tabs.map(t => t.isSelected()));
for (let i = 0; i < tabs.length; i++) {
if (isSelected[i]) {
if (isSelected[i]) {
return tabs[i];
}
}
throw new Error('No selected tab could be found.');
}
}