UNPKG

@angular/material

Version:
1 lines 17.2 kB
{"version":3,"file":"form-field-testing.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/material/form-field/testing/error-harness.ts","../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/material/form-field/testing/form-field-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 BaseHarnessFilters,\n ComponentHarness,\n ComponentHarnessConstructor,\n HarnessPredicate,\n} from '@angular/cdk/testing';\n\n/** A set of criteria that can be used to filter a list of error harness instances. */\nexport interface ErrorHarnessFilters extends BaseHarnessFilters {\n /** Only find instances whose text matches the given value. */\n text?: string | RegExp;\n}\n\n/** Harness for interacting with a `mat-error` in tests. */\nexport class MatErrorHarness extends ComponentHarness {\n static hostSelector = '.mat-mdc-form-field-error';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for an error with specific\n * attributes.\n * @param options Options for filtering which error instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatErrorHarness>(\n this: ComponentHarnessConstructor<T>,\n options: ErrorHarnessFilters = {},\n ): HarnessPredicate<T> {\n return MatErrorHarness._getErrorPredicate(this, options);\n }\n\n protected static _getErrorPredicate<T extends MatErrorHarness>(\n type: ComponentHarnessConstructor<T>,\n options: ErrorHarnessFilters,\n ): HarnessPredicate<T> {\n return new HarnessPredicate(type, options).addOption('text', options.text, (harness, text) =>\n HarnessPredicate.stringMatches(harness.getText(), text),\n );\n }\n\n /** Gets a promise for the error's label text. */\n async getText(): Promise<string> {\n return (await this.host()).text();\n }\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.dev/license\n */\n\nimport {\n ComponentHarness,\n ComponentHarnessConstructor,\n HarnessPredicate,\n HarnessQuery,\n parallel,\n} from '@angular/cdk/testing';\nimport {ErrorHarnessFilters, MatErrorHarness} from './error-harness';\nimport {MatInputHarness} from '../../input/testing';\nimport {MatFormFieldControlHarness} from './control';\nimport {MatSelectHarness} from '../../select/testing';\nimport {MatDatepickerInputHarness, MatDateRangeInputHarness} from '../../datepicker/testing';\nimport {FormFieldHarnessFilters} from './form-field-harness-filters';\n\n/** Possible harnesses of controls which can be bound to a form-field. */\nexport type FormFieldControlHarness =\n | MatInputHarness\n | MatSelectHarness\n | MatDatepickerInputHarness\n | MatDateRangeInputHarness;\n\nexport class MatFormFieldHarness extends ComponentHarness {\n private _prefixContainer = this.locatorForOptional('.mat-mdc-form-field-text-prefix');\n private _suffixContainer = this.locatorForOptional('.mat-mdc-form-field-text-suffix');\n private _label = this.locatorForOptional('.mdc-floating-label');\n private _hints = this.locatorForAll('.mat-mdc-form-field-hint');\n private _inputControl = this.locatorForOptional(MatInputHarness);\n private _selectControl = this.locatorForOptional(MatSelectHarness);\n private _datepickerInputControl = this.locatorForOptional(MatDatepickerInputHarness);\n private _dateRangeInputControl = this.locatorForOptional(MatDateRangeInputHarness);\n private _textField = this.locatorFor('.mat-mdc-text-field-wrapper');\n private _errorHarness = MatErrorHarness;\n\n static hostSelector = '.mat-mdc-form-field';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a form field with specific\n * attributes.\n * @param options Options for filtering which form field instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatFormFieldHarness>(\n this: ComponentHarnessConstructor<T>,\n options: FormFieldHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options)\n .addOption('floatingLabelText', options.floatingLabelText, async (harness, text) =>\n HarnessPredicate.stringMatches(await harness.getLabel(), text),\n )\n .addOption(\n 'hasErrors',\n options.hasErrors,\n async (harness, hasErrors) => (await harness.hasErrors()) === hasErrors,\n )\n .addOption(\n 'isValid',\n options.isValid,\n async (harness, isValid) => (await harness.isControlValid()) === isValid,\n );\n }\n\n /** Gets the appearance of the form-field. */\n async getAppearance(): Promise<'fill' | 'outline'> {\n const textFieldEl = await this._textField();\n if (await textFieldEl.hasClass('mdc-text-field--outlined')) {\n return 'outline';\n }\n return 'fill';\n }\n\n /** Whether the form-field has a label. */\n async hasLabel(): Promise<boolean> {\n return (await this._label()) !== null;\n }\n\n /** Whether the label is currently floating. */\n async isLabelFloating(): Promise<boolean> {\n const labelEl = await this._label();\n return labelEl !== null ? await labelEl.hasClass('mdc-floating-label--float-above') : false;\n }\n\n /** Gets the label of the form-field. */\n async getLabel(): Promise<string | null> {\n const labelEl = await this._label();\n return labelEl ? labelEl.text() : null;\n }\n\n /** Whether the form-field has errors. */\n async hasErrors(): Promise<boolean> {\n return (await this.getTextErrors()).length > 0;\n }\n\n /** Whether the form-field is disabled. */\n async isDisabled(): Promise<boolean> {\n return (await this.host()).hasClass('mat-form-field-disabled');\n }\n\n /** Whether the form-field is currently autofilled. */\n async isAutofilled(): Promise<boolean> {\n return (await this.host()).hasClass('mat-form-field-autofilled');\n }\n\n /**\n * Gets the harness of the control that is bound to the form-field. Only\n * default controls such as \"MatInputHarness\" and \"MatSelectHarness\" are\n * supported.\n */\n async getControl(): Promise<FormFieldControlHarness | null>;\n\n /**\n * Gets the harness of the control that is bound to the form-field. Searches\n * for a control that matches the specified harness type.\n */\n async getControl<X extends MatFormFieldControlHarness>(\n type: ComponentHarnessConstructor<X>,\n ): Promise<X | null>;\n\n /**\n * Gets the harness of the control that is bound to the form-field. Searches\n * for a control that matches the specified harness predicate.\n */\n async getControl<X extends MatFormFieldControlHarness>(\n type: HarnessPredicate<X>,\n ): Promise<X | null>;\n\n // Implementation of the \"getControl\" method overload signatures.\n async getControl<X extends MatFormFieldControlHarness>(type?: HarnessQuery<X>) {\n if (type) {\n return this.locatorForOptional(type)();\n }\n const [select, input, datepickerInput, dateRangeInput] = await parallel(() => [\n this._selectControl(),\n this._inputControl(),\n this._datepickerInputControl(),\n this._dateRangeInputControl(),\n ]);\n\n // Match the datepicker inputs first since they can also have a `MatInput`.\n return datepickerInput || dateRangeInput || select || input;\n }\n\n /** Gets the theme color of the form-field. */\n async getThemeColor(): Promise<'primary' | 'accent' | 'warn'> {\n const hostEl = await this.host();\n const [isAccent, isWarn] = await parallel(() => {\n return [hostEl.hasClass('mat-accent'), hostEl.hasClass('mat-warn')];\n });\n if (isAccent) {\n return 'accent';\n } else if (isWarn) {\n return 'warn';\n }\n return 'primary';\n }\n\n /** Gets error messages which are currently displayed in the form-field. */\n async getTextErrors(): Promise<string[]> {\n const errors = await this.getErrors();\n return parallel(() => errors.map(e => e.getText()));\n }\n\n /** Gets all of the error harnesses in the form field. */\n async getErrors(filter: ErrorHarnessFilters = {}): Promise<MatErrorHarness[]> {\n return this.locatorForAll(this._errorHarness.with(filter))();\n }\n\n /** Gets hint messages which are currently displayed in the form-field. */\n async getTextHints(): Promise<string[]> {\n const hints = await this._hints();\n return parallel(() => hints.map(e => e.text()));\n }\n\n /** Gets the text inside the prefix element. */\n async getPrefixText(): Promise<string> {\n const prefix = await this._prefixContainer();\n return prefix ? prefix.text() : '';\n }\n\n /** Gets the text inside the suffix element. */\n async getSuffixText(): Promise<string> {\n const suffix = await this._suffixContainer();\n return suffix ? suffix.text() : '';\n }\n\n /**\n * Whether the form control has been touched. Returns \"null\"\n * if no form control is set up.\n */\n async isControlTouched(): Promise<boolean | null> {\n if (!(await this._hasFormControl())) {\n return null;\n }\n return (await this.host()).hasClass('ng-touched');\n }\n\n /**\n * Whether the form control is dirty. Returns \"null\"\n * if no form control is set up.\n */\n async isControlDirty(): Promise<boolean | null> {\n if (!(await this._hasFormControl())) {\n return null;\n }\n return (await this.host()).hasClass('ng-dirty');\n }\n\n /**\n * Whether the form control is valid. Returns \"null\"\n * if no form control is set up.\n */\n async isControlValid(): Promise<boolean | null> {\n if (!(await this._hasFormControl())) {\n return null;\n }\n return (await this.host()).hasClass('ng-valid');\n }\n\n /**\n * Whether the form control is pending validation. Returns \"null\"\n * if no form control is set up.\n */\n async isControlPending(): Promise<boolean | null> {\n if (!(await this._hasFormControl())) {\n return null;\n }\n return (await this.host()).hasClass('ng-pending');\n }\n\n /** Checks whether the form-field control has set up a form control. */\n private async _hasFormControl(): Promise<boolean> {\n const hostEl = await this.host();\n // If no form \"NgControl\" is bound to the form-field control, the form-field\n // is not able to forward any control status classes. Therefore if either the\n // \"ng-touched\" or \"ng-untouched\" class is set, we know that it has a form control\n const [isTouched, isUntouched] = await parallel(() => [\n hostEl.hasClass('ng-touched'),\n hostEl.hasClass('ng-untouched'),\n ]);\n return isTouched || isUntouched;\n }\n}\n"],"names":["MatErrorHarness","ComponentHarness","hostSelector","with","options","_getErrorPredicate","type","HarnessPredicate","addOption","text","harness","stringMatches","getText","host","MatFormFieldHarness","_prefixContainer","locatorForOptional","_suffixContainer","_label","_hints","locatorForAll","_inputControl","MatInputHarness","_selectControl","MatSelectHarness","_datepickerInputControl","MatDatepickerInputHarness","_dateRangeInputControl","MatDateRangeInputHarness","_textField","locatorFor","_errorHarness","floatingLabelText","getLabel","hasErrors","isValid","isControlValid","getAppearance","textFieldEl","hasClass","hasLabel","isLabelFloating","labelEl","getTextErrors","length","isDisabled","isAutofilled","getControl","select","input","datepickerInput","dateRangeInput","parallel","getThemeColor","hostEl","isAccent","isWarn","errors","getErrors","map","e","filter","getTextHints","hints","getPrefixText","prefix","getSuffixText","suffix","isControlTouched","_hasFormControl","isControlDirty","isControlPending","isTouched","isUntouched"],"mappings":";;;;;;;;;AAsBM,MAAOA,eAAgB,SAAQC,gBAAgB,CAAA;EACnD,OAAOC,YAAY,GAAG,2BAA2B;AAQjD,EAAA,OAAOC,IAAIA,CAETC,OAAA,GAA+B,EAAE,EAAA;AAEjC,IAAA,OAAOJ,eAAe,CAACK,kBAAkB,CAAC,IAAI,EAAED,OAAO,CAAC;AAC1D;AAEU,EAAA,OAAOC,kBAAkBA,CACjCC,IAAoC,EACpCF,OAA4B,EAAA;AAE5B,IAAA,OAAO,IAAIG,gBAAgB,CAACD,IAA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