From aaf0d51569c0a5626055ca61663d6dbe9fbd1776 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 12 Aug 2024 09:53:27 +0200 Subject: [PATCH] fix(material/checkbox): account for disabledInteractive in harness Switches to using a CSS class to get the disabled state in the harness so it continues to work when `disabledInteractive` is set. (cherry picked from commit 56b977f9fd911bb4a83094d41338753ea304afb8) --- .../checkbox/testing/checkbox-harness.spec.ts | 18 +++++++++++++++++- .../checkbox/testing/checkbox-harness.ts | 10 ++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/material/checkbox/testing/checkbox-harness.spec.ts b/src/material/checkbox/testing/checkbox-harness.spec.ts index dadfad5e56af..5d5d9fc2fcbc 100644 --- a/src/material/checkbox/testing/checkbox-harness.spec.ts +++ b/src/material/checkbox/testing/checkbox-harness.spec.ts @@ -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({ @@ -179,7 +190,11 @@ describe('MatCheckboxHarness', () => { aria-label="First checkbox"> First - + Second Second checkbox @@ -190,4 +205,5 @@ describe('MatCheckboxHarness', () => { class CheckboxHarnessTest { ctrl = new FormControl(true); disabled = signal(true); + disabledInteractive = signal(false); } diff --git a/src/material/checkbox/testing/checkbox-harness.ts b/src/material/checkbox/testing/checkbox-harness.ts index f81e1c1c1f9e..9a0f07ec601d 100644 --- a/src/material/checkbox/testing/checkbox-harness.ts +++ b/src/material/checkbox/testing/checkbox-harness.ts @@ -72,8 +72,14 @@ export class MatCheckboxHarness extends ComponentHarness { /** Whether the checkbox is disabled. */ async isDisabled(): Promise { - 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. */