Skip to content

Commit 2562e12

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 2562e12

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ 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+
const text = await host.text({exclude: '*'});
56+
57+
// There are some internal cases where the icon name is wrapped in another node.
58+
// Fall back to extracting the entire text if we ended up excluding everything above.
59+
return text.length > 0 ? text : host.text();
5460
}
5561

5662
return null;

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

Lines changed: 23 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(6);
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(5);
5151
});
5252

5353
it('should filter icon harnesses based on their name', async () => {
@@ -69,31 +69,45 @@ 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(4);
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+
IconType.FONT,
85+
]);
7986
});
8087

8188
it('should get the name of an icon', async () => {
8289
const icons = await loader.getAllHarnesses(iconHarness);
8390
const names = await parallel(() => icons.map(icon => icon.getName()));
84-
expect(names).toEqual(['fontIcon', 'svgIcon', 'ligature_icon', 'ligature_icon_by_attribute']);
91+
expect(names).toEqual([
92+
'fontIcon',
93+
'svgIcon',
94+
'ligature_icon',
95+
'ligature_icon_by_attribute',
96+
'ligature_icon_with_additional_content',
97+
'ligature_icon_with_indirect_name',
98+
]);
8599
});
86100

87101
it('should get the namespace of an icon', async () => {
88102
const icons = await loader.getAllHarnesses(iconHarness);
89103
const namespaces = await parallel(() => icons.map(icon => icon.getNamespace()));
90-
expect(namespaces).toEqual(['fontIcons', 'svgIcons', null, null]);
104+
expect(namespaces).toEqual(['fontIcons', 'svgIcons', null, null, null, null]);
91105
});
92106

93107
it('should get whether an icon is inline', async () => {
94108
const icons = await loader.getAllHarnesses(iconHarness);
95109
const inlineStates = await parallel(() => icons.map(icon => icon.isInline()));
96-
expect(inlineStates).toEqual([false, false, true, false]);
110+
expect(inlineStates).toEqual([false, false, true, false, false, false]);
97111
});
98112
}
99113

@@ -103,6 +117,8 @@ export function runHarnessTests(
103117
<mat-icon svgIcon="svgIcons:svgIcon"></mat-icon>
104118
<mat-icon inline>ligature_icon</mat-icon>
105119
<mat-icon fontIcon="ligature_icon_by_attribute"></mat-icon>
120+
<mat-icon>ligature_icon_with_additional_content <span class="fake-badge">Hello</span></mat-icon>
121+
<mat-icon><span>ligature_icon_with_indirect_name</span></mat-icon>
106122
`,
107123
})
108124
class IconHarnessTest {}

0 commit comments

Comments
 (0)