Skip to content

Commit

Permalink
fix(material/checkbox): account for disabledInteractive in harness
Browse files Browse the repository at this point in the history
Switches to using a CSS class to get the disabled state in the harness so it continues to work when `disabledInteractive` is set.
  • Loading branch information
crisbeto committed Aug 13, 2024
1 parent 5fa8101 commit 56b977f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/material/checkbox/testing/checkbox-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ describe('MatCheckboxHarness', () => {
await disabledCheckbox.toggle();
expect(await disabledCheckbox.isChecked()).toBe(false);
});

it('should get disabled state for checkbox with disabledInteractive', async () => {
fixture.componentInstance.disabled.set(false);
fixture.componentInstance.disabledInteractive.set(true);

const checkbox = await loader.getHarness(MatCheckboxHarness.with({label: 'Second'}));
expect(await checkbox.isDisabled()).toBe(false);

fixture.componentInstance.disabled.set(true);
expect(await checkbox.isDisabled()).toBe(true);
});
});

@Component({
Expand All @@ -179,7 +190,11 @@ describe('MatCheckboxHarness', () => {
aria-label="First checkbox">
First
</mat-checkbox>
<mat-checkbox indeterminate="true" [disabled]="disabled()" aria-labelledby="second-label">
<mat-checkbox
indeterminate="true"
[disabled]="disabled()"
aria-labelledby="second-label"
[disabledInteractive]="disabledInteractive()">
Second
</mat-checkbox>
<span id="second-label">Second checkbox</span>
Expand All @@ -190,4 +205,5 @@ describe('MatCheckboxHarness', () => {
class CheckboxHarnessTest {
ctrl = new FormControl(true);
disabled = signal(true);
disabledInteractive = signal(false);
}
10 changes: 8 additions & 2 deletions src/material/checkbox/testing/checkbox-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export class MatCheckboxHarness extends ComponentHarness {

/** Whether the checkbox is disabled. */
async isDisabled(): Promise<boolean> {
const disabled = (await this._input()).getAttribute('disabled');
return coerceBooleanProperty(await disabled);
const input = await this._input();
const disabled = await input.getAttribute('disabled');

if (disabled !== null) {
return coerceBooleanProperty(disabled);
}

return (await input.getAttribute('aria-disabled')) === 'true';
}

/** Whether the checkbox is required. */
Expand Down

0 comments on commit 56b977f

Please sign in to comment.