1 | import { __awaiter } from 'tslib';
|
2 | import { HarnessPredicate, ComponentHarness, parallel, TestKey } from '@angular/cdk/testing';
|
3 | import { MatFormFieldControlHarness } from '@angular/material/form-field/testing/control';
|
4 | import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
5 |
|
6 |
|
7 | function getInputPredicate(type, options) {
|
8 | return new HarnessPredicate(type, options)
|
9 | .addOption('value', options.value, (harness, value) => {
|
10 | return HarnessPredicate.stringMatches(harness.getValue(), value);
|
11 | })
|
12 | .addOption('placeholder', options.placeholder, (harness, placeholder) => {
|
13 | return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
|
14 | });
|
15 | }
|
16 |
|
17 | class MatDatepickerInputHarnessBase extends MatFormFieldControlHarness {
|
18 |
|
19 | isDisabled() {
|
20 | return __awaiter(this, void 0, void 0, function* () {
|
21 | return (yield this.host()).getProperty('disabled');
|
22 | });
|
23 | }
|
24 |
|
25 | isRequired() {
|
26 | return __awaiter(this, void 0, void 0, function* () {
|
27 | return (yield this.host()).getProperty('required');
|
28 | });
|
29 | }
|
30 |
|
31 | getValue() {
|
32 | return __awaiter(this, void 0, void 0, function* () {
|
33 |
|
34 | return yield (yield this.host()).getProperty('value');
|
35 | });
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
41 | setValue(newValue) {
|
42 | return __awaiter(this, void 0, void 0, function* () {
|
43 | const inputEl = yield this.host();
|
44 | yield inputEl.clear();
|
45 |
|
46 |
|
47 |
|
48 | if (newValue) {
|
49 | yield inputEl.sendKeys(newValue);
|
50 | }
|
51 | yield inputEl.dispatchEvent('change');
|
52 | });
|
53 | }
|
54 |
|
55 | getPlaceholder() {
|
56 | return __awaiter(this, void 0, void 0, function* () {
|
57 | return yield (yield this.host()).getProperty('placeholder');
|
58 | });
|
59 | }
|
60 | |
61 |
|
62 |
|
63 |
|
64 | focus() {
|
65 | return __awaiter(this, void 0, void 0, function* () {
|
66 | return (yield this.host()).focus();
|
67 | });
|
68 | }
|
69 | |
70 |
|
71 |
|
72 |
|
73 | blur() {
|
74 | return __awaiter(this, void 0, void 0, function* () {
|
75 | return (yield this.host()).blur();
|
76 | });
|
77 | }
|
78 |
|
79 | isFocused() {
|
80 | return __awaiter(this, void 0, void 0, function* () {
|
81 | return (yield this.host()).isFocused();
|
82 | });
|
83 | }
|
84 |
|
85 | getMin() {
|
86 | return __awaiter(this, void 0, void 0, function* () {
|
87 | return (yield this.host()).getAttribute('min');
|
88 | });
|
89 | }
|
90 |
|
91 | getMax() {
|
92 | return __awaiter(this, void 0, void 0, function* () {
|
93 | return (yield this.host()).getAttribute('max');
|
94 | });
|
95 | }
|
96 | }
|
97 |
|
98 |
|
99 | class MatCalendarCellHarness extends ComponentHarness {
|
100 | constructor() {
|
101 | super(...arguments);
|
102 |
|
103 | this._content = this.locatorFor('.mat-calendar-body-cell-content');
|
104 | }
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | static with(options = {}) {
|
112 | return new HarnessPredicate(MatCalendarCellHarness, options)
|
113 | .addOption('text', options.text, (harness, text) => {
|
114 | return HarnessPredicate.stringMatches(harness.getText(), text);
|
115 | })
|
116 | .addOption('selected', options.selected, (harness, selected) => __awaiter(this, void 0, void 0, function* () {
|
117 | return (yield harness.isSelected()) === selected;
|
118 | }))
|
119 | .addOption('active', options.active, (harness, active) => __awaiter(this, void 0, void 0, function* () {
|
120 | return (yield harness.isActive()) === active;
|
121 | }))
|
122 | .addOption('disabled', options.disabled, (harness, disabled) => __awaiter(this, void 0, void 0, function* () {
|
123 | return (yield harness.isDisabled()) === disabled;
|
124 | }))
|
125 | .addOption('today', options.today, (harness, today) => __awaiter(this, void 0, void 0, function* () {
|
126 | return (yield harness.isToday()) === today;
|
127 | }))
|
128 | .addOption('inRange', options.inRange, (harness, inRange) => __awaiter(this, void 0, void 0, function* () {
|
129 | return (yield harness.isInRange()) === inRange;
|
130 | }))
|
131 | .addOption('inComparisonRange', options.inComparisonRange, (harness, inComparisonRange) => __awaiter(this, void 0, void 0, function* () {
|
132 | return (yield harness.isInComparisonRange()) === inComparisonRange;
|
133 | }))
|
134 | .addOption('inPreviewRange', options.inPreviewRange, (harness, inPreviewRange) => __awaiter(this, void 0, void 0, function* () {
|
135 | return (yield harness.isInPreviewRange()) === inPreviewRange;
|
136 | }));
|
137 | }
|
138 |
|
139 | getText() {
|
140 | return __awaiter(this, void 0, void 0, function* () {
|
141 | return (yield this._content()).text();
|
142 | });
|
143 | }
|
144 |
|
145 | getAriaLabel() {
|
146 | return __awaiter(this, void 0, void 0, function* () {
|
147 |
|
148 |
|
149 | return (yield this.host()).getAttribute('aria-label');
|
150 | });
|
151 | }
|
152 |
|
153 | isSelected() {
|
154 | return __awaiter(this, void 0, void 0, function* () {
|
155 | const host = yield this.host();
|
156 | return (yield host.getAttribute('aria-pressed')) === 'true';
|
157 | });
|
158 | }
|
159 |
|
160 | isDisabled() {
|
161 | return __awaiter(this, void 0, void 0, function* () {
|
162 | return this._hasState('disabled');
|
163 | });
|
164 | }
|
165 |
|
166 | isActive() {
|
167 | return __awaiter(this, void 0, void 0, function* () {
|
168 | return this._hasState('active');
|
169 | });
|
170 | }
|
171 |
|
172 | isToday() {
|
173 | return __awaiter(this, void 0, void 0, function* () {
|
174 | return (yield this._content()).hasClass('mat-calendar-body-today');
|
175 | });
|
176 | }
|
177 |
|
178 | select() {
|
179 | return __awaiter(this, void 0, void 0, function* () {
|
180 | return (yield this.host()).click();
|
181 | });
|
182 | }
|
183 |
|
184 | hover() {
|
185 | return __awaiter(this, void 0, void 0, function* () {
|
186 | return (yield this.host()).hover();
|
187 | });
|
188 | }
|
189 |
|
190 | mouseAway() {
|
191 | return __awaiter(this, void 0, void 0, function* () {
|
192 | return (yield this.host()).mouseAway();
|
193 | });
|
194 | }
|
195 |
|
196 | focus() {
|
197 | return __awaiter(this, void 0, void 0, function* () {
|
198 | return (yield this.host()).focus();
|
199 | });
|
200 | }
|
201 |
|
202 | blur() {
|
203 | return __awaiter(this, void 0, void 0, function* () {
|
204 | return (yield this.host()).blur();
|
205 | });
|
206 | }
|
207 |
|
208 | isRangeStart() {
|
209 | return __awaiter(this, void 0, void 0, function* () {
|
210 | return this._hasState('range-start');
|
211 | });
|
212 | }
|
213 |
|
214 | isRangeEnd() {
|
215 | return __awaiter(this, void 0, void 0, function* () {
|
216 | return this._hasState('range-end');
|
217 | });
|
218 | }
|
219 |
|
220 | isInRange() {
|
221 | return __awaiter(this, void 0, void 0, function* () {
|
222 | return this._hasState('in-range');
|
223 | });
|
224 | }
|
225 |
|
226 | isComparisonRangeStart() {
|
227 | return __awaiter(this, void 0, void 0, function* () {
|
228 | return this._hasState('comparison-start');
|
229 | });
|
230 | }
|
231 |
|
232 | isComparisonRangeEnd() {
|
233 | return __awaiter(this, void 0, void 0, function* () {
|
234 | return this._hasState('comparison-end');
|
235 | });
|
236 | }
|
237 |
|
238 | isInComparisonRange() {
|
239 | return __awaiter(this, void 0, void 0, function* () {
|
240 | return this._hasState('in-comparison-range');
|
241 | });
|
242 | }
|
243 |
|
244 | isPreviewRangeStart() {
|
245 | return __awaiter(this, void 0, void 0, function* () {
|
246 | return this._hasState('preview-start');
|
247 | });
|
248 | }
|
249 |
|
250 | isPreviewRangeEnd() {
|
251 | return __awaiter(this, void 0, void 0, function* () {
|
252 | return this._hasState('preview-end');
|
253 | });
|
254 | }
|
255 |
|
256 | isInPreviewRange() {
|
257 | return __awaiter(this, void 0, void 0, function* () {
|
258 | return this._hasState('in-preview');
|
259 | });
|
260 | }
|
261 |
|
262 | _hasState(name) {
|
263 | return __awaiter(this, void 0, void 0, function* () {
|
264 | return (yield this.host()).hasClass(`mat-calendar-body-${name}`);
|
265 | });
|
266 | }
|
267 | }
|
268 | MatCalendarCellHarness.hostSelector = '.mat-calendar-body-cell';
|
269 |
|
270 |
|
271 | class MatCalendarHarness extends ComponentHarness {
|
272 | constructor() {
|
273 | super(...arguments);
|
274 |
|
275 | this._periodButton = this.locatorFor('.mat-calendar-period-button');
|
276 | }
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | static with(options = {}) {
|
284 | return new HarnessPredicate(MatCalendarHarness, options);
|
285 | }
|
286 | |
287 |
|
288 |
|
289 |
|
290 | getCells(filter = {}) {
|
291 | return __awaiter(this, void 0, void 0, function* () {
|
292 | return this.locatorForAll(MatCalendarCellHarness.with(filter))();
|
293 | });
|
294 | }
|
295 |
|
296 | getCurrentView() {
|
297 | return __awaiter(this, void 0, void 0, function* () {
|
298 | if (yield this.locatorForOptional('mat-multi-year-view')()) {
|
299 | return 2 ;
|
300 | }
|
301 | if (yield this.locatorForOptional('mat-year-view')()) {
|
302 | return 1 ;
|
303 | }
|
304 | return 0 ;
|
305 | });
|
306 | }
|
307 |
|
308 | getCurrentViewLabel() {
|
309 | return __awaiter(this, void 0, void 0, function* () {
|
310 | return (yield this._periodButton()).text();
|
311 | });
|
312 | }
|
313 |
|
314 | changeView() {
|
315 | return __awaiter(this, void 0, void 0, function* () {
|
316 | return (yield this._periodButton()).click();
|
317 | });
|
318 | }
|
319 |
|
320 | next() {
|
321 | return __awaiter(this, void 0, void 0, function* () {
|
322 | return (yield this.locatorFor('.mat-calendar-next-button')()).click();
|
323 | });
|
324 | }
|
325 | |
326 |
|
327 |
|
328 |
|
329 | previous() {
|
330 | return __awaiter(this, void 0, void 0, function* () {
|
331 | return (yield this.locatorFor('.mat-calendar-previous-button')()).click();
|
332 | });
|
333 | }
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 | selectCell(filter = {}) {
|
340 | return __awaiter(this, void 0, void 0, function* () {
|
341 | const cells = yield this.getCells(filter);
|
342 | if (!cells.length) {
|
343 | throw Error(`Cannot find calendar cell matching filter ${JSON.stringify(filter)}`);
|
344 | }
|
345 | yield cells[0].select();
|
346 | });
|
347 | }
|
348 | }
|
349 | MatCalendarHarness.hostSelector = '.mat-calendar';
|
350 |
|
351 |
|
352 | class DatepickerTriggerHarnessBase extends ComponentHarness {
|
353 |
|
354 | openCalendar() {
|
355 | return __awaiter(this, void 0, void 0, function* () {
|
356 | const [isDisabled, hasCalendar] = yield parallel(() => [this.isDisabled(), this.hasCalendar()]);
|
357 | if (!isDisabled && hasCalendar) {
|
358 | return this._openCalendar();
|
359 | }
|
360 | });
|
361 | }
|
362 |
|
363 | closeCalendar() {
|
364 | return __awaiter(this, void 0, void 0, function* () {
|
365 | if (yield this.isCalendarOpen()) {
|
366 | yield closeCalendar(getCalendarId(this.host()), this.documentRootLocatorFactory());
|
367 |
|
368 | yield this.forceStabilize();
|
369 | }
|
370 | });
|
371 | }
|
372 |
|
373 | hasCalendar() {
|
374 | return __awaiter(this, void 0, void 0, function* () {
|
375 | return (yield getCalendarId(this.host())) != null;
|
376 | });
|
377 | }
|
378 | |
379 |
|
380 |
|
381 |
|
382 | getCalendar(filter = {}) {
|
383 | return __awaiter(this, void 0, void 0, function* () {
|
384 | return getCalendar(filter, this.host(), this.documentRootLocatorFactory());
|
385 | });
|
386 | }
|
387 | }
|
388 |
|
389 | function getCalendarId(host) {
|
390 | return __awaiter(this, void 0, void 0, function* () {
|
391 | return (yield host).getAttribute('data-mat-calendar');
|
392 | });
|
393 | }
|
394 |
|
395 | function closeCalendar(calendarId, documentLocator) {
|
396 | return __awaiter(this, void 0, void 0, function* () {
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 | const backdropSelector = `.${yield calendarId}-backdrop`;
|
404 | return (yield documentLocator.locatorFor(backdropSelector)()).click();
|
405 | });
|
406 | }
|
407 |
|
408 | function getCalendar(filter, host, documentLocator) {
|
409 | return __awaiter(this, void 0, void 0, function* () {
|
410 | const calendarId = yield getCalendarId(host);
|
411 | if (!calendarId) {
|
412 | throw Error(`Element is not associated with a calendar`);
|
413 | }
|
414 | return documentLocator.locatorFor(MatCalendarHarness.with(Object.assign(Object.assign({}, filter), { selector: `#${calendarId}` })))();
|
415 | });
|
416 | }
|
417 |
|
418 |
|
419 | class MatDatepickerInputHarness extends MatDatepickerInputHarnessBase {
|
420 | |
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | static with(options = {}) {
|
427 | return getInputPredicate(MatDatepickerInputHarness, options);
|
428 | }
|
429 |
|
430 | isCalendarOpen() {
|
431 | return __awaiter(this, void 0, void 0, function* () {
|
432 |
|
433 | const host = yield this.host();
|
434 | return (yield host.getAttribute('aria-owns')) != null;
|
435 | });
|
436 | }
|
437 |
|
438 | openCalendar() {
|
439 | return __awaiter(this, void 0, void 0, function* () {
|
440 | const [isDisabled, hasCalendar] = yield parallel(() => [this.isDisabled(), this.hasCalendar()]);
|
441 | if (!isDisabled && hasCalendar) {
|
442 |
|
443 | const host = yield this.host();
|
444 | return host.sendKeys({ alt: true }, TestKey.DOWN_ARROW);
|
445 | }
|
446 | });
|
447 | }
|
448 |
|
449 | closeCalendar() {
|
450 | return __awaiter(this, void 0, void 0, function* () {
|
451 | if (yield this.isCalendarOpen()) {
|
452 | yield closeCalendar(getCalendarId(this.host()), this.documentRootLocatorFactory());
|
453 |
|
454 | yield this.forceStabilize();
|
455 | }
|
456 | });
|
457 | }
|
458 |
|
459 | hasCalendar() {
|
460 | return __awaiter(this, void 0, void 0, function* () {
|
461 | return (yield getCalendarId(this.host())) != null;
|
462 | });
|
463 | }
|
464 | |
465 |
|
466 |
|
467 |
|
468 | getCalendar(filter = {}) {
|
469 | return __awaiter(this, void 0, void 0, function* () {
|
470 | return getCalendar(filter, this.host(), this.documentRootLocatorFactory());
|
471 | });
|
472 | }
|
473 | }
|
474 | MatDatepickerInputHarness.hostSelector = '.mat-datepicker-input';
|
475 |
|
476 |
|
477 | class MatDatepickerToggleHarness extends DatepickerTriggerHarnessBase {
|
478 | constructor() {
|
479 | super(...arguments);
|
480 |
|
481 | this._button = this.locatorFor('button');
|
482 | }
|
483 | |
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | static with(options = {}) {
|
490 | return new HarnessPredicate(MatDatepickerToggleHarness, options);
|
491 | }
|
492 |
|
493 | isCalendarOpen() {
|
494 | return __awaiter(this, void 0, void 0, function* () {
|
495 | return (yield this.host()).hasClass('mat-datepicker-toggle-active');
|
496 | });
|
497 | }
|
498 |
|
499 | isDisabled() {
|
500 | return __awaiter(this, void 0, void 0, function* () {
|
501 | const button = yield this._button();
|
502 | return coerceBooleanProperty(yield button.getAttribute('disabled'));
|
503 | });
|
504 | }
|
505 | _openCalendar() {
|
506 | return __awaiter(this, void 0, void 0, function* () {
|
507 | return (yield this._button()).click();
|
508 | });
|
509 | }
|
510 | }
|
511 | MatDatepickerToggleHarness.hostSelector = '.mat-datepicker-toggle';
|
512 |
|
513 |
|
514 | class MatStartDateHarness extends MatDatepickerInputHarnessBase {
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | static with(options = {}) {
|
522 | return getInputPredicate(MatStartDateHarness, options);
|
523 | }
|
524 | }
|
525 | MatStartDateHarness.hostSelector = '.mat-start-date';
|
526 |
|
527 | class MatEndDateHarness extends MatDatepickerInputHarnessBase {
|
528 | |
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | static with(options = {}) {
|
535 | return getInputPredicate(MatEndDateHarness, options);
|
536 | }
|
537 | }
|
538 | MatEndDateHarness.hostSelector = '.mat-end-date';
|
539 |
|
540 | class MatDateRangeInputHarness extends DatepickerTriggerHarnessBase {
|
541 | |
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 | static with(options = {}) {
|
548 | return new HarnessPredicate(MatDateRangeInputHarness, options).addOption('value', options.value, (harness, value) => HarnessPredicate.stringMatches(harness.getValue(), value));
|
549 | }
|
550 |
|
551 | getValue() {
|
552 | return __awaiter(this, void 0, void 0, function* () {
|
553 | const [start, end, separator] = yield parallel(() => [
|
554 | this.getStartInput().then(input => input.getValue()),
|
555 | this.getEndInput().then(input => input.getValue()),
|
556 | this.getSeparator(),
|
557 | ]);
|
558 | return start + `${end ? ` ${separator} ${end}` : ''}`;
|
559 | });
|
560 | }
|
561 |
|
562 | getStartInput() {
|
563 | return __awaiter(this, void 0, void 0, function* () {
|
564 |
|
565 | return this.locatorFor(MatStartDateHarness)();
|
566 | });
|
567 | }
|
568 |
|
569 | getEndInput() {
|
570 | return __awaiter(this, void 0, void 0, function* () {
|
571 |
|
572 | return this.locatorFor(MatEndDateHarness)();
|
573 | });
|
574 | }
|
575 |
|
576 | getSeparator() {
|
577 | return __awaiter(this, void 0, void 0, function* () {
|
578 | return (yield this.locatorFor('.mat-date-range-input-separator')()).text();
|
579 | });
|
580 | }
|
581 |
|
582 | isDisabled() {
|
583 | return __awaiter(this, void 0, void 0, function* () {
|
584 |
|
585 | const [startDisabled, endDisabled] = yield parallel(() => [
|
586 | this.getStartInput().then(input => input.isDisabled()),
|
587 | this.getEndInput().then(input => input.isDisabled()),
|
588 | ]);
|
589 | return startDisabled && endDisabled;
|
590 | });
|
591 | }
|
592 |
|
593 | isRequired() {
|
594 | return __awaiter(this, void 0, void 0, function* () {
|
595 | return (yield this.host()).hasClass('mat-date-range-input-required');
|
596 | });
|
597 | }
|
598 |
|
599 | isCalendarOpen() {
|
600 | return __awaiter(this, void 0, void 0, function* () {
|
601 |
|
602 |
|
603 | const startHost = yield (yield this.getStartInput()).host();
|
604 | return (yield startHost.getAttribute('aria-owns')) != null;
|
605 | });
|
606 | }
|
607 | _openCalendar() {
|
608 | return __awaiter(this, void 0, void 0, function* () {
|
609 |
|
610 | const startHost = yield (yield this.getStartInput()).host();
|
611 | return startHost.sendKeys({ alt: true }, TestKey.DOWN_ARROW);
|
612 | });
|
613 | }
|
614 | }
|
615 | MatDateRangeInputHarness.hostSelector = '.mat-date-range-input';
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 | export { MatCalendarCellHarness, MatCalendarHarness, MatDateRangeInputHarness, MatDatepickerInputHarness, MatDatepickerToggleHarness, MatEndDateHarness, MatStartDateHarness };
|
634 |
|