Skip to content

Commit 0860c45

Browse files
committed
fix(material/icon): harness returning wrong name if icon has other content
Fixes that the badge was returning all of its `textContent` for the name which may include other elements (e.g. badges). Only the text directly inside of the icon determines its name. Fixes #27015.
1 parent 3cb369e commit 0860c45

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class MatIconHarness extends ComponentHarness {
5050
// Some icons support defining the icon as a ligature.
5151
// As a fallback, try to extract it from the DOM text.
5252
if ((await this.getType()) === IconType.FONT) {
53-
return host.text();
53+
// Other directives may add content to the icon (e.g. `MatBadge`), however only the direct
54+
// text nodes affect the name of the icon. Exclude all element descendants from the result.
55+
return host.text({exclude: '*'});
5456
}
5557

5658
return null;

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function runHarnessTests(
3737

3838
it('should load all icon harnesses', async () => {
3939
const icons = await loader.getAllHarnesses(iconHarness);
40-
expect(icons.length).toBe(4);
40+
expect(icons.length).toBe(5);
4141
});
4242

4343
it('should filter icon harnesses based on their type', async () => {
@@ -47,7 +47,7 @@ export function runHarnessTests(
4747
]);
4848

4949
expect(svgIcons.length).toBe(1);
50-
expect(fontIcons.length).toBe(3);
50+
expect(fontIcons.length).toBe(4);
5151
});
5252

5353
it('should filter icon harnesses based on their name', async () => {
@@ -69,31 +69,43 @@ export function runHarnessTests(
6969

7070
expect(regexFilterResults.length).toBe(1);
7171
expect(stringFilterResults.length).toBe(1);
72-
expect(nullFilterResults.length).toBe(2);
72+
expect(nullFilterResults.length).toBe(3);
7373
});
7474

7575
it('should get the type of each icon', async () => {
7676
const icons = await loader.getAllHarnesses(iconHarness);
7777
const types = await parallel(() => icons.map(icon => icon.getType()));
78-
expect(types).toEqual([IconType.FONT, IconType.SVG, IconType.FONT, IconType.FONT]);
78+
expect(types).toEqual([
79+
IconType.FONT,
80+
IconType.SVG,
81+
IconType.FONT,
82+
IconType.FONT,
83+
IconType.FONT,
84+
]);
7985
});
8086

8187
it('should get the name of an icon', async () => {
8288
const icons = await loader.getAllHarnesses(iconHarness);
8389
const names = await parallel(() => icons.map(icon => icon.getName()));
84-
expect(names).toEqual(['fontIcon', 'svgIcon', 'ligature_icon', 'ligature_icon_by_attribute']);
90+
expect(names).toEqual([
91+
'fontIcon',
92+
'svgIcon',
93+
'ligature_icon',
94+
'ligature_icon_by_attribute',
95+
'ligature_icon_with_additional_content',
96+
]);
8597
});
8698

8799
it('should get the namespace of an icon', async () => {
88100
const icons = await loader.getAllHarnesses(iconHarness);
89101
const namespaces = await parallel(() => icons.map(icon => icon.getNamespace()));
90-
expect(namespaces).toEqual(['fontIcons', 'svgIcons', null, null]);
102+
expect(namespaces).toEqual(['fontIcons', 'svgIcons', null, null, null]);
91103
});
92104

93105
it('should get whether an icon is inline', async () => {
94106
const icons = await loader.getAllHarnesses(iconHarness);
95107
const inlineStates = await parallel(() => icons.map(icon => icon.isInline()));
96-
expect(inlineStates).toEqual([false, false, true, false]);
108+
expect(inlineStates).toEqual([false, false, true, false, false]);
97109
});
98110
}
99111

@@ -103,6 +115,7 @@ export function runHarnessTests(
103115
<mat-icon svgIcon="svgIcons:svgIcon"></mat-icon>
104116
<mat-icon inline>ligature_icon</mat-icon>
105117
<mat-icon fontIcon="ligature_icon_by_attribute"></mat-icon>
118+
<mat-icon>ligature_icon_with_additional_content <span class="fake-badge">Hello</span></mat-icon>
106119
`,
107120
})
108121
class IconHarnessTest {}

0 commit comments

Comments
 (0)