Skip to content

Commit f3b4d99

Browse files
committed
feat(material/testing): Add 'type' attribute filter and getter to MatButtonHarness
1 parent 94ab09a commit f3b4d99

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export type ButtonVariant = 'basic' | 'icon' | 'fab' | 'mini-fab';
1414
/** Possible button appearances. */
1515
export type ButtonAppearance = 'text' | 'filled' | 'elevated' | 'outlined' | 'tonal';
1616

17+
/** Possible button types. */
18+
export type ButtonType = 'button' | 'submit' | 'reset';
19+
1720
/** A set of criteria that can be used to filter a list of button harness instances. */
1821
export interface ButtonHarnessFilters extends BaseHarnessFilters {
1922
/** Only find instances whose text matches the given value. */
@@ -27,4 +30,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {
2730

2831
/** Only find instances which match the given disabled state. */
2932
disabled?: boolean;
33+
34+
/** Only find instances with the specified type. */
35+
type?: ButtonType;
3036
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('MatButtonHarness', () => {
2121

2222
it('should load all button harnesses', async () => {
2323
const buttons = await loader.getAllHarnesses(MatButtonHarness);
24-
expect(buttons.length).toBe(17);
24+
expect(buttons.length).toBe(18);
2525
});
2626

2727
it('should load button with exact text', async () => {
@@ -40,7 +40,7 @@ describe('MatButtonHarness', () => {
4040
it('should filter by whether a button is disabled', async () => {
4141
const enabledButtons = await loader.getAllHarnesses(MatButtonHarness.with({disabled: false}));
4242
const disabledButtons = await loader.getAllHarnesses(MatButtonHarness.with({disabled: true}));
43-
expect(enabledButtons.length).toBe(15);
43+
expect(enabledButtons.length).toBe(16);
4444
expect(disabledButtons.length).toBe(2);
4545
});
4646

@@ -59,6 +59,13 @@ describe('MatButtonHarness', () => {
5959
expect(await disabledElevatedAnchor.isDisabled()).toBe(true);
6060
});
6161

62+
it('should load button with type attribute', async () => {
63+
const buttons = await loader.getAllHarnesses(MatButtonHarness.with({type: 'submit'}));
64+
expect(buttons.length).toBe(1);
65+
expect(await buttons[0].getText()).toBe('Submit button');
66+
expect(await buttons[0].getType()).toBe('submit');
67+
});
68+
6269
it('should get button text', async () => {
6370
const [firstButton, secondButton] = await loader.getAllHarnesses(MatButtonHarness);
6471
expect(await firstButton.getText()).toBe('Basic button');
@@ -127,6 +134,7 @@ describe('MatButtonHarness', () => {
127134
'basic',
128135
'basic',
129136
'basic',
137+
'basic',
130138
'icon',
131139
'fab',
132140
'mini-fab',
@@ -148,6 +156,7 @@ describe('MatButtonHarness', () => {
148156
null,
149157
null,
150158
'text',
159+
'text',
151160
'filled',
152161
'elevated',
153162
'outlined',
@@ -199,6 +208,7 @@ describe('MatButtonHarness', () => {
199208
</button>
200209
<button id="fab" type="button" matFab>Fab button</button>
201210
<button id="mini-fab" type="button" matMiniFab>Mini Fab button</button>
211+
<button id="submit" type="submit" matButton>Submit button</button>
202212
203213
<a id="anchor-basic" matButton>Basic anchor</a>
204214
<a id="anchor-flat" matButton="filled">Filled anchor</a>

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
ContentContainerComponentHarness,
1313
HarnessPredicate,
1414
} from '@angular/cdk/testing';
15-
import {ButtonAppearance, ButtonHarnessFilters, ButtonVariant} from './button-harness-filters';
15+
import {
16+
ButtonAppearance,
17+
ButtonHarnessFilters,
18+
ButtonType,
19+
ButtonVariant,
20+
} from './button-harness-filters';
1621

1722
/** Harness for interacting with a mat-button in tests. */
1823
export class MatButtonHarness extends ContentContainerComponentHarness {
@@ -50,7 +55,10 @@ export class MatButtonHarness extends ContentContainerComponentHarness {
5055
)
5156
.addOption('disabled', options.disabled, async (harness, disabled) => {
5257
return (await harness.isDisabled()) === disabled;
53-
});
58+
})
59+
.addOption('type', options.type, (harness, type) =>
60+
HarnessPredicate.stringMatches(harness.getType(), type),
61+
);
5462
}
5563

5664
/**
@@ -150,4 +158,16 @@ export class MatButtonHarness extends ContentContainerComponentHarness {
150158

151159
return null;
152160
}
161+
162+
/**
163+
* Gets the type of the button. Supported values are 'button', 'submit', and 'reset'.
164+
*/
165+
async getType(): Promise<ButtonType | null> {
166+
const host = await this.host();
167+
const type = await host.getAttribute('type');
168+
if (type === 'button' || type === 'submit' || type === 'reset') {
169+
return type;
170+
}
171+
return null;
172+
}
153173
}

0 commit comments

Comments
 (0)