1 | import { __awaiter } from 'tslib';
|
---|
2 | import { parallel, HarnessPredicate } from '@angular/cdk/testing';
|
---|
3 | import { MatFormFieldControlHarness } from '@angular/material/form-field/testing/control';
|
---|
4 | import { MatOptionHarness, MatOptgroupHarness } from '@angular/material/core/testing';
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @license
|
---|
8 | * Copyright Google LLC All Rights Reserved.
|
---|
9 | *
|
---|
10 | * Use of this source code is governed by an MIT-style license that can be
|
---|
11 | * found in the LICENSE file at https://angular.io/license
|
---|
12 | */
|
---|
13 | class _MatSelectHarnessBase extends MatFormFieldControlHarness {
|
---|
14 | constructor() {
|
---|
15 | super(...arguments);
|
---|
16 | this._documentRootLocator = this.documentRootLocatorFactory();
|
---|
17 | this._backdrop = this._documentRootLocator.locatorFor('.cdk-overlay-backdrop');
|
---|
18 | }
|
---|
19 | /** Gets a boolean promise indicating if the select is disabled. */
|
---|
20 | isDisabled() {
|
---|
21 | return __awaiter(this, void 0, void 0, function* () {
|
---|
22 | return (yield this.host()).hasClass(`${this._prefix}-select-disabled`);
|
---|
23 | });
|
---|
24 | }
|
---|
25 | /** Gets a boolean promise indicating if the select is valid. */
|
---|
26 | isValid() {
|
---|
27 | return __awaiter(this, void 0, void 0, function* () {
|
---|
28 | return !(yield (yield this.host()).hasClass('ng-invalid'));
|
---|
29 | });
|
---|
30 | }
|
---|
31 | /** Gets a boolean promise indicating if the select is required. */
|
---|
32 | isRequired() {
|
---|
33 | return __awaiter(this, void 0, void 0, function* () {
|
---|
34 | return (yield this.host()).hasClass(`${this._prefix}-select-required`);
|
---|
35 | });
|
---|
36 | }
|
---|
37 | /** Gets a boolean promise indicating if the select is empty (no value is selected). */
|
---|
38 | isEmpty() {
|
---|
39 | return __awaiter(this, void 0, void 0, function* () {
|
---|
40 | return (yield this.host()).hasClass(`${this._prefix}-select-empty`);
|
---|
41 | });
|
---|
42 | }
|
---|
43 | /** Gets a boolean promise indicating if the select is in multi-selection mode. */
|
---|
44 | isMultiple() {
|
---|
45 | return __awaiter(this, void 0, void 0, function* () {
|
---|
46 | return (yield this.host()).hasClass(`${this._prefix}-select-multiple`);
|
---|
47 | });
|
---|
48 | }
|
---|
49 | /** Gets a promise for the select's value text. */
|
---|
50 | getValueText() {
|
---|
51 | return __awaiter(this, void 0, void 0, function* () {
|
---|
52 | const value = yield this.locatorFor(`.${this._prefix}-select-value`)();
|
---|
53 | return value.text();
|
---|
54 | });
|
---|
55 | }
|
---|
56 | /** Focuses the select and returns a void promise that indicates when the action is complete. */
|
---|
57 | focus() {
|
---|
58 | return __awaiter(this, void 0, void 0, function* () {
|
---|
59 | return (yield this.host()).focus();
|
---|
60 | });
|
---|
61 | }
|
---|
62 | /** Blurs the select and returns a void promise that indicates when the action is complete. */
|
---|
63 | blur() {
|
---|
64 | return __awaiter(this, void 0, void 0, function* () {
|
---|
65 | return (yield this.host()).blur();
|
---|
66 | });
|
---|
67 | }
|
---|
68 | /** Whether the select is focused. */
|
---|
69 | isFocused() {
|
---|
70 | return __awaiter(this, void 0, void 0, function* () {
|
---|
71 | return (yield this.host()).isFocused();
|
---|
72 | });
|
---|
73 | }
|
---|
74 | /** Gets the options inside the select panel. */
|
---|
75 | getOptions(filter) {
|
---|
76 | return __awaiter(this, void 0, void 0, function* () {
|
---|
77 | return this._documentRootLocator.locatorForAll(this._optionClass.with(Object.assign(Object.assign({}, (filter || {})), { ancestor: yield this._getPanelSelector() })))();
|
---|
78 | });
|
---|
79 | }
|
---|
80 | /** Gets the groups of options inside the panel. */
|
---|
81 | getOptionGroups(filter) {
|
---|
82 | return __awaiter(this, void 0, void 0, function* () {
|
---|
83 | return this._documentRootLocator.locatorForAll(this._optionGroupClass.with(Object.assign(Object.assign({}, (filter || {})), { ancestor: yield this._getPanelSelector() })))();
|
---|
84 | });
|
---|
85 | }
|
---|
86 | /** Gets whether the select is open. */
|
---|
87 | isOpen() {
|
---|
88 | return __awaiter(this, void 0, void 0, function* () {
|
---|
89 | return !!(yield this._documentRootLocator.locatorForOptional(yield this._getPanelSelector())());
|
---|
90 | });
|
---|
91 | }
|
---|
92 | /** Opens the select's panel. */
|
---|
93 | open() {
|
---|
94 | return __awaiter(this, void 0, void 0, function* () {
|
---|
95 | if (!(yield this.isOpen())) {
|
---|
96 | const trigger = yield this.locatorFor(`.${this._prefix}-select-trigger`)();
|
---|
97 | return trigger.click();
|
---|
98 | }
|
---|
99 | });
|
---|
100 | }
|
---|
101 | /**
|
---|
102 | * Clicks the options that match the passed-in filter. If the select is in multi-selection
|
---|
103 | * mode all options will be clicked, otherwise the harness will pick the first matching option.
|
---|
104 | */
|
---|
105 | clickOptions(filter) {
|
---|
106 | return __awaiter(this, void 0, void 0, function* () {
|
---|
107 | yield this.open();
|
---|
108 | const [isMultiple, options] = yield parallel(() => [this.isMultiple(), this.getOptions(filter)]);
|
---|
109 | if (options.length === 0) {
|
---|
110 | throw Error('Select does not have options matching the specified filter');
|
---|
111 | }
|
---|
112 | if (isMultiple) {
|
---|
113 | yield parallel(() => options.map(option => option.click()));
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | yield options[0].click();
|
---|
117 | }
|
---|
118 | });
|
---|
119 | }
|
---|
120 | /** Closes the select's panel. */
|
---|
121 | close() {
|
---|
122 | return __awaiter(this, void 0, void 0, function* () {
|
---|
123 | if (yield this.isOpen()) {
|
---|
124 | // This is the most consistent way that works both in both single and multi-select modes,
|
---|
125 | // but it assumes that only one overlay is open at a time. We should be able to make it
|
---|
126 | // a bit more precise after #16645 where we can dispatch an ESCAPE press to the host instead.
|
---|
127 | return (yield this._backdrop()).click();
|
---|
128 | }
|
---|
129 | });
|
---|
130 | }
|
---|
131 | /** Gets the selector that should be used to find this select's panel. */
|
---|
132 | _getPanelSelector() {
|
---|
133 | return __awaiter(this, void 0, void 0, function* () {
|
---|
134 | const id = yield (yield this.host()).getAttribute('id');
|
---|
135 | return `#${id}-panel`;
|
---|
136 | });
|
---|
137 | }
|
---|
138 | }
|
---|
139 | /** Harness for interacting with a standard mat-select in tests. */
|
---|
140 | class MatSelectHarness extends _MatSelectHarnessBase {
|
---|
141 | constructor() {
|
---|
142 | super(...arguments);
|
---|
143 | this._prefix = 'mat';
|
---|
144 | this._optionClass = MatOptionHarness;
|
---|
145 | this._optionGroupClass = MatOptgroupHarness;
|
---|
146 | }
|
---|
147 | /**
|
---|
148 | * Gets a `HarnessPredicate` that can be used to search for a `MatSelectHarness` that meets
|
---|
149 | * certain criteria.
|
---|
150 | * @param options Options for filtering which select instances are considered a match.
|
---|
151 | * @return a `HarnessPredicate` configured with the given options.
|
---|
152 | */
|
---|
153 | static with(options = {}) {
|
---|
154 | return new HarnessPredicate(MatSelectHarness, options);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | MatSelectHarness.hostSelector = '.mat-select';
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * @license
|
---|
161 | * Copyright Google LLC All Rights Reserved.
|
---|
162 | *
|
---|
163 | * Use of this source code is governed by an MIT-style license that can be
|
---|
164 | * found in the LICENSE file at https://angular.io/license
|
---|
165 | */
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * @license
|
---|
169 | * Copyright Google LLC All Rights Reserved.
|
---|
170 | *
|
---|
171 | * Use of this source code is governed by an MIT-style license that can be
|
---|
172 | * found in the LICENSE file at https://angular.io/license
|
---|
173 | */
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * @license
|
---|
177 | * Copyright Google LLC All Rights Reserved.
|
---|
178 | *
|
---|
179 | * Use of this source code is governed by an MIT-style license that can be
|
---|
180 | * found in the LICENSE file at https://angular.io/license
|
---|
181 | */
|
---|
182 |
|
---|
183 | export { MatSelectHarness, _MatSelectHarnessBase };
|
---|
184 | //# sourceMappingURL=testing.js.map
|
---|