@angular/material
Version:
Angular Material
1 lines • 10 kB
Source Map (JSON)
{"version":3,"file":"button-testing.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/material/button/testing/button-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 {booleanAttribute} from '@angular/core';\nimport {\n ComponentHarnessConstructor,\n ContentContainerComponentHarness,\n HarnessPredicate,\n} from '@angular/cdk/testing';\nimport {MatIconHarness} from '@angular/material/icon/testing';\nimport {\n ButtonAppearance,\n ButtonHarnessFilters,\n ButtonType,\n ButtonVariant,\n} from './button-harness-filters';\n\n/** Harness for interacting with a mat-button in tests. */\nexport class MatButtonHarness extends ContentContainerComponentHarness {\n // Note: `.mat-mdc-button-base` should be enough for all buttons, however some apps are using\n // the harness without actually having an applied button. Keep the attributes for backwards\n // compatibility.\n\n /** Selector for the harness. */\n static hostSelector = `.mat-mdc-button-base, [matButton], [mat-button], [matIconButton],\n [matFab], [matMiniFab], [mat-raised-button], [mat-flat-button], [mat-icon-button],\n [mat-stroked-button], [mat-fab], [mat-mini-fab]`;\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a button with specific attributes.\n * @param options Options for narrowing the search:\n * - `selector` finds a button whose host element matches the given selector.\n * - `text` finds a button with specific text content.\n * - `variant` finds buttons matching a specific variant.\n * - `appearance` finds buttons matching a specific appearance.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatButtonHarness>(\n this: ComponentHarnessConstructor<T>,\n options: ButtonHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options)\n .addOption('text', options.text, (harness, text) =>\n HarnessPredicate.stringMatches(harness.getText(), text),\n )\n .addOption('variant', options.variant, (harness, variant) =>\n HarnessPredicate.stringMatches(harness.getVariant(), variant),\n )\n .addOption('appearance', options.appearance, (harness, appearance) =>\n HarnessPredicate.stringMatches(harness.getAppearance(), appearance),\n )\n .addOption('disabled', options.disabled, async (harness, disabled) => {\n return (await harness.isDisabled()) === disabled;\n })\n .addOption('buttonType', options.buttonType, (harness, buttonType) =>\n HarnessPredicate.stringMatches(harness.getType(), buttonType),\n )\n .addOption('iconName', options.iconName, async (harness, iconName) => {\n const result = await harness.locatorForOptional(MatIconHarness.with({name: iconName}))();\n return result !== null;\n });\n }\n\n /**\n * Clicks the button at the given position relative to its top-left.\n * @param relativeX The relative x position of the click.\n * @param relativeY The relative y position of the click.\n */\n click(relativeX: number, relativeY: number): Promise<void>;\n /** Clicks the button at its center. */\n click(location: 'center'): Promise<void>;\n /** Clicks the button. */\n click(): Promise<void>;\n async click(...args: [] | ['center'] | [number, number]): Promise<void> {\n return (await this.host()).click(...(args as []));\n }\n\n /** Gets a boolean promise indicating if the button is disabled. */\n async isDisabled(): Promise<boolean> {\n const host = await this.host();\n return (\n booleanAttribute(await host.getAttribute('disabled')) ||\n (await host.hasClass('mat-mdc-button-disabled'))\n );\n }\n\n /** Gets a promise for the button's label text. */\n async getText(): Promise<string> {\n return (await this.host()).text();\n }\n\n /** Focuses the button and returns a void promise that indicates when the action is complete. */\n async focus(): Promise<void> {\n return (await this.host()).focus();\n }\n\n /** Blurs the button and returns a void promise that indicates when the action is complete. */\n async blur(): Promise<void> {\n return (await this.host()).blur();\n }\n\n /** Whether the button is focused. */\n async isFocused(): Promise<boolean> {\n return (await this.host()).isFocused();\n }\n\n /** Gets the variant of the button. */\n async getVariant(): Promise<ButtonVariant> {\n const host = await this.host();\n\n // TODO(crisbeto): we're checking both classes and attributes for backwards compatibility\n // with some internal apps that were applying the attribute without importing the directive.\n // Really we should be only targeting the classes.\n if (\n (await host.hasClass('mat-mdc-icon-button')) ||\n (await host.getAttribute('mat-icon-button')) != null\n ) {\n return 'icon';\n }\n\n if (\n (await host.hasClass('mat-mdc-mini-fab')) ||\n (await host.getAttribute('mat-mini-fab')) != null\n ) {\n return 'mini-fab';\n }\n\n if ((await host.hasClass('mat-mdc-fab')) || (await host.getAttribute('mat-fab')) != null) {\n return 'fab';\n }\n\n return 'basic';\n }\n\n /** Gets the appearance of the button. */\n async getAppearance(): Promise<ButtonAppearance | null> {\n const host = await this.host();\n\n if (await host.hasClass('mat-mdc-outlined-button')) {\n return 'outlined';\n }\n\n if (await host.hasClass('mat-mdc-raised-button')) {\n return 'elevated';\n }\n\n if (await host.hasClass('mat-mdc-unelevated-button')) {\n return 'filled';\n }\n\n if (await host.hasClass('mat-mdc-button')) {\n return 'text';\n }\n\n if (await host.hasClass('mat-tonal-button')) {\n return 'tonal';\n }\n\n return null;\n }\n\n /**\n * Gets the type of the button. Supported values are 'button', 'submit', and 'reset'.\n */\n async getType(): Promise<ButtonType | null> {\n const host = await this.host();\n const buttonType = await host.getAttribute('type');\n if (buttonType === 'button' || buttonType === 'submit' || buttonType === 'reset') {\n return buttonType;\n }\n return null;\n }\n}\n"],"names":["MatButtonHarness","ContentContainerComponentHarness","hostSelector","with","options","HarnessPredicate","addOption","text","harness","stringMatches","getText","variant","getVariant","appearance","getAppearance","disabled","isDisabled","buttonType","getType","iconName","result","locatorForOptional","MatIconHarness","name","click","args","host","booleanAttribute","getAttribute","hasClass","focus","blur","isFocused"],"mappings":";;;;AAuBM,MAAOA,gBAAiB,SAAQC,gCAAgC,CAAA;AAMpE,EAAA,OAAOC,YAAY,GAAG,CAAA;;mDAE4B,CAAA;AAWlD,EAAA,OAAOC,IAAIA,CAETC,OAAA,GAAgC,EAAE,EAAA;IAElC,OAAO,IAAIC,gBAAgB,CAAC,IAAI,EAAED,OAAO,CAAA,CACtCE,SAAS,CAAC,MAAM,EAAEF,OAAO,CAACG,IAAI,EAAE,CAACC,OAAO,EAAED,IAAI,KAC7CF,gBAAgB,CAACI,aAAa,CAACD,OAAO,CAACE,OAAO,EAAE,EAAEH,IAAI,CAAC,CAAA,CAExDD,SAAS,CAAC,SAAS,EAAEF,OAAO,CAACO,OAAO,EAAE,CAACH,OAAO,EAAEG,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