blob: 7ea75705bd6c54fe70ec95cfb732897a6342f838 [file] [log] [blame]
import { __awaiter } from 'tslib';
import { ComponentHarness, HarnessPredicate, TestKey, parallel } from '@angular/cdk/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
*/
/** Harness for interacting with a standard Material chip remove button in tests. */
class MatChipRemoveHarness extends ComponentHarness {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipRemoveHarness` that meets
* certain criteria.
* @param options Options for filtering which input instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipRemoveHarness, options);
}
/** Clicks the remove button. */
click() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).click();
});
}
}
MatChipRemoveHarness.hostSelector = '.mat-chip-remove';
/**
* @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
*/
/** Harness for interacting with a standard selectable Angular Material chip in tests. */
class MatChipHarness extends ComponentHarness {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipHarness` that meets
* certain criteria.
* @param options Options for filtering which chip instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipHarness, options)
.addOption('text', options.text, (harness, label) => HarnessPredicate.stringMatches(harness.getText(), label))
.addOption('selected', options.selected, (harness, selected) => __awaiter(this, void 0, void 0, function* () { return (yield harness.isSelected()) === selected; }));
}
/** Gets the text of the chip. */
getText() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).text({
exclude: '.mat-chip-avatar, .mat-chip-trailing-icon, .mat-icon'
});
});
}
/**
* Whether the chip is selected.
* @deprecated Use `MatChipOptionHarness.isSelected` instead.
* @breaking-change 12.0.0
*/
isSelected() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).hasClass('mat-chip-selected');
});
}
/** Whether the chip is disabled. */
isDisabled() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).hasClass('mat-chip-disabled');
});
}
/**
* Selects the given chip. Only applies if it's selectable.
* @deprecated Use `MatChipOptionHarness.select` instead.
* @breaking-change 12.0.0
*/
select() {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield this.isSelected())) {
yield this.toggle();
}
});
}
/**
* Deselects the given chip. Only applies if it's selectable.
* @deprecated Use `MatChipOptionHarness.deselect` instead.
* @breaking-change 12.0.0
*/
deselect() {
return __awaiter(this, void 0, void 0, function* () {
if (yield this.isSelected()) {
yield this.toggle();
}
});
}
/**
* Toggles the selected state of the given chip. Only applies if it's selectable.
* @deprecated Use `MatChipOptionHarness.toggle` instead.
* @breaking-change 12.0.0
*/
toggle() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).sendKeys(' ');
});
}
/** Removes the given chip. Only applies if it's removable. */
remove() {
return __awaiter(this, void 0, void 0, function* () {
yield (yield this.host()).sendKeys(TestKey.DELETE);
});
}
/**
* Gets the remove button inside of a chip.
* @param filter Optionally filters which chips are included.
*/
getRemoveButton(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
return this.locatorFor(MatChipRemoveHarness.with(filter))();
});
}
}
/** The selector for the host element of a `MatChip` instance. */
MatChipHarness.hostSelector = '.mat-chip';
/**
* @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
*/
/** Harness for interacting with a standard Material chip inputs in tests. */
class MatChipInputHarness extends ComponentHarness {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipInputHarness` that meets
* certain criteria.
* @param options Options for filtering which input instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipInputHarness, options)
.addOption('value', options.value, (harness, value) => __awaiter(this, void 0, void 0, function* () {
return (yield harness.getValue()) === value;
}))
.addOption('placeholder', options.placeholder, (harness, placeholder) => __awaiter(this, void 0, void 0, function* () {
return (yield harness.getPlaceholder()) === placeholder;
}));
}
/** Whether the input is disabled. */
isDisabled() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).getProperty('disabled');
});
}
/** Whether the input is required. */
isRequired() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).getProperty('required');
});
}
/** Gets the value of the input. */
getValue() {
return __awaiter(this, void 0, void 0, function* () {
// The "value" property of the native input is never undefined.
return (yield (yield this.host()).getProperty('value'));
});
}
/** Gets the placeholder of the input. */
getPlaceholder() {
return __awaiter(this, void 0, void 0, function* () {
return (yield (yield this.host()).getProperty('placeholder'));
});
}
/**
* Focuses the input and returns a promise that indicates when the
* action is complete.
*/
focus() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).focus();
});
}
/**
* Blurs the input and returns a promise that indicates when the
* action is complete.
*/
blur() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).blur();
});
}
/** Whether the input is focused. */
isFocused() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).isFocused();
});
}
/**
* Sets the value of the input. The value will be set by simulating
* keypresses that correspond to the given value.
*/
setValue(newValue) {
return __awaiter(this, void 0, void 0, function* () {
const inputEl = yield this.host();
yield inputEl.clear();
// We don't want to send keys for the value if the value is an empty
// string in order to clear the value. Sending keys with an empty string
// still results in unnecessary focus events.
if (newValue) {
yield inputEl.sendKeys(newValue);
}
});
}
/** Sends a chip separator key to the input element. */
sendSeparatorKey(key) {
return __awaiter(this, void 0, void 0, function* () {
const inputEl = yield this.host();
return inputEl.sendKeys(key);
});
}
}
MatChipInputHarness.hostSelector = '.mat-chip-input';
/**
* @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
*/
/** Base class for chip list harnesses. */
class _MatChipListHarnessBase extends ComponentHarness {
/** Gets whether the chip list is disabled. */
isDisabled() {
return __awaiter(this, void 0, void 0, function* () {
return (yield (yield this.host()).getAttribute('aria-disabled')) === 'true';
});
}
/** Gets whether the chip list is required. */
isRequired() {
return __awaiter(this, void 0, void 0, function* () {
return (yield (yield this.host()).getAttribute('aria-required')) === 'true';
});
}
/** Gets whether the chip list is invalid. */
isInvalid() {
return __awaiter(this, void 0, void 0, function* () {
return (yield (yield this.host()).getAttribute('aria-invalid')) === 'true';
});
}
/** Gets whether the chip list is in multi selection mode. */
isMultiple() {
return __awaiter(this, void 0, void 0, function* () {
return (yield (yield this.host()).getAttribute('aria-multiselectable')) === 'true';
});
}
/** Gets whether the orientation of the chip list. */
getOrientation() {
return __awaiter(this, void 0, void 0, function* () {
const orientation = yield (yield this.host()).getAttribute('aria-orientation');
return orientation === 'vertical' ? 'vertical' : 'horizontal';
});
}
}
/** Harness for interacting with a standard chip list in tests. */
class MatChipListHarness extends _MatChipListHarnessBase {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipListHarness` that meets
* certain criteria.
* @param options Options for filtering which chip list instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipListHarness, options);
}
/**
* Gets the list of chips inside the chip list.
* @param filter Optionally filters which chips are included.
*/
getChips(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
return this.locatorForAll(MatChipHarness.with(filter))();
});
}
/**
* Selects a chip inside the chip list.
* @param filter An optional filter to apply to the child chips.
* All the chips matching the filter will be selected.
* @deprecated Use `MatChipListboxHarness.selectChips` instead.
* @breaking-change 12.0.0
*/
selectChips(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
const chips = yield this.getChips(filter);
if (!chips.length) {
throw Error(`Cannot find chip matching filter ${JSON.stringify(filter)}`);
}
yield parallel(() => chips.map(chip => chip.select()));
});
}
/**
* Gets the `MatChipInput` inside the chip list.
* @param filter Optionally filters which chip input is included.
*/
getInput(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
// The input isn't required to be a descendant of the chip list so we have to look it up by id.
const inputId = yield (yield this.host()).getAttribute('data-mat-chip-input');
if (!inputId) {
throw Error(`Chip list is not associated with an input`);
}
return this.documentRootLocatorFactory().locatorFor(MatChipInputHarness.with(Object.assign(Object.assign({}, filter), { selector: `#${inputId}` })))();
});
}
}
/** The selector for the host element of a `MatChipList` instance. */
MatChipListHarness.hostSelector = '.mat-chip-list';
/**
* @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
*/
class MatChipOptionHarness extends MatChipHarness {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipOptionHarness`
* that meets certain criteria.
* @param options Options for filtering which chip instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipOptionHarness, options)
.addOption('text', options.text, (harness, label) => HarnessPredicate.stringMatches(harness.getText(), label))
.addOption('selected', options.selected, (harness, selected) => __awaiter(this, void 0, void 0, function* () { return (yield harness.isSelected()) === selected; }));
}
/** Whether the chip is selected. */
isSelected() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).hasClass('mat-chip-selected');
});
}
/** Selects the given chip. Only applies if it's selectable. */
select() {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield this.isSelected())) {
yield this.toggle();
}
});
}
/** Deselects the given chip. Only applies if it's selectable. */
deselect() {
return __awaiter(this, void 0, void 0, function* () {
if (yield this.isSelected()) {
yield this.toggle();
}
});
}
/** Toggles the selected state of the given chip. */
toggle() {
return __awaiter(this, void 0, void 0, function* () {
return (yield this.host()).sendKeys(' ');
});
}
}
/** The selector for the host element of a selectable chip instance. */
MatChipOptionHarness.hostSelector = '.mat-chip';
/**
* @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
*/
/** Harness for interacting with a standard selectable chip list in tests. */
class MatChipListboxHarness extends _MatChipListHarnessBase {
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatChipListHarness` that meets
* certain criteria.
* @param options Options for filtering which chip list instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(MatChipListboxHarness, options);
}
/**
* Gets the list of chips inside the chip list.
* @param filter Optionally filters which chips are included.
*/
getChips(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
return this.locatorForAll(MatChipOptionHarness.with(filter))();
});
}
/**
* Selects a chip inside the chip list.
* @param filter An optional filter to apply to the child chips.
* All the chips matching the filter will be selected.
*/
selectChips(filter = {}) {
return __awaiter(this, void 0, void 0, function* () {
const chips = yield this.getChips(filter);
if (!chips.length) {
throw Error(`Cannot find chip matching filter ${JSON.stringify(filter)}`);
}
yield parallel(() => chips.map(chip => chip.select()));
});
}
}
/** The selector for the host element of a `MatChipList` instance. */
MatChipListboxHarness.hostSelector = '.mat-chip-list';
/**
* @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 { MatChipHarness, MatChipInputHarness, MatChipListHarness, MatChipListboxHarness, MatChipOptionHarness, MatChipRemoveHarness };
//# sourceMappingURL=testing.js.map