Skip to content

Commit c370e06

Browse files
authored
feat(material/tabs): add a harness filter for tab selected state (#25806)
1 parent baaf7c9 commit c370e06

File tree

9 files changed

+48
-14
lines changed

9 files changed

+48
-14
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import {MatLegacyTabsModule} from '@angular/material/legacy-tabs';
22
import {runTabGroupHarnessTests} from '@angular/material/tabs/testing/tab-group-shared.spec';
33
import {MatLegacyTabGroupHarness} from './tab-group-harness';
4+
import {MatLegacyTabHarness} from './tab-harness';
45

56
describe('Non-MDC-based MatTabGroupHarness', () => {
6-
runTabGroupHarnessTests(MatLegacyTabsModule, MatLegacyTabGroupHarness as any);
7+
runTabGroupHarnessTests(
8+
MatLegacyTabsModule,
9+
MatLegacyTabGroupHarness as any,
10+
MatLegacyTabHarness as any,
11+
);
712
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';
1515
export interface LegacyTabHarnessFilters extends BaseHarnessFilters {
1616
/** Only find instances whose label matches the given value. */
1717
label?: string | RegExp;
18+
/** Only find instances whose selected state matches the given value. */
19+
selected?: boolean;
1820
}
1921

2022
/**

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ export class MatLegacyTabHarness extends ContentContainerComponentHarness<string
2929
* @return a `HarnessPredicate` configured with the given options.
3030
*/
3131
static with(options: LegacyTabHarnessFilters = {}): HarnessPredicate<MatLegacyTabHarness> {
32-
return new HarnessPredicate(MatLegacyTabHarness, options).addOption(
33-
'label',
34-
options.label,
35-
(harness, label) => HarnessPredicate.stringMatches(harness.getLabel(), label),
36-
);
32+
return new HarnessPredicate(MatLegacyTabHarness, options)
33+
.addOption('label', options.label, (harness, label) =>
34+
HarnessPredicate.stringMatches(harness.getLabel(), label),
35+
)
36+
.addOption(
37+
'selected',
38+
options.selected,
39+
async (harness, selected) => (await harness.isSelected()) == selected,
40+
);
3741
}
3842

3943
/** Gets the label of the tab. */
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {MatTabsModule} from '@angular/material/tabs';
22
import {runTabGroupHarnessTests} from './tab-group-shared.spec';
33
import {MatTabGroupHarness} from './tab-group-harness';
4+
import {MatTabHarness} from './tab-harness';
45

56
describe('MDC-based MatTabGroupHarness', () => {
6-
runTabGroupHarnessTests(MatTabsModule, MatTabGroupHarness);
7+
runTabGroupHarnessTests(MatTabsModule, MatTabGroupHarness, MatTabHarness);
78
});

src/material/tabs/testing/tab-group-shared.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import {ComponentHarness, HarnessLoader} from '@angular/cdk/testing';
1+
import {ComponentHarness, HarnessLoader, parallel} from '@angular/cdk/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
33
import {Component} from '@angular/core';
44
import {ComponentFixture, TestBed} from '@angular/core/testing';
55
import {MatTabsModule} from '@angular/material/tabs';
66
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
77
import {MatTabGroupHarness} from './tab-group-harness';
8+
import {MatTabHarness} from './tab-harness';
89

910
/** Shared tests to run on both the original and MDC-based tab-group's. */
1011
export function runTabGroupHarnessTests(
1112
tabsModule: typeof MatTabsModule,
1213
tabGroupHarness: typeof MatTabGroupHarness,
14+
tabHarness: typeof MatTabHarness,
1315
) {
1416
let fixture: ComponentFixture<TabGroupHarnessTest>;
1517
let loader: HarnessLoader;
@@ -151,6 +153,16 @@ export function runTabGroupHarnessTests(
151153
expect(await tabs[1].isSelected()).toBe(false);
152154
expect(await tabs[2].isSelected()).toBe(true);
153155
});
156+
157+
it('should be able to get tabs by selected state', async () => {
158+
const selectedTabs = await loader.getAllHarnesses(tabHarness.with({selected: true}));
159+
const unselectedTabs = await loader.getAllHarnesses(tabHarness.with({selected: false}));
160+
expect(await parallel(() => selectedTabs.map(t => t.getLabel()))).toEqual(['First']);
161+
expect(await parallel(() => unselectedTabs.map(t => t.getLabel()))).toEqual([
162+
'Second',
163+
'Third',
164+
]);
165+
});
154166
}
155167

156168
@Component({

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
*/
88
import {BaseHarnessFilters} from '@angular/cdk/testing';
99

10-
/** A set of criteria that can be used to filter a list of `MatRadioButtonHarness` instances. */
10+
/** A set of criteria that can be used to filter a list of `MatTabHarness` instances. */
1111
export interface TabHarnessFilters extends BaseHarnessFilters {
1212
/** Only find instances whose label matches the given value. */
1313
label?: string | RegExp;
14+
/** Only find instances whose selected state matches the given value. */
15+
selected?: boolean;
1416
}
1517

16-
/** A set of criteria that can be used to filter a list of `MatRadioButtonHarness` instances. */
18+
/** A set of criteria that can be used to filter a list of `MatTabGroupHarness` instances. */
1719
export interface TabGroupHarnessFilters extends BaseHarnessFilters {
1820
/** Only find instances whose selected tab label matches the given value. */
1921
selectedTabLabel?: string | RegExp;
@@ -28,5 +30,5 @@ export interface TabLinkHarnessFilters extends BaseHarnessFilters {
2830
/** A set of criteria that can be used to filter a list of `MatTabNavBarHarness` instances. */
2931
export interface TabNavBarHarnessFilters extends BaseHarnessFilters {}
3032

31-
/** A set of criteria that can be used to filter a list of `MatTabNavBarHarness` instances. */
33+
/** A set of criteria that can be used to filter a list of `MatTabNavPanelHarness` instances. */
3234
export interface TabNavPanelHarnessFilters extends BaseHarnessFilters {}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ export class MatTabHarness extends ContentContainerComponentHarness<string> {
2828
this: ComponentHarnessConstructor<T>,
2929
options: TabHarnessFilters = {},
3030
): HarnessPredicate<T> {
31-
return new HarnessPredicate(this, options).addOption('label', options.label, (harness, label) =>
32-
HarnessPredicate.stringMatches(harness.getLabel(), label),
33-
);
31+
return new HarnessPredicate(this, options)
32+
.addOption('label', options.label, (harness, label) =>
33+
HarnessPredicate.stringMatches(harness.getLabel(), label),
34+
)
35+
.addOption(
36+
'selected',
37+
options.selected,
38+
async (harness, selected) => (await harness.isSelected()) == selected,
39+
);
3440
}
3541

3642
/** Gets the label of the tab. */

tools/public_api_guard/material/legacy-tabs-testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface LegacyTabGroupHarnessFilters extends BaseHarnessFilters {
1818
// @public @deprecated
1919
export interface LegacyTabHarnessFilters extends BaseHarnessFilters {
2020
label?: string | RegExp;
21+
selected?: boolean;
2122
}
2223

2324
// @public @deprecated

tools/public_api_guard/material/tabs-testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface TabGroupHarnessFilters extends BaseHarnessFilters {
6363
// @public
6464
export interface TabHarnessFilters extends BaseHarnessFilters {
6565
label?: string | RegExp;
66+
selected?: boolean;
6667
}
6768

6869
// @public

0 commit comments

Comments
 (0)