Skip to content

feat(cdk/testing): make MatButtonHarness extend ContentContainerCompo… #20653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {ButtonHarnessFilters} from '@angular/material/button/testing';


/** Harness for interacting with a MDC-based mat-button in tests. */
export class MatButtonHarness extends ComponentHarness {
export class MatButtonHarness extends ContentContainerComponentHarness {
// TODO(jelbourn) use a single class, like `.mat-button-base`
static hostSelector = [
'[mat-button]',
Expand Down
2 changes: 2 additions & 0 deletions src/material/button/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ ng_test_library(
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/button",
"//src/material/icon",
"//src/material/icon/testing",
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
],
Expand Down
4 changes: 2 additions & 2 deletions src/material/button/testing/button-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

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


/** Harness for interacting with a standard mat-button in tests. */
export class MatButtonHarness extends ComponentHarness {
export class MatButtonHarness extends ContentContainerComponentHarness {
// TODO(jelbourn) use a single class, like `.mat-button-base`
/** The selector for the host element of a `MatButton` instance. */
static hostSelector = [
Expand Down
24 changes: 21 additions & 3 deletions src/material/button/testing/shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {Component} from '@angular/core';
import {ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {MatButtonModule} from '@angular/material/button';
import {MatButtonHarness} from '@angular/material/button/testing/button-harness';
import {MatIconModule} from '@angular/material/icon';
import {MatIconHarness} from '@angular/material/icon/testing/icon-harness';

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

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [buttonModule, PlatformModule],
imports: [buttonModule, MatIconModule, PlatformModule],
declarations: [ButtonHarnessTest],
}).compileComponents();

Expand All @@ -30,7 +32,7 @@ export function runHarnessTests(

it('should load all button harnesses', async () => {
const buttons = await loader.getAllHarnesses(buttonHarness);
expect(buttons.length).toBe(14);
expect(buttons.length).toBe(15);
});

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

expect(fixture.componentInstance.clicked).toBe(false);
});

it('should be able to handle nested harnesses', async () => {
const homeBtn = await loader.getHarness(buttonHarness.with({selector: '#home-icon'}));
const favBtn = await loader.getHarness(buttonHarness.with({selector: '#favorite-icon'}));

const homeIcon = await homeBtn.getHarness(MatIconHarness);
const favIcon = await favBtn.getHarness(MatIconHarness);

expect(await homeIcon.getName()).toBe('home');
expect(await favIcon.getName()).toBe('favorite');
});
}

@Component({
Expand All @@ -110,7 +123,12 @@ export function runHarnessTests(
</button>
<button id="raised" type="button" mat-raised-button>Raised button</button>
<button id="stroked" type="button" mat-stroked-button>Stroked button</button>
<button id="icon" type="button" mat-icon-button>Icon button</button>
<button id="home-icon" type="button" mat-icon-button>
<mat-icon>home</mat-icon>
</button>
<button id="favorite-icon" type="button" mat-icon-button>
<mat-icon>favorite</mat-icon>
</button>
Comment on lines +126 to +131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't one of these test cases remain as a mat-icon-button without a mat-icon element inside?

Suggested change
<button id="home-icon" type="button" mat-icon-button>
<mat-icon>home</mat-icon>
</button>
<button id="favorite-icon" type="button" mat-icon-button>
<mat-icon>favorite</mat-icon>
</button>
<button id="home-icon" type="button" mat-icon-button>
<mat-icon>home</mat-icon>
</button>
<button id="favorite-icon" type="button" mat-icon-button>favorite</button>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to have two mat-icon-buttons with mat-icons inside so that we could make sure that the correct mat-icon was being returned.

Also, the mat-icon with text in it was being used for any tests. @mmalerba What are your thoughts here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the icon button with text in it was just a sloppy previously written test. I'm good with these changes

<button id="fab" type="button" mat-fab>Fab button</button>
<button id="mini-fab" type="button" mat-mini-fab>Mini Fab button</button>

Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/button/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {
text?: string | RegExp;
}

export declare class MatButtonHarness extends ComponentHarness {
export declare class MatButtonHarness extends ContentContainerComponentHarness {
blur(): Promise<void>;
click(relativeX: number, relativeY: number): Promise<void>;
click(location: 'center'): Promise<void>;
Expand Down