UNPKG

@angular/material

Version:
1 lines 9.92 kB
{"version":3,"file":"checkbox-testing.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/material/checkbox/testing/checkbox-harness.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {\n ComponentHarness,\n ComponentHarnessConstructor,\n HarnessPredicate,\n} from '@angular/cdk/testing';\nimport {CheckboxHarnessFilters} from './checkbox-harness-filters';\nimport {coerceBooleanProperty} from '@angular/cdk/coercion';\n\n/** Harness for interacting with a mat-checkbox in tests. */\nexport class MatCheckboxHarness extends ComponentHarness {\n static hostSelector = '.mat-mdc-checkbox';\n\n _input = this.locatorFor('input');\n private _label = this.locatorFor('label');\n private _inputContainer = this.locatorFor('.mdc-checkbox');\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a checkbox with specific attributes.\n * @param options Options for narrowing the search:\n * - `selector` finds a checkbox whose host element matches the given selector.\n * - `label` finds a checkbox with specific label text.\n * - `name` finds a checkbox with specific name.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatCheckboxHarness>(\n this: ComponentHarnessConstructor<T>,\n options: CheckboxHarnessFilters = {},\n ): HarnessPredicate<T> {\n return (\n new HarnessPredicate(this, options)\n .addOption('label', options.label, (harness, label) =>\n HarnessPredicate.stringMatches(harness.getLabelText(), label),\n )\n // We want to provide a filter option for \"name\" because the name of the checkbox is\n // only set on the underlying input. This means that it's not possible for developers\n // to retrieve the harness of a specific checkbox with name through a CSS selector.\n .addOption(\n 'name',\n options.name,\n async (harness, name) => (await harness.getName()) === name,\n )\n .addOption(\n 'checked',\n options.checked,\n async (harness, checked) => (await harness.isChecked()) == checked,\n )\n .addOption('disabled', options.disabled, async (harness, disabled) => {\n return (await harness.isDisabled()) === disabled;\n })\n );\n }\n\n /** Whether the checkbox is checked. */\n async isChecked(): Promise<boolean> {\n const checked = (await this._input()).getProperty<boolean>('checked');\n return coerceBooleanProperty(await checked);\n }\n\n /** Whether the checkbox is in an indeterminate state. */\n async isIndeterminate(): Promise<boolean> {\n const indeterminate = (await this._input()).getProperty<string>('indeterminate');\n return coerceBooleanProperty(await indeterminate);\n }\n\n /** Whether the checkbox is disabled. */\n async isDisabled(): Promise<boolean> {\n const input = await this._input();\n const disabled = await input.getAttribute('disabled');\n\n if (disabled !== null) {\n return coerceBooleanProperty(disabled);\n }\n\n return (await input.getAttribute('aria-disabled')) === 'true';\n }\n\n /** Whether the checkbox is required. */\n async isRequired(): Promise<boolean> {\n const required = (await this._input()).getProperty<boolean>('required');\n return coerceBooleanProperty(await required);\n }\n\n /** Whether the checkbox is valid. */\n async isValid(): Promise<boolean> {\n const invalid = (await this.host()).hasClass('ng-invalid');\n return !(await invalid);\n }\n\n /** Gets the checkbox's name. */\n async getName(): Promise<string | null> {\n return (await this._input()).getAttribute('name');\n }\n\n /** Gets the checkbox's value. */\n async getValue(): Promise<string | null> {\n return (await this._input()).getProperty<string | null>('value');\n }\n\n /** Gets the checkbox's aria-label. */\n async getAriaLabel(): Promise<string | null> {\n return (await this._input()).getAttribute('aria-label');\n }\n\n /** Gets the checkbox's aria-labelledby. */\n async getAriaLabelledby(): Promise<string | null> {\n return (await this._input()).getAttribute('aria-labelledby');\n }\n\n /** Gets the checkbox's label text. */\n async getLabelText(): Promise<string> {\n return (await this._label()).text();\n }\n\n /** Focuses the checkbox. */\n async focus(): Promise<void> {\n return (await this._input()).focus();\n }\n\n /** Blurs the checkbox. */\n async blur(): Promise<void> {\n return (await this._input()).blur();\n }\n\n /** Whether the checkbox is focused. */\n async isFocused(): Promise<boolean> {\n return (await this._input()).isFocused();\n }\n\n /**\n * Toggles the checked state of the checkbox.\n *\n * Note: This attempts to toggle the checkbox as a user would, by clicking it. Therefore if you\n * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method\n * might not have the expected result.\n */\n async toggle(): Promise<void> {\n const elToClick = await ((await this.isDisabled()) ? this._inputContainer() : this._input());\n return elToClick.click();\n }\n\n /**\n * Puts the checkbox in a checked state by toggling it if it is currently unchecked, or doing\n * nothing if it is already checked.\n *\n * Note: This attempts to check the checkbox as a user would, by clicking it. Therefore if you\n * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method\n * might not have the expected result.\n */\n async check(): Promise<void> {\n if (!(await this.isChecked())) {\n await this.toggle();\n }\n }\n\n /**\n * Puts the checkbox in an unchecked state by toggling it if it is currently checked, or doing\n * nothing if it is already unchecked.\n *\n * Note: This attempts to uncheck the checkbox as a user would, by clicking it. Therefore if you\n * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method\n * might not have the expected result.\n */\n async uncheck(): Promise<void> {\n if (await this.isChecked()) {\n await this.toggle();\n }\n }\n}\n"],"names":["MatCheckboxHarness","ComponentHarness","hostSelector","_input","locatorFor","_label","_inputContainer","with","options","HarnessPredicate","addOption","label","harness","stringMatches","getLabelText","name","getName","checked","isChecked","disabled","isDisabled","getProperty","coerceBooleanProperty","isIndeterminate","indeterminate","input","getAttribute","isRequired","required","isValid","invalid","host","hasClass","getValue","getAriaLabel","getAriaLabelledby","text","focus","blur","isFocused","toggle","elToClick","click","check","uncheck"],"mappings":";;;AAiBM,MAAOA,kBAAmB,SAAQC,gBAAgB,CAAA;EACtD,OAAOC,YAAY,GAAG,mBAAmB;AAEzCC,EAAAA,MAAM,GAAG,IAAI,CAACC,UAAU,CAAC,OAAO,CAAC;AACzBC,EAAAA,MAAM,GAAG,IAAI,CAACD,UAAU,CAAC,OAAO,CAAC;AACjCE,EAAAA,eAAe,GAAG,IAAI,CAACF,UAAU,CAAC,eAAe,CAAC;AAU1D,EAAA,OAAOG,IAAIA,CAETC,OAAA,GAAkC,EAAE,EAAA;IAEpC,OACE,IAAIC,gBAAgB,CAAC,IAAI,EAAED,OAAO,CAAA,CAC/BE,SAAS,CAAC,OAAO,EAAEF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