Skip to content

Commit 3619903

Browse files
feat(material/testing): Add icon name filtering to MatButtonHarness (angular#31852)
* feat(material/testing): Add icon name filter to MatButtonHarness * fix(material/testing): Update goldens/material/button/testing/index.api.md * refactor(material/testing): Rename MatButtonHarness "icon" filter to "iconName"
1 parent ff10f04 commit 3619903

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

goldens/material/button/testing/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {
1717
appearance?: ButtonAppearance;
1818
buttonType?: ButtonType;
1919
disabled?: boolean;
20+
iconName?: string | RegExp;
2021
text?: string | RegExp;
2122
variant?: ButtonVariant;
2223
}

src/material/button/testing/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ts_project(
1111
deps = [
1212
"//:node_modules/@angular/core",
1313
"//src/cdk/testing",
14+
"//src/material/icon/testing",
1415
],
1516
)
1617

src/material/button/testing/button-harness-filters.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {
3333

3434
/** Only find instances with the specified type. */
3535
buttonType?: ButtonType;
36+
37+
/** Only find instances that contain an icon whose name matches the given value. */
38+
iconName?: string | RegExp;
3639
}

src/material/button/testing/button-harness.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ describe('MatButtonHarness', () => {
115115
expect(await favIcon.getName()).toBe('favorite');
116116
});
117117

118+
it('should be able to filter buttons containing a named icon', async () => {
119+
const favBtn = await loader.getHarness(MatButtonHarness.with({iconName: 'favorite'}));
120+
121+
expect(await (await favBtn.host()).getAttribute('id')).toBe('favorite-icon');
122+
expect(await (await favBtn.getHarness(MatIconHarness)).getName()).toBe('favorite');
123+
});
124+
118125
it('should be able to ge the type variant of the button', async () => {
119126
const buttons = await loader.getAllHarnesses(MatButtonHarness);
120127
const variants = await parallel(() => buttons.map(button => button.getVariant()));

src/material/button/testing/button-harness.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ContentContainerComponentHarness,
1313
HarnessPredicate,
1414
} from '@angular/cdk/testing';
15+
import {MatIconHarness} from '@angular/material/icon/testing';
1516
import {
1617
ButtonAppearance,
1718
ButtonHarnessFilters,
@@ -58,7 +59,10 @@ export class MatButtonHarness extends ContentContainerComponentHarness {
5859
})
5960
.addOption('buttonType', options.buttonType, (harness, buttonType) =>
6061
HarnessPredicate.stringMatches(harness.getType(), buttonType),
61-
);
62+
)
63+
.addOption('iconName', options.iconName, (harness, iconName) => {
64+
return harness.hasHarness(MatIconHarness.with({name: iconName}));
65+
});
6266
}
6367

6468
/**

0 commit comments

Comments
 (0)