1 | import { __awaiter } from 'tslib';
|
---|
2 | import { ContentContainerComponentHarness, TestKey, HarnessPredicate } from '@angular/cdk/testing';
|
---|
3 | import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @license
|
---|
7 | * Copyright Google LLC All Rights Reserved.
|
---|
8 | *
|
---|
9 | * Use of this source code is governed by an MIT-style license that can be
|
---|
10 | * found in the LICENSE file at https://angular.io/license
|
---|
11 | */
|
---|
12 | class _MatMenuHarnessBase extends ContentContainerComponentHarness {
|
---|
13 | constructor() {
|
---|
14 | super(...arguments);
|
---|
15 | this._documentRootLocator = this.documentRootLocatorFactory();
|
---|
16 | }
|
---|
17 | // TODO: potentially extend MatButtonHarness
|
---|
18 | /** Whether the menu is disabled. */
|
---|
19 | isDisabled() {
|
---|
20 | return __awaiter(this, void 0, void 0, function* () {
|
---|
21 | const disabled = (yield this.host()).getAttribute('disabled');
|
---|
22 | return coerceBooleanProperty(yield disabled);
|
---|
23 | });
|
---|
24 | }
|
---|
25 | /** Whether the menu is open. */
|
---|
26 | isOpen() {
|
---|
27 | return __awaiter(this, void 0, void 0, function* () {
|
---|
28 | return !!(yield this._getMenuPanel());
|
---|
29 | });
|
---|
30 | }
|
---|
31 | /** Gets the text of the menu's trigger element. */
|
---|
32 | getTriggerText() {
|
---|
33 | return __awaiter(this, void 0, void 0, function* () {
|
---|
34 | return (yield this.host()).text();
|
---|
35 | });
|
---|
36 | }
|
---|
37 | /** Focuses the menu. */
|
---|
38 | focus() {
|
---|
39 | return __awaiter(this, void 0, void 0, function* () {
|
---|
40 | return (yield this.host()).focus();
|
---|
41 | });
|
---|
42 | }
|
---|
43 | /** Blurs the menu. */
|
---|
44 | blur() {
|
---|
45 | return __awaiter(this, void 0, void 0, function* () {
|
---|
46 | return (yield this.host()).blur();
|
---|
47 | });
|
---|
48 | }
|
---|
49 | /** Whether the menu is focused. */
|
---|
50 | isFocused() {
|
---|
51 | return __awaiter(this, void 0, void 0, function* () {
|
---|
52 | return (yield this.host()).isFocused();
|
---|
53 | });
|
---|
54 | }
|
---|
55 | /** Opens the menu. */
|
---|
56 | open() {
|
---|
57 | return __awaiter(this, void 0, void 0, function* () {
|
---|
58 | if (!(yield this.isOpen())) {
|
---|
59 | return (yield this.host()).click();
|
---|
60 | }
|
---|
61 | });
|
---|
62 | }
|
---|
63 | /** Closes the menu. */
|
---|
64 | close() {
|
---|
65 | return __awaiter(this, void 0, void 0, function* () {
|
---|
66 | const panel = yield this._getMenuPanel();
|
---|
67 | if (panel) {
|
---|
68 | return panel.sendKeys(TestKey.ESCAPE);
|
---|
69 | }
|
---|
70 | });
|
---|
71 | }
|
---|
72 | /**
|
---|
73 | * Gets a list of `MatMenuItemHarness` representing the items in the menu.
|
---|
74 | * @param filters Optionally filters which menu items are included.
|
---|
75 | */
|
---|
76 | getItems(filters) {
|
---|
77 | return __awaiter(this, void 0, void 0, function* () {
|
---|
78 | const panelId = yield this._getPanelId();
|
---|
79 | if (panelId) {
|
---|
80 | return this._documentRootLocator.locatorForAll(this._itemClass.with(Object.assign(Object.assign({}, (filters || {})), { ancestor: `#${panelId}` })))();
|
---|
81 | }
|
---|
82 | return [];
|
---|
83 | });
|
---|
84 | }
|
---|
85 | /**
|
---|
86 | * Clicks an item in the menu, and optionally continues clicking items in subsequent sub-menus.
|
---|
87 | * @param itemFilter A filter used to represent which item in the menu should be clicked. The
|
---|
88 | * first matching menu item will be clicked.
|
---|
89 | * @param subItemFilters A list of filters representing the items to click in any subsequent
|
---|
90 | * sub-menus. The first item in the sub-menu matching the corresponding filter in
|
---|
91 | * `subItemFilters` will be clicked.
|
---|
92 | */
|
---|
93 | clickItem(itemFilter, ...subItemFilters) {
|
---|
94 | return __awaiter(this, void 0, void 0, function* () {
|
---|
95 | yield this.open();
|
---|
96 | const items = yield this.getItems(itemFilter);
|
---|
97 | if (!items.length) {
|
---|
98 | throw Error(`Could not find item matching ${JSON.stringify(itemFilter)}`);
|
---|
99 | }
|
---|
100 | if (!subItemFilters.length) {
|
---|
101 | return yield items[0].click();
|
---|
102 | }
|
---|
103 | const menu = yield items[0].getSubmenu();
|
---|
104 | if (!menu) {
|
---|
105 | throw Error(`Item matching ${JSON.stringify(itemFilter)} does not have a submenu`);
|
---|
106 | }
|
---|
107 | return menu.clickItem(...subItemFilters);
|
---|
108 | });
|
---|
109 | }
|
---|
110 | getRootHarnessLoader() {
|
---|
111 | return __awaiter(this, void 0, void 0, function* () {
|
---|
112 | const panelId = yield this._getPanelId();
|
---|
113 | return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`);
|
---|
114 | });
|
---|
115 | }
|
---|
116 | /** Gets the menu panel associated with this menu. */
|
---|
117 | _getMenuPanel() {
|
---|
118 | return __awaiter(this, void 0, void 0, function* () {
|
---|
119 | const panelId = yield this._getPanelId();
|
---|
120 | return panelId ? this._documentRootLocator.locatorForOptional(`#${panelId}`)() : null;
|
---|
121 | });
|
---|
122 | }
|
---|
123 | /** Gets the id of the menu panel associated with this menu. */
|
---|
124 | _getPanelId() {
|
---|
125 | return __awaiter(this, void 0, void 0, function* () {
|
---|
126 | const panelId = yield (yield this.host()).getAttribute('aria-controls');
|
---|
127 | return panelId || null;
|
---|
128 | });
|
---|
129 | }
|
---|
130 | }
|
---|
131 | class _MatMenuItemHarnessBase extends ContentContainerComponentHarness {
|
---|
132 | /** Whether the menu is disabled. */
|
---|
133 | isDisabled() {
|
---|
134 | return __awaiter(this, void 0, void 0, function* () {
|
---|
135 | const disabled = (yield this.host()).getAttribute('disabled');
|
---|
136 | return coerceBooleanProperty(yield disabled);
|
---|
137 | });
|
---|
138 | }
|
---|
139 | /** Gets the text of the menu item. */
|
---|
140 | getText() {
|
---|
141 | return __awaiter(this, void 0, void 0, function* () {
|
---|
142 | return (yield this.host()).text();
|
---|
143 | });
|
---|
144 | }
|
---|
145 | /** Focuses the menu item. */
|
---|
146 | focus() {
|
---|
147 | return __awaiter(this, void 0, void 0, function* () {
|
---|
148 | return (yield this.host()).focus();
|
---|
149 | });
|
---|
150 | }
|
---|
151 | /** Blurs the menu item. */
|
---|
152 | blur() {
|
---|
153 | return __awaiter(this, void 0, void 0, function* () {
|
---|
154 | return (yield this.host()).blur();
|
---|
155 | });
|
---|
156 | }
|
---|
157 | /** Whether the menu item is focused. */
|
---|
158 | isFocused() {
|
---|
159 | return __awaiter(this, void 0, void 0, function* () {
|
---|
160 | return (yield this.host()).isFocused();
|
---|
161 | });
|
---|
162 | }
|
---|
163 | /** Clicks the menu item. */
|
---|
164 | click() {
|
---|
165 | return __awaiter(this, void 0, void 0, function* () {
|
---|
166 | return (yield this.host()).click();
|
---|
167 | });
|
---|
168 | }
|
---|
169 | /** Whether this item has a submenu. */
|
---|
170 | hasSubmenu() {
|
---|
171 | return __awaiter(this, void 0, void 0, function* () {
|
---|
172 | return (yield this.host()).matchesSelector(this._menuClass.hostSelector);
|
---|
173 | });
|
---|
174 | }
|
---|
175 | /** Gets the submenu associated with this menu item, or null if none. */
|
---|
176 | getSubmenu() {
|
---|
177 | return __awaiter(this, void 0, void 0, function* () {
|
---|
178 | if (yield this.hasSubmenu()) {
|
---|
179 | return new this._menuClass(this.locatorFactory);
|
---|
180 | }
|
---|
181 | return null;
|
---|
182 | });
|
---|
183 | }
|
---|
184 | }
|
---|
185 | /** Harness for interacting with a standard mat-menu in tests. */
|
---|
186 | class MatMenuHarness extends _MatMenuHarnessBase {
|
---|
187 | constructor() {
|
---|
188 | super(...arguments);
|
---|
189 | this._itemClass = MatMenuItemHarness;
|
---|
190 | }
|
---|
191 | /**
|
---|
192 | * Gets a `HarnessPredicate` that can be used to search for a `MatMenuHarness` that meets certain
|
---|
193 | * criteria.
|
---|
194 | * @param options Options for filtering which menu instances are considered a match.
|
---|
195 | * @return a `HarnessPredicate` configured with the given options.
|
---|
196 | */
|
---|
197 | static with(options = {}) {
|
---|
198 | return new HarnessPredicate(MatMenuHarness, options)
|
---|
199 | .addOption('triggerText', options.triggerText, (harness, text) => HarnessPredicate.stringMatches(harness.getTriggerText(), text));
|
---|
200 | }
|
---|
201 | }
|
---|
202 | /** The selector for the host element of a `MatMenu` instance. */
|
---|
203 | MatMenuHarness.hostSelector = '.mat-menu-trigger';
|
---|
204 | /** Harness for interacting with a standard mat-menu-item in tests. */
|
---|
205 | class MatMenuItemHarness extends _MatMenuItemHarnessBase {
|
---|
206 | constructor() {
|
---|
207 | super(...arguments);
|
---|
208 | this._menuClass = MatMenuHarness;
|
---|
209 | }
|
---|
210 | /**
|
---|
211 | * Gets a `HarnessPredicate` that can be used to search for a `MatMenuItemHarness` that meets
|
---|
212 | * certain criteria.
|
---|
213 | * @param options Options for filtering which menu item instances are considered a match.
|
---|
214 | * @return a `HarnessPredicate` configured with the given options.
|
---|
215 | */
|
---|
216 | static with(options = {}) {
|
---|
217 | return new HarnessPredicate(MatMenuItemHarness, options)
|
---|
218 | .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
|
---|
219 | .addOption('hasSubmenu', options.hasSubmenu, (harness, hasSubmenu) => __awaiter(this, void 0, void 0, function* () { return (yield harness.hasSubmenu()) === hasSubmenu; }));
|
---|
220 | }
|
---|
221 | }
|
---|
222 | /** The selector for the host element of a `MatMenuItem` instance. */
|
---|
223 | MatMenuItemHarness.hostSelector = '.mat-menu-item';
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * @license
|
---|
227 | * Copyright Google LLC All Rights Reserved.
|
---|
228 | *
|
---|
229 | * Use of this source code is governed by an MIT-style license that can be
|
---|
230 | * found in the LICENSE file at https://angular.io/license
|
---|
231 | */
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * @license
|
---|
235 | * Copyright Google LLC All Rights Reserved.
|
---|
236 | *
|
---|
237 | * Use of this source code is governed by an MIT-style license that can be
|
---|
238 | * found in the LICENSE file at https://angular.io/license
|
---|
239 | */
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * @license
|
---|
243 | * Copyright Google LLC All Rights Reserved.
|
---|
244 | *
|
---|
245 | * Use of this source code is governed by an MIT-style license that can be
|
---|
246 | * found in the LICENSE file at https://angular.io/license
|
---|
247 | */
|
---|
248 |
|
---|
249 | export { MatMenuHarness, MatMenuItemHarness, _MatMenuHarnessBase, _MatMenuItemHarnessBase };
|
---|
250 | //# sourceMappingURL=testing.js.map
|
---|