@angular/material
Version:
Angular Material
1 lines • 17.8 kB
Source Map (JSON)
{"version":3,"file":"testing.mjs","sources":["../../../../../../../src/material/radio/testing/radio-harness.ts","../../../../../../../src/material/radio/testing/radio-harness-filters.ts","../../../../../../../src/material/radio/testing/public-api.ts","../../../../../../../src/material/radio/testing/index.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.io/license\n */\n\nimport {coerceBooleanProperty} from '@angular/cdk/coercion';\nimport {\n AsyncFactoryFn,\n BaseHarnessFilters,\n ComponentHarness,\n ComponentHarnessConstructor,\n HarnessPredicate,\n TestElement,\n} from '@angular/cdk/testing';\nimport {RadioButtonHarnessFilters, RadioGroupHarnessFilters} from './radio-harness-filters';\n\nexport abstract class _MatRadioGroupHarnessBase<\n ButtonType extends ComponentHarnessConstructor<Button> & {\n with: (options?: ButtonFilters) => HarnessPredicate<Button>;\n },\n Button extends ComponentHarness & {\n isChecked(): Promise<boolean>;\n getValue(): Promise<string | null>;\n getName(): Promise<string | null>;\n check(): Promise<void>;\n },\n ButtonFilters extends BaseHarnessFilters,\n> extends ComponentHarness {\n protected abstract _buttonClass: ButtonType;\n\n /** Gets the name of the radio-group. */\n async getName(): Promise<string | null> {\n const hostName = await this._getGroupNameFromHost();\n // It's not possible to always determine the \"name\" of a radio-group by reading\n // the attribute. This is because the radio-group does not set the \"name\" as an\n // element attribute if the \"name\" value is set through a binding.\n if (hostName !== null) {\n return hostName;\n }\n // In case we couldn't determine the \"name\" of a radio-group by reading the\n // \"name\" attribute, we try to determine the \"name\" of the group by going\n // through all radio buttons.\n const radioNames = await this._getNamesFromRadioButtons();\n if (!radioNames.length) {\n return null;\n }\n if (!this._checkRadioNamesInGroupEqual(radioNames)) {\n throw Error('Radio buttons in radio-group have mismatching names.');\n }\n return radioNames[0]!;\n }\n\n /** Gets the id of the radio-group. */\n async getId(): Promise<string | null> {\n return (await this.host()).getProperty<string | null>('id');\n }\n\n /** Gets the checked radio-button in a radio-group. */\n async getCheckedRadioButton(): Promise<Button | null> {\n for (let radioButton of await this.getRadioButtons()) {\n if (await radioButton.isChecked()) {\n return radioButton;\n }\n }\n return null;\n }\n\n /** Gets the checked value of the radio-group. */\n async getCheckedValue(): Promise<string | null> {\n const checkedRadio = await this.getCheckedRadioButton();\n if (!checkedRadio) {\n return null;\n }\n return checkedRadio.getValue();\n }\n\n /**\n * Gets a list of radio buttons which are part of the radio-group.\n * @param filter Optionally filters which radio buttons are included.\n */\n async getRadioButtons(filter?: ButtonFilters): Promise<Button[]> {\n return this.locatorForAll(this._buttonClass.with(filter))();\n }\n\n /**\n * Checks a radio button in this group.\n * @param filter An optional filter to apply to the child radio buttons. The first tab matching\n * the filter will be selected.\n */\n async checkRadioButton(filter?: ButtonFilters): Promise<void> {\n const radioButtons = await this.getRadioButtons(filter);\n if (!radioButtons.length) {\n throw Error(`Could not find radio button matching ${JSON.stringify(filter)}`);\n }\n return radioButtons[0].check();\n }\n\n /** Gets the name attribute of the host element. */\n private async _getGroupNameFromHost() {\n return (await this.host()).getAttribute('name');\n }\n\n /** Gets a list of the name attributes of all child radio buttons. */\n private async _getNamesFromRadioButtons(): Promise<string[]> {\n const groupNames: string[] = [];\n for (let radio of await this.getRadioButtons()) {\n const radioName = await radio.getName();\n if (radioName !== null) {\n groupNames.push(radioName);\n }\n }\n return groupNames;\n }\n\n /** Checks if the specified radio names are all equal. */\n private _checkRadioNamesInGroupEqual(radioNames: string[]): boolean {\n let groupName: string | null = null;\n for (let radioName of radioNames) {\n if (groupName === null) {\n groupName = radioName;\n } else if (groupName !== radioName) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Checks if a radio-group harness has the given name. Throws if a radio-group with\n * matching name could be found but has mismatching radio-button names.\n */\n protected static async _checkRadioGroupName(\n harness: _MatRadioGroupHarnessBase<any, any, any>,\n name: string,\n ) {\n // Check if there is a radio-group which has the \"name\" attribute set\n // to the expected group name. It's not possible to always determine\n // the \"name\" of a radio-group by reading the attribute. This is because\n // the radio-group does not set the \"name\" as an element attribute if the\n // \"name\" value is set through a binding.\n if ((await harness._getGroupNameFromHost()) === name) {\n return true;\n }\n // Check if there is a group with radio-buttons that all have the same\n // expected name. This implies that the group has the given name. It's\n // not possible to always determine the name of a radio-group through\n // the attribute because there is\n const radioNames = await harness._getNamesFromRadioButtons();\n if (radioNames.indexOf(name) === -1) {\n return false;\n }\n if (!harness._checkRadioNamesInGroupEqual(radioNames)) {\n throw Error(\n `The locator found a radio-group with name \"${name}\", but some ` +\n `radio-button's within the group have mismatching names, which is invalid.`,\n );\n }\n return true;\n }\n}\n\n/** Harness for interacting with an MDC-based mat-radio-group in tests. */\nexport class MatRadioGroupHarness extends _MatRadioGroupHarnessBase<\n typeof MatRadioButtonHarness,\n MatRadioButtonHarness,\n RadioButtonHarnessFilters\n> {\n /** The selector for the host element of a `MatRadioGroup` instance. */\n static hostSelector = '.mat-mdc-radio-group';\n protected _buttonClass = MatRadioButtonHarness;\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a radio group with specific\n * attributes.\n * @param options Options for filtering which radio group instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatRadioGroupHarness>(\n this: ComponentHarnessConstructor<T>,\n options: RadioGroupHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options).addOption(\n 'name',\n options.name,\n MatRadioGroupHarness._checkRadioGroupName,\n );\n }\n}\n\nexport abstract class _MatRadioButtonHarnessBase extends ComponentHarness {\n protected abstract _textLabel: AsyncFactoryFn<TestElement>;\n protected abstract _clickLabel: AsyncFactoryFn<TestElement>;\n private _input = this.locatorFor('input');\n\n /** Whether the radio-button 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 radio-button is disabled. */\n async isDisabled(): Promise<boolean> {\n const disabled = (await this._input()).getAttribute('disabled');\n return coerceBooleanProperty(await disabled);\n }\n\n /** Whether the radio-button is required. */\n async isRequired(): Promise<boolean> {\n const required = (await this._input()).getAttribute('required');\n return coerceBooleanProperty(await required);\n }\n\n /** Gets the radio-button's name. */\n async getName(): Promise<string | null> {\n return (await this._input()).getAttribute('name');\n }\n\n /** Gets the radio-button's id. */\n async getId(): Promise<string | null> {\n return (await this.host()).getProperty<string>('id');\n }\n\n /**\n * Gets the value of the radio-button. The radio-button value will be converted to a string.\n *\n * Note: This means that for radio-button's with an object as a value `[object Object]` is\n * intentionally returned.\n */\n async getValue(): Promise<string | null> {\n return (await this._input()).getProperty('value');\n }\n\n /** Gets the radio-button's label text. */\n async getLabelText(): Promise<string> {\n return (await this._textLabel()).text();\n }\n\n /** Focuses the radio-button. */\n async focus(): Promise<void> {\n return (await this._input()).focus();\n }\n\n /** Blurs the radio-button. */\n async blur(): Promise<void> {\n return (await this._input()).blur();\n }\n\n /** Whether the radio-button is focused. */\n async isFocused(): Promise<boolean> {\n return (await this._input()).isFocused();\n }\n\n /**\n * Puts the radio-button in a checked state by clicking it if it is currently unchecked,\n * or doing nothing if it is already checked.\n */\n async check(): Promise<void> {\n if (!(await this.isChecked())) {\n return (await this._clickLabel()).click();\n }\n }\n}\n\n/** Harness for interacting with an MDC-based mat-radio-button in tests. */\nexport class MatRadioButtonHarness extends _MatRadioButtonHarnessBase {\n /** The selector for the host element of a `MatRadioButton` instance. */\n static hostSelector = '.mat-mdc-radio-button';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a radio button with specific\n * attributes.\n * @param options Options for filtering which radio button instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatRadioButtonHarness>(\n this: ComponentHarnessConstructor<T>,\n options: RadioButtonHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options)\n .addOption('label', options.label, (harness, label) =>\n HarnessPredicate.stringMatches(harness.getLabelText(), label),\n )\n .addOption('name', options.name, async (harness, name) => (await harness.getName()) === name)\n .addOption(\n 'checked',\n options.checked,\n async (harness, checked) => (await harness.isChecked()) == checked,\n );\n }\n\n protected _textLabel = this.locatorFor('label');\n protected _clickLabel = this._textLabel;\n}\n","/**\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.io/license\n */\n\nimport {BaseHarnessFilters} from '@angular/cdk/testing';\n\n/** A set of criteria that can be used to filter a list of `MatRadioGroupHarness` instances. */\nexport interface RadioGroupHarnessFilters extends BaseHarnessFilters {\n /** Only find instances whose name attribute is the given value. */\n name?: string;\n}\n\n/** A set of criteria that can be used to filter a list of `MatRadioButtonHarness` instances. */\nexport interface RadioButtonHarnessFilters extends BaseHarnessFilters {\n /** Only find instances whose label matches the given value. */\n label?: string | RegExp;\n /** Only find instances whose name attribute is the given value. */\n name?: string;\n /** Only find instances with the given checked value. */\n checked?: boolean;\n}\n","/**\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.io/license\n */\n\nexport * from './radio-harness';\nexport * from './radio-harness-filters';\n","/**\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.io/license\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;AAAA;;;;;;AAMG;AAaG,MAAgB,yBAWpB,SAAQ,gBAAgB,CAAA;;AAIxB,IAAA,MAAM,OAAO,GAAA;AACX,QAAA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,qBAAqB,EAAE,CAAC;;;;QAIpD,IAAI,QAAQ,KAAK,IAAI,EAAE;AACrB,YAAA,OAAO,QAAQ,CAAC;AACjB,SAAA;;;;AAID,QAAA,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,yBAAyB,EAAE,CAAC;AAC1D,QAAA,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE;AACtB,YAAA,OAAO,IAAI,CAAC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