@angular/material
Version:
Angular Material
115 lines (110 loc) • 4.6 kB
JavaScript
import { __awaiter } from 'tslib';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
import { MatOptionHarness, MatOptgroupHarness } from '@angular/material/core/testing';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/** Selector for the autocomplete panel. */
const PANEL_SELECTOR = '.mat-autocomplete-panel';
/** Harness for interacting with a standard mat-autocomplete in tests. */
class MatAutocompleteHarness extends ComponentHarness {
constructor() {
super(...arguments);
this._documentRootLocator = this.documentRootLocatorFactory();
this._optionalPanel = this._documentRootLocator.locatorForOptional(PANEL_SELECTOR);
}
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatAutocompleteHarness` that meets
* certain criteria.
* @param options Options for filtering which autocomplete instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatAutocompleteHarness, options)
.addOption('value', options.value, (harness, value) => HarnessPredicate.stringMatches(harness.getValue(), value));
}
/** Gets the value of the autocomplete input. */
getValue() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).getProperty('value');
});
}
/** Whether the autocomplete input is disabled. */
isDisabled() {
return __awaiter(this, void 0, void 0, function* () {
const disabled = (yield this.host()).getAttribute('disabled');
return coerceBooleanProperty(yield disabled);
});
}
/** Focuses the autocomplete input. */
focus() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).focus();
});
}
/** Blurs the autocomplete input. */
blur() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).blur();
});
}
/** Enters text into the autocomplete. */
enterText(value) {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).sendKeys(value);
});
}
/** Gets the options inside the autocomplete panel. */
getOptions(filters = {}) {
return __awaiter(this, void 0, void 0, function* () {
return this._documentRootLocator.locatorForAll(MatOptionHarness.with(Object.assign(Object.assign({}, filters), { ancestor: PANEL_SELECTOR })))();
});
}
/** Gets the option groups inside the autocomplete panel. */
getOptionGroups(filters = {}) {
return __awaiter(this, void 0, void 0, function* () {
return this._documentRootLocator.locatorForAll(MatOptgroupHarness.with(Object.assign(Object.assign({}, filters), { ancestor: PANEL_SELECTOR })))();
});
}
/** Selects the first option matching the given filters. */
selectOption(filters) {
return __awaiter(this, void 0, void 0, function* () {
yield this.focus(); // Focus the input to make sure the autocomplete panel is shown.
const options = yield this.getOptions(filters);
if (!options.length) {
throw Error(`Could not find a mat-option matching ${JSON.stringify(filters)}`);
}
yield options[0].click();
});
}
/** Whether the autocomplete is open. */
isOpen() {
return __awaiter(this, void 0, void 0, function* () {
const panel = yield this._optionalPanel();
return !!panel && (yield panel.hasClass('mat-autocomplete-visible'));
});
}
}
/** The selector for the host element of a `MatAutocomplete` instance. */
MatAutocompleteHarness.hostSelector = '.mat-autocomplete-trigger';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export { MatAutocompleteHarness };
//# sourceMappingURL=testing.js.map