UNPKG

@angular/material

Version:
1 lines 7.92 kB
{"version":3,"file":"testing.mjs","sources":["../../../../../../darwin_arm64-fastbuild-ST-46c76129e412/bin/src/material/slide-toggle/testing/slide-toggle-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 {coerceBooleanProperty} from '@angular/cdk/coercion';\nimport {SlideToggleHarnessFilters} from './slide-toggle-harness-filters';\n\n/** Harness for interacting with a mat-slide-toggle in tests. */\nexport class MatSlideToggleHarness extends ComponentHarness {\n private _label = this.locatorFor('label');\n _nativeElement = this.locatorFor('button');\n static hostSelector = '.mat-mdc-slide-toggle';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a slide-toggle w/ specific attributes.\n * @param options Options for narrowing the search:\n * - `selector` finds a slide-toggle whose host element matches the given selector.\n * - `label` finds a slide-toggle with specific label text.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatSlideToggleHarness>(\n this: ComponentHarnessConstructor<T>,\n options: SlideToggleHarnessFilters = {},\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 slide-toggle 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(\n 'disabled',\n options.disabled,\n async (harness, disabled) => (await harness.isDisabled()) == disabled,\n )\n );\n }\n\n /** Toggle the checked state of the slide-toggle. */\n async toggle(): Promise<void> {\n return (await this._nativeElement()).click();\n }\n\n /** Whether the slide-toggle is checked. */\n async isChecked(): Promise<boolean> {\n const checked = (await this._nativeElement()).getAttribute('aria-checked');\n return coerceBooleanProperty(await checked);\n }\n\n /** Whether the slide-toggle is disabled. */\n async isDisabled(): Promise<boolean> {\n const nativeElement = await this._nativeElement();\n const disabled = await nativeElement.getAttribute('disabled');\n\n if (disabled !== null) {\n return coerceBooleanProperty(disabled);\n }\n\n return (await nativeElement.getAttribute('aria-disabled')) === 'true';\n }\n\n /** Whether the slide-toggle is required. */\n async isRequired(): Promise<boolean> {\n const ariaRequired = await (await this._nativeElement()).getAttribute('aria-required');\n return ariaRequired === 'true';\n }\n\n /** Whether the slide-toggle 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 slide-toggle's name. */\n async getName(): Promise<string | null> {\n return (await this._nativeElement()).getAttribute('name');\n }\n\n /** Gets the slide-toggle's aria-label. */\n async getAriaLabel(): Promise<string | null> {\n return (await this._nativeElement()).getAttribute('aria-label');\n }\n\n /** Gets the slide-toggle's aria-labelledby. */\n async getAriaLabelledby(): Promise<string | null> {\n return (await this._nativeElement()).getAttribute('aria-labelledby');\n }\n\n /** Gets the slide-toggle's label text. */\n async getLabelText(): Promise<string> {\n return (await this._label()).text();\n }\n\n /** Focuses the slide-toggle. */\n async focus(): Promise<void> {\n return (await this._nativeElement()).focus();\n }\n\n /** Blurs the slide-toggle. */\n async blur(): Promise<void> {\n return (await this._nativeElement()).blur();\n }\n\n /** Whether the slide-toggle is focused. */\n async isFocused(): Promise<boolean> {\n return (await this._nativeElement()).isFocused();\n }\n\n /**\n * Puts the slide-toggle in a checked state by toggling it if it is currently unchecked, or doing\n * nothing if it is already checked.\n */\n async check(): Promise<void> {\n if (!(await this.isChecked())) {\n await this.toggle();\n }\n }\n\n /**\n * Puts the slide-toggle in an unchecked state by toggling it if it is currently checked, or doing\n * nothing if it is already unchecked.\n */\n async uncheck(): Promise<void> {\n if (await this.isChecked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