UNPKG

@angular/material

Version:
1 lines 16.8 kB
{"version":3,"file":"testing.mjs","sources":["../../../../../../../src/material/form-field/testing/form-field-harness.ts","../../../../../../../src/material/form-field/testing/public-api.ts","../../../../../../../src/material/form-field/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 {\n AsyncFactoryFn,\n ComponentHarness,\n ComponentHarnessConstructor,\n HarnessPredicate,\n HarnessQuery,\n parallel,\n TestElement,\n} from '@angular/cdk/testing';\nimport {\n MatDatepickerInputHarness,\n MatDateRangeInputHarness,\n} from '@angular/material/datepicker/testing';\nimport {MatFormFieldControlHarness} from '@angular/material/form-field/testing/control';\nimport {MatInputHarness} from '@angular/material/input/testing';\nimport {MatSelectHarness} from '@angular/material/select/testing';\nimport {FormFieldHarnessFilters} from './form-field-harness-filters';\n\n// TODO(devversion): support support chip list harness\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 abstract class _MatFormFieldHarnessBase<\n ControlHarness extends MatFormFieldControlHarness,\n> extends ComponentHarness {\n protected abstract _prefixContainer: AsyncFactoryFn<TestElement | null>;\n protected abstract _suffixContainer: AsyncFactoryFn<TestElement | null>;\n protected abstract _label: AsyncFactoryFn<TestElement | null>;\n protected abstract _errors: AsyncFactoryFn<TestElement[]>;\n protected abstract _hints: AsyncFactoryFn<TestElement[]>;\n protected abstract _inputControl: AsyncFactoryFn<ControlHarness | null>;\n protected abstract _selectControl: AsyncFactoryFn<ControlHarness | null>;\n protected abstract _datepickerInputControl: AsyncFactoryFn<ControlHarness | null>;\n protected abstract _dateRangeInputControl: AsyncFactoryFn<ControlHarness | null>;\n\n /** Gets the appearance of the form-field. */\n abstract getAppearance(): Promise<string>;\n\n /** Whether the label is currently floating. */\n abstract isLabelFloating(): Promise<boolean>;\n\n /** Whether the form-field has a label. */\n abstract hasLabel(): Promise<boolean>;\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<ControlHarness | 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._errors();\n return parallel(() => errors.map(e => e.text()));\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\n/** Harness for interacting with a standard Material form-field's in tests. */\nexport class MatFormFieldHarness extends _MatFormFieldHarnessBase<FormFieldControlHarness> {\n static hostSelector = '.mat-form-field';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a `MatFormFieldHarness` that meets\n * certain criteria.\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(options: FormFieldHarnessFilters = {}): HarnessPredicate<MatFormFieldHarness> {\n return new HarnessPredicate(MatFormFieldHarness, 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 }\n\n protected _prefixContainer = this.locatorForOptional('.mat-form-field-prefix');\n protected _suffixContainer = this.locatorForOptional('.mat-form-field-suffix');\n protected _label = this.locatorForOptional('.mat-form-field-label');\n protected _errors = this.locatorForAll('.mat-error');\n protected _hints = this.locatorForAll('mat-hint, .mat-hint');\n protected _inputControl = this.locatorForOptional(MatInputHarness);\n protected _selectControl = this.locatorForOptional(MatSelectHarness);\n protected _datepickerInputControl = this.locatorForOptional(MatDatepickerInputHarness);\n protected _dateRangeInputControl = this.locatorForOptional(MatDateRangeInputHarness);\n\n /** Gets the appearance of the form-field. */\n async getAppearance(): Promise<'legacy' | 'standard' | 'fill' | 'outline'> {\n const hostClasses = await (await this.host()).getAttribute('class');\n if (hostClasses !== null) {\n const appearanceMatch = hostClasses.match(\n /mat-form-field-appearance-(legacy|standard|fill|outline)(?:$| )/,\n );\n if (appearanceMatch) {\n return appearanceMatch[1] as 'legacy' | 'standard' | 'fill' | 'outline';\n }\n }\n throw Error('Could not determine appearance of form-field.');\n }\n\n /** Whether the form-field has a label. */\n async hasLabel(): Promise<boolean> {\n return (await this.host()).hasClass('mat-form-field-has-label');\n }\n\n /** Whether the label is currently floating. */\n async isLabelFloating(): Promise<boolean> {\n const host = await this.host();\n const [hasLabel, shouldFloat] = await parallel(() => [\n this.hasLabel(),\n host.hasClass('mat-form-field-should-float'),\n ]);\n // If there is no label, the label conceptually can never float. The `should-float` class\n // is just always set regardless of whether the label is displayed or not.\n return hasLabel && shouldFloat;\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.io/license\n */\n\n// Re-export the base control harness from the \"form-field/testing/control\" entry-point. To\n// avoid circular dependencies, harnesses for form-field controls (i.e. input, select)\n// need to import the base form-field control harness through a separate entry-point.\nexport {MatFormFieldControlHarness} from '@angular/material/form-field/testing/control';\n\nexport * from './form-field-harness';\nexport * from './form-field-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":";;;;;;;AAkCM,MAAgB,wBAEpB,SAAQ,gBAAgB,CAAA;;IAqBlB,QAAQ,GAAA;;AACZ,YAAA,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;AACpC,YAAA,OAAO,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC;SACxC,CAAA,CAAA;AAAA,KAAA;;IAGK,SAAS,GAAA;;YACb,OAAO,CAAC,MAAM,IAAI,CAAC,aAAa,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;SAChD,CAAA,CAAA;AAAA,KAAA;;IAGK,UAAU,GAAA;;AACd,YAAA,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC,yBAAyB,CAAC,CAAC;SAChE,CAAA,CAAA;AAAA,KAAA;;IAGK,YAAY,GAAA;;AAChB,YAAA,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC,2BAA2B,CAAC,CAAC;SAClE,CAAA,CAAA;AAAA,KA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