Skip to content

Commit 8cacf64

Browse files
authored
feat(cdk/testing): make MatButtonHarness extend ContentContainerComponentHarness (#20653)
* Allows users to access mat-icons within mat-buttons
1 parent a14eeb4 commit 8cacf64

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
9+
import {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {ButtonHarnessFilters} from '@angular/material/button/testing';
1212

1313

1414
/** Harness for interacting with a MDC-based mat-button in tests. */
15-
export class MatButtonHarness extends ComponentHarness {
15+
export class MatButtonHarness extends ContentContainerComponentHarness {
1616
// TODO(jelbourn) use a single class, like `.mat-button-base`
1717
static hostSelector = [
1818
'[mat-button]',

src/material/button/testing/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ ng_test_library(
3030
"//src/cdk/testing",
3131
"//src/cdk/testing/testbed",
3232
"//src/material/button",
33+
"//src/material/icon",
34+
"//src/material/icon/testing",
3335
"@npm//@angular/forms",
3436
"@npm//@angular/platform-browser",
3537
],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
9+
import {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1111
import {ButtonHarnessFilters} from './button-harness-filters';
1212

1313

1414
/** Harness for interacting with a standard mat-button in tests. */
15-
export class MatButtonHarness extends ComponentHarness {
15+
export class MatButtonHarness extends ContentContainerComponentHarness {
1616
// TODO(jelbourn) use a single class, like `.mat-button-base`
1717
/** The selector for the host element of a `MatButton` instance. */
1818
static hostSelector = [

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {Component} from '@angular/core';
55
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
66
import {MatButtonModule} from '@angular/material/button';
77
import {MatButtonHarness} from '@angular/material/button/testing/button-harness';
8+
import {MatIconModule} from '@angular/material/icon';
9+
import {MatIconHarness} from '@angular/material/icon/testing/icon-harness';
810

911
/** Shared tests to run on both the original and MDC-based buttons. */
1012
export function runHarnessTests(
@@ -15,7 +17,7 @@ export function runHarnessTests(
1517

1618
beforeEach(async () => {
1719
await TestBed.configureTestingModule({
18-
imports: [buttonModule, PlatformModule],
20+
imports: [buttonModule, MatIconModule, PlatformModule],
1921
declarations: [ButtonHarnessTest],
2022
}).compileComponents();
2123

@@ -30,7 +32,7 @@ export function runHarnessTests(
3032

3133
it('should load all button harnesses', async () => {
3234
const buttons = await loader.getAllHarnesses(buttonHarness);
33-
expect(buttons.length).toBe(14);
35+
expect(buttons.length).toBe(15);
3436
});
3537

3638
it('should load button with exact text', async () => {
@@ -96,6 +98,17 @@ export function runHarnessTests(
9698

9799
expect(fixture.componentInstance.clicked).toBe(false);
98100
});
101+
102+
it('should be able to handle nested harnesses', async () => {
103+
const homeBtn = await loader.getHarness(buttonHarness.with({selector: '#home-icon'}));
104+
const favBtn = await loader.getHarness(buttonHarness.with({selector: '#favorite-icon'}));
105+
106+
const homeIcon = await homeBtn.getHarness(MatIconHarness);
107+
const favIcon = await favBtn.getHarness(MatIconHarness);
108+
109+
expect(await homeIcon.getName()).toBe('home');
110+
expect(await favIcon.getName()).toBe('favorite');
111+
});
99112
}
100113

101114
@Component({
@@ -110,7 +123,12 @@ export function runHarnessTests(
110123
</button>
111124
<button id="raised" type="button" mat-raised-button>Raised button</button>
112125
<button id="stroked" type="button" mat-stroked-button>Stroked button</button>
113-
<button id="icon" type="button" mat-icon-button>Icon button</button>
126+
<button id="home-icon" type="button" mat-icon-button>
127+
<mat-icon>home</mat-icon>
128+
</button>
129+
<button id="favorite-icon" type="button" mat-icon-button>
130+
<mat-icon>favorite</mat-icon>
131+
</button>
114132
<button id="fab" type="button" mat-fab>Fab button</button>
115133
<button id="mini-fab" type="button" mat-mini-fab>Mini Fab button</button>
116134

tools/public_api_guard/material/button/testing.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {
22
text?: string | RegExp;
33
}
44

5-
export declare class MatButtonHarness extends ComponentHarness {
5+
export declare class MatButtonHarness extends ContentContainerComponentHarness {
66
blur(): Promise<void>;
77
click(relativeX: number, relativeY: number): Promise<void>;
88
click(location: 'center'): Promise<void>;

0 commit comments

Comments
 (0)