1 | import { ContentContainerComponentHarness, TestKey, HarnessPredicate } from '@angular/cdk/testing';
|
2 | import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | class _MatMenuHarnessBase extends ContentContainerComponentHarness {
|
12 | constructor() {
|
13 | super(...arguments);
|
14 | this._documentRootLocator = this.documentRootLocatorFactory();
|
15 | }
|
16 |
|
17 |
|
18 | async isDisabled() {
|
19 | const disabled = (await this.host()).getAttribute('disabled');
|
20 | return coerceBooleanProperty(await disabled);
|
21 | }
|
22 |
|
23 | async isOpen() {
|
24 | return !!(await this._getMenuPanel());
|
25 | }
|
26 |
|
27 | async getTriggerText() {
|
28 | return (await this.host()).text();
|
29 | }
|
30 |
|
31 | async focus() {
|
32 | return (await this.host()).focus();
|
33 | }
|
34 |
|
35 | async blur() {
|
36 | return (await this.host()).blur();
|
37 | }
|
38 |
|
39 | async isFocused() {
|
40 | return (await this.host()).isFocused();
|
41 | }
|
42 |
|
43 | async open() {
|
44 | if (!(await this.isOpen())) {
|
45 | return (await this.host()).click();
|
46 | }
|
47 | }
|
48 |
|
49 | async close() {
|
50 | const panel = await this._getMenuPanel();
|
51 | if (panel) {
|
52 | return panel.sendKeys(TestKey.ESCAPE);
|
53 | }
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 | async getItems(filters) {
|
60 | const panelId = await this._getPanelId();
|
61 | if (panelId) {
|
62 | return this._documentRootLocator.locatorForAll(this._itemClass.with({
|
63 | ...(filters || {}),
|
64 | ancestor: `#${panelId}`,
|
65 | }))();
|
66 | }
|
67 | return [];
|
68 | }
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | async clickItem(itemFilter, ...subItemFilters) {
|
78 | await this.open();
|
79 | const items = await this.getItems(itemFilter);
|
80 | if (!items.length) {
|
81 | throw Error(`Could not find item matching ${JSON.stringify(itemFilter)}`);
|
82 | }
|
83 | if (!subItemFilters.length) {
|
84 | return await items[0].click();
|
85 | }
|
86 | const menu = await items[0].getSubmenu();
|
87 | if (!menu) {
|
88 | throw Error(`Item matching ${JSON.stringify(itemFilter)} does not have a submenu`);
|
89 | }
|
90 | return menu.clickItem(...subItemFilters);
|
91 | }
|
92 | async getRootHarnessLoader() {
|
93 | const panelId = await this._getPanelId();
|
94 | return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`);
|
95 | }
|
96 |
|
97 | async _getMenuPanel() {
|
98 | const panelId = await this._getPanelId();
|
99 | return panelId ? this._documentRootLocator.locatorForOptional(`#${panelId}`)() : null;
|
100 | }
|
101 |
|
102 | async _getPanelId() {
|
103 | const panelId = await (await this.host()).getAttribute('aria-controls');
|
104 | return panelId || null;
|
105 | }
|
106 | }
|
107 | class _MatMenuItemHarnessBase extends ContentContainerComponentHarness {
|
108 |
|
109 | async isDisabled() {
|
110 | const disabled = (await this.host()).getAttribute('disabled');
|
111 | return coerceBooleanProperty(await disabled);
|
112 | }
|
113 |
|
114 | async getText() {
|
115 | return (await this.host()).text();
|
116 | }
|
117 |
|
118 | async focus() {
|
119 | return (await this.host()).focus();
|
120 | }
|
121 |
|
122 | async blur() {
|
123 | return (await this.host()).blur();
|
124 | }
|
125 |
|
126 | async isFocused() {
|
127 | return (await this.host()).isFocused();
|
128 | }
|
129 |
|
130 | async click() {
|
131 | return (await this.host()).click();
|
132 | }
|
133 |
|
134 | async hasSubmenu() {
|
135 | return (await this.host()).matchesSelector(this._menuClass.hostSelector);
|
136 | }
|
137 |
|
138 | async getSubmenu() {
|
139 | if (await this.hasSubmenu()) {
|
140 | return new this._menuClass(this.locatorFactory);
|
141 | }
|
142 | return null;
|
143 | }
|
144 | }
|
145 |
|
146 | class MatMenuHarness extends _MatMenuHarnessBase {
|
147 | constructor() {
|
148 | super(...arguments);
|
149 | this._itemClass = MatMenuItemHarness;
|
150 | }
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | static with(options = {}) {
|
158 | return new HarnessPredicate(MatMenuHarness, options).addOption('triggerText', options.triggerText, (harness, text) => HarnessPredicate.stringMatches(harness.getTriggerText(), text));
|
159 | }
|
160 | }
|
161 |
|
162 | MatMenuHarness.hostSelector = '.mat-menu-trigger';
|
163 |
|
164 | class MatMenuItemHarness extends _MatMenuItemHarnessBase {
|
165 | constructor() {
|
166 | super(...arguments);
|
167 | this._menuClass = MatMenuHarness;
|
168 | }
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | static with(options = {}) {
|
176 | return new HarnessPredicate(MatMenuItemHarness, options)
|
177 | .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
|
178 | .addOption('hasSubmenu', options.hasSubmenu, async (harness, hasSubmenu) => (await harness.hasSubmenu()) === hasSubmenu);
|
179 | }
|
180 | }
|
181 |
|
182 | MatMenuItemHarness.hostSelector = '.mat-menu-item';
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | export { MatMenuHarness, MatMenuItemHarness, _MatMenuHarnessBase, _MatMenuItemHarnessBase };
|
209 |
|