UNPKG

@angular/material

Version:
246 lines (241 loc) 10.4 kB
import { __extends, __awaiter, __generator } from 'tslib'; import { HarnessPredicate, ComponentHarness } from '@angular/cdk/testing'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; /** * @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 mat-slide-toggle in tests. */ var MatSlideToggleHarness = /** @class */ (function (_super) { __extends(MatSlideToggleHarness, _super); function MatSlideToggleHarness() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._label = _this.locatorFor('label'); _this._input = _this.locatorFor('input'); _this._inputContainer = _this.locatorFor('.mat-slide-toggle-bar'); return _this; } /** * Gets a `HarnessPredicate` that can be used to search for a `MatSlideToggleHarness` that meets * certain criteria. * @param options Options for filtering which slide toggle instances are considered a match. * @return a `HarnessPredicate` configured with the given options. */ MatSlideToggleHarness.with = function (options) { var _this = this; if (options === void 0) { options = {}; } return new HarnessPredicate(MatSlideToggleHarness, options) .addOption('label', options.label, function (harness, label) { return HarnessPredicate.stringMatches(harness.getLabelText(), label); }) // We want to provide a filter option for "name" because the name of the slide-toggle is // only set on the underlying input. This means that it's not possible for developers // to retrieve the harness of a specific checkbox with name through a CSS selector. .addOption('name', options.name, function (harness, name) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, harness.getName()]; case 1: return [2 /*return*/, (_a.sent()) === name]; } }); }); }); }; /** Whether the slide-toggle is checked. */ MatSlideToggleHarness.prototype.isChecked = function () { return __awaiter(this, void 0, void 0, function () { var checked, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this._input()]; case 1: checked = (_b.sent()).getProperty('checked'); _a = coerceBooleanProperty; return [4 /*yield*/, checked]; case 2: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; } }); }); }; /** Whether the slide-toggle is disabled. */ MatSlideToggleHarness.prototype.isDisabled = function () { return __awaiter(this, void 0, void 0, function () { var disabled, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this._input()]; case 1: disabled = (_b.sent()).getAttribute('disabled'); _a = coerceBooleanProperty; return [4 /*yield*/, disabled]; case 2: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; } }); }); }; /** Whether the slide-toggle is required. */ MatSlideToggleHarness.prototype.isRequired = function () { return __awaiter(this, void 0, void 0, function () { var required, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this._input()]; case 1: required = (_b.sent()).getAttribute('required'); _a = coerceBooleanProperty; return [4 /*yield*/, required]; case 2: return [2 /*return*/, _a.apply(void 0, [_b.sent()])]; } }); }); }; /** Whether the slide-toggle is valid. */ MatSlideToggleHarness.prototype.isValid = function () { return __awaiter(this, void 0, void 0, function () { var invalid; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.host()]; case 1: invalid = (_a.sent()).hasClass('ng-invalid'); return [4 /*yield*/, invalid]; case 2: return [2 /*return*/, !(_a.sent())]; } }); }); }; /** Gets the slide-toggle's name. */ MatSlideToggleHarness.prototype.getName = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._input()]; case 1: return [2 /*return*/, (_a.sent()).getAttribute('name')]; } }); }); }; /** Gets the slide-toggle's aria-label. */ MatSlideToggleHarness.prototype.getAriaLabel = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._input()]; case 1: return [2 /*return*/, (_a.sent()).getAttribute('aria-label')]; } }); }); }; /** Gets the slide-toggle's aria-labelledby. */ MatSlideToggleHarness.prototype.getAriaLabelledby = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._input()]; case 1: return [2 /*return*/, (_a.sent()).getAttribute('aria-labelledby')]; } }); }); }; /** Gets the slide-toggle's label text. */ MatSlideToggleHarness.prototype.getLabelText = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._label()]; case 1: return [2 /*return*/, (_a.sent()).text()]; } }); }); }; /** Focuses the slide-toggle. */ MatSlideToggleHarness.prototype.focus = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._input()]; case 1: return [2 /*return*/, (_a.sent()).focus()]; } }); }); }; /** Blurs the slide-toggle. */ MatSlideToggleHarness.prototype.blur = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._input()]; case 1: return [2 /*return*/, (_a.sent()).blur()]; } }); }); }; /** Toggle the checked state of the slide-toggle. */ MatSlideToggleHarness.prototype.toggle = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._inputContainer()]; case 1: return [2 /*return*/, (_a.sent()).click()]; } }); }); }; /** * Puts the slide-toggle in a checked state by toggling it if it is currently unchecked, or doing * nothing if it is already checked. */ MatSlideToggleHarness.prototype.check = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.isChecked()]; case 1: if (!!(_a.sent())) return [3 /*break*/, 3]; return [4 /*yield*/, this.toggle()]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); }; /** * Puts the slide-toggle in an unchecked state by toggling it if it is currently checked, or doing * nothing if it is already unchecked. */ MatSlideToggleHarness.prototype.uncheck = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.isChecked()]; case 1: if (!_a.sent()) return [3 /*break*/, 3]; return [4 /*yield*/, this.toggle()]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); }; /** The selector for the host element of a `MatSlideToggle` instance. */ MatSlideToggleHarness.hostSelector = 'mat-slide-toggle'; return MatSlideToggleHarness; }(ComponentHarness)); /** * @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 { MatSlideToggleHarness }; //# sourceMappingURL=testing.js.map