Skip to content

Commit

Permalink
test(material/button-toggle): add disabled harness filter (angular#26139
Browse files Browse the repository at this point in the history
)

* test(material/button-toggle): add disabled harness filter

* fixup! test(material/button-toggle): add disabled harness filter
  • Loading branch information
wagnermaciel committed Dec 1, 2022
1 parent f504fae commit 6f35599
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
import {BaseHarnessFilters} from '@angular/cdk/testing';

/** Criteria that can be used to filter a list of `MatButtonToggleGroupHarness` instances. */
export interface ButtonToggleGroupHarnessFilters extends BaseHarnessFilters {}
export interface ButtonToggleGroupHarnessFilters extends BaseHarnessFilters {
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export class MatButtonToggleGroupHarness extends ComponentHarness {
static with(
options: ButtonToggleGroupHarnessFilters = {},
): HarnessPredicate<MatButtonToggleGroupHarness> {
return new HarnessPredicate(MatButtonToggleGroupHarness, options);
return new HarnessPredicate(MatButtonToggleGroupHarness, options).addOption(
'disabled',
options.disabled,
async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
},
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ export function runHarnessTests(
expect(await group.isDisabled()).toBe(true);
});

it('should filter by whether the group is disabled', async () => {
let enabledGroups = await loader.getAllHarnesses(
buttonToggleGroupHarness.with({disabled: false}),
);
let disabledGroups = await loader.getAllHarnesses(
buttonToggleGroupHarness.with({disabled: true}),
);
expect(enabledGroups.length).toBe(1);
expect(disabledGroups.length).toBe(0);

fixture.componentInstance.disabled = true;

enabledGroups = await loader.getAllHarnesses(buttonToggleGroupHarness.with({disabled: false}));
disabledGroups = await loader.getAllHarnesses(buttonToggleGroupHarness.with({disabled: true}));
expect(enabledGroups.length).toBe(0);
expect(disabledGroups.length).toBe(1);
});

it('should get whether the group is vertical', async () => {
const group = await loader.getHarness(buttonToggleGroupHarness);
expect(await group.isVertical()).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export interface ButtonToggleHarnessFilters extends BaseHarnessFilters {
name?: string | RegExp;
/** Only find instances that are checked. */
checked?: boolean;
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}
5 changes: 4 additions & 1 deletion src/material/button-toggle/testing/button-toggle-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export class MatButtonToggleHarness extends ComponentHarness {
'checked',
options.checked,
async (harness, checked) => (await harness.isChecked()) === checked,
);
)
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}

/** Gets a boolean promise indicating if the button toggle is checked. */
Expand Down
11 changes: 11 additions & 0 deletions src/material/button-toggle/testing/button-toggle-shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export function runHarnessTests(
expect(await uncheckedToggle.isChecked()).toBe(false);
});

it('should filter by whether the group is disabled', async () => {
const enabledToggles = await loader.getAllHarnesses(
buttonToggleHarness.with({disabled: false}),
);
const disabledToggles = await loader.getAllHarnesses(
buttonToggleHarness.with({disabled: true}),
);
expect(enabledToggles.length).toBe(1);
expect(disabledToggles.length).toBe(1);
});

it('should get the toggle disabled state', async () => {
const [enabledToggle, disabledToggle] = await loader.getAllHarnesses(buttonToggleHarness);
expect(await enabledToggle.isDisabled()).toBe(false);
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/material/button-toggle-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { MatButtonToggleAppearance } from '@angular/material/button-toggle';

// @public
export interface ButtonToggleGroupHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
}

// @public
export interface ButtonToggleHarnessFilters extends BaseHarnessFilters {
checked?: boolean;
disabled?: boolean;
name?: string | RegExp;
text?: string | RegExp;
}
Expand Down

0 comments on commit 6f35599

Please sign in to comment.