Skip to content

test(material/button): add disabled harness filter #26138

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 3 commits into from
Dec 1, 2022
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
3 changes: 3 additions & 0 deletions src/material/button/testing/button-harness-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ export interface ButtonHarnessFilters extends BaseHarnessFilters {

/** Only find instances with a variant. */
variant?: ButtonVariant;

/** Only find instances which match the given disabled state. */
disabled?: boolean;
}
5 changes: 4 additions & 1 deletion src/material/button/testing/button-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export class MatButtonHarness extends ContentContainerComponentHarness {
)
.addOption('variant', options.variant, (harness, variant) =>
HarnessPredicate.stringMatches(harness.getVariant(), variant),
);
)
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/material/button/testing/shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export function runHarnessTests(
expect(await buttons[1].getText()).toBe('Basic anchor');
});

it('should filter by whether a button is disabled', async () => {
const enabledButtons = await loader.getAllHarnesses(buttonHarness.with({disabled: false}));
const disabledButtons = await loader.getAllHarnesses(buttonHarness.with({disabled: true}));
expect(enabledButtons.length).toBe(13);
expect(disabledButtons.length).toBe(2);
});

it('should get disabled state', async () => {
// Grab each combination of [enabled, disabled] ⨯ [button, anchor]
const [disabledFlatButton, enabledFlatAnchor] = await loader.getAllHarnesses(
Expand Down
5 changes: 4 additions & 1 deletion src/material/legacy-button/testing/button-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export class MatLegacyButtonHarness extends ContentContainerComponentHarness {
)
.addOption('variant', options.variant, (harness, variant) =>
HarnessPredicate.stringMatches(harness.getVariant(), variant),
);
)
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/button-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { HarnessPredicate } from '@angular/cdk/testing';

// @public
export interface ButtonHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
text?: string | RegExp;
variant?: ButtonVariant;
}
Expand Down