1 | import { getNoKeysSpecifiedError, TestKey, _getTextWithExcludedElements, handleAutoChangeDetectionStatus, stopHandlingAutoChangeDetectionStatus, HarnessEnvironment } from '@angular/cdk/testing';
|
2 | import { flush } from '@angular/core/testing';
|
3 | import { takeWhile } from 'rxjs/operators';
|
4 | import { BehaviorSubject } from 'rxjs';
|
5 | import * as keyCodes from '@angular/cdk/keycodes';
|
6 | import { PERIOD } from '@angular/cdk/keycodes';
|
7 |
|
8 |
|
9 | const stateObservableSymbol = Symbol('ProxyZone_PATCHED#stateObservable');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class TaskStateZoneInterceptor {
|
17 | constructor(_lastState) {
|
18 | this._lastState = _lastState;
|
19 |
|
20 | this._stateSubject = new BehaviorSubject(this._lastState ? this._getTaskStateFromInternalZoneState(this._lastState) : { stable: true });
|
21 |
|
22 | this.state = this._stateSubject;
|
23 | }
|
24 |
|
25 | onHasTask(delegate, current, target, hasTaskState) {
|
26 | if (current === target) {
|
27 | this._stateSubject.next(this._getTaskStateFromInternalZoneState(hasTaskState));
|
28 | }
|
29 | }
|
30 |
|
31 | _getTaskStateFromInternalZoneState(state) {
|
32 | return { stable: !state.macroTask && !state.microTask };
|
33 | }
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 | static setup() {
|
40 | if (Zone === undefined) {
|
41 | throw Error('Could not find ZoneJS. For test harnesses running in TestBed, ' +
|
42 | 'ZoneJS needs to be installed.');
|
43 | }
|
44 |
|
45 | const ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
46 |
|
47 |
|
48 | if (!ProxyZoneSpec) {
|
49 | throw Error('ProxyZoneSpec is needed for the test harnesses but could not be found. ' +
|
50 | 'Please make sure that your environment includes zone.js/dist/zone-testing.js');
|
51 | }
|
52 |
|
53 |
|
54 | const zoneSpec = ProxyZoneSpec.assertPresent();
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | if (zoneSpec[stateObservableSymbol]) {
|
60 | return zoneSpec[stateObservableSymbol];
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const interceptor = new TaskStateZoneInterceptor(zoneSpec.lastTaskState);
|
67 | const zoneSpecOnHasTask = zoneSpec.onHasTask.bind(zoneSpec);
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | zoneSpec.onHasTask = function (...args) {
|
75 | zoneSpecOnHasTask(...args);
|
76 | interceptor.onHasTask(...args);
|
77 | };
|
78 | return (zoneSpec[stateObservableSymbol] = interceptor.state);
|
79 | }
|
80 | }
|
81 |
|
82 |
|
83 | let uniqueIds = 0;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function createMouseEvent(type, clientX = 0, clientY = 0, offsetX = 1, offsetY = 1, button = 0, modifiers = {}) {
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | const screenX = clientX;
|
94 | const screenY = clientY;
|
95 | const event = new MouseEvent(type, {
|
96 | bubbles: true,
|
97 | cancelable: true,
|
98 | composed: true,
|
99 | view: window,
|
100 | detail: 0,
|
101 | relatedTarget: null,
|
102 | screenX,
|
103 | screenY,
|
104 | clientX,
|
105 | clientY,
|
106 | ctrlKey: modifiers.control,
|
107 | altKey: modifiers.alt,
|
108 | shiftKey: modifiers.shift,
|
109 | metaKey: modifiers.meta,
|
110 | button: button,
|
111 | buttons: 1,
|
112 | });
|
113 |
|
114 |
|
115 | if (offsetX != null) {
|
116 | defineReadonlyEventProperty(event, 'offsetX', offsetX);
|
117 | }
|
118 | if (offsetY != null) {
|
119 | defineReadonlyEventProperty(event, 'offsetY', offsetY);
|
120 | }
|
121 | return event;
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function createPointerEvent(type, clientX = 0, clientY = 0, offsetX, offsetY, options = { isPrimary: true }) {
|
134 | const event = new PointerEvent(type, {
|
135 | bubbles: true,
|
136 | cancelable: true,
|
137 | composed: true,
|
138 | view: window,
|
139 | clientX,
|
140 | clientY,
|
141 | ...options,
|
142 | });
|
143 | if (offsetX != null) {
|
144 | defineReadonlyEventProperty(event, 'offsetX', offsetX);
|
145 | }
|
146 | if (offsetY != null) {
|
147 | defineReadonlyEventProperty(event, 'offsetY', offsetY);
|
148 | }
|
149 | return event;
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function createTouchEvent(type, pageX = 0, pageY = 0, clientX = 0, clientY = 0) {
|
156 |
|
157 |
|
158 | const event = document.createEvent('UIEvent');
|
159 | const touchDetails = { pageX, pageY, clientX, clientY, identifier: uniqueIds++ };
|
160 |
|
161 | event.initUIEvent(type, true, true, window, 0);
|
162 |
|
163 |
|
164 | defineReadonlyEventProperty(event, 'touches', [touchDetails]);
|
165 | defineReadonlyEventProperty(event, 'targetTouches', [touchDetails]);
|
166 | defineReadonlyEventProperty(event, 'changedTouches', [touchDetails]);
|
167 | return event;
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | function createKeyboardEvent(type, keyCode = 0, key = '', modifiers = {}) {
|
174 | return new KeyboardEvent(type, {
|
175 | bubbles: true,
|
176 | cancelable: true,
|
177 | composed: true,
|
178 | view: window,
|
179 | keyCode: keyCode,
|
180 | key: key,
|
181 | shiftKey: modifiers.shift,
|
182 | metaKey: modifiers.meta,
|
183 | altKey: modifiers.alt,
|
184 | ctrlKey: modifiers.control,
|
185 | });
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | function createFakeEvent(type, bubbles = false, cancelable = true, composed = true) {
|
192 | return new Event(type, { bubbles, cancelable, composed });
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function defineReadonlyEventProperty(event, propertyName, value) {
|
199 | Object.defineProperty(event, propertyName, { get: () => value, configurable: true });
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | function dispatchEvent(node, event) {
|
207 | node.dispatchEvent(event);
|
208 | return event;
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function dispatchFakeEvent(node, type, bubbles) {
|
215 | return dispatchEvent(node, createFakeEvent(type, bubbles));
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | function dispatchKeyboardEvent(node, type, keyCode, key, modifiers) {
|
223 | return dispatchEvent(node, createKeyboardEvent(type, keyCode, key, modifiers));
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | function dispatchMouseEvent(node, type, clientX = 0, clientY = 0, offsetX, offsetY, button, modifiers) {
|
230 | return dispatchEvent(node, createMouseEvent(type, clientX, clientY, offsetX, offsetY, button, modifiers));
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | function dispatchPointerEvent(node, type, clientX = 0, clientY = 0, offsetX, offsetY, options) {
|
237 | return dispatchEvent(node, createPointerEvent(type, clientX, clientY, offsetX, offsetY, options));
|
238 | }
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | function dispatchTouchEvent(node, type, pageX = 0, pageY = 0, clientX = 0, clientY = 0) {
|
244 | return dispatchEvent(node, createTouchEvent(type, pageX, pageY, clientX, clientY));
|
245 | }
|
246 |
|
247 | function triggerFocusChange(element, event) {
|
248 | let eventFired = false;
|
249 | const handler = () => (eventFired = true);
|
250 | element.addEventListener(event, handler);
|
251 | element[event]();
|
252 | element.removeEventListener(event, handler);
|
253 | if (!eventFired) {
|
254 | dispatchFakeEvent(element, event);
|
255 | }
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function patchElementFocus(element) {
|
266 | element.focus = () => dispatchFakeEvent(element, 'focus');
|
267 | element.blur = () => dispatchFakeEvent(element, 'blur');
|
268 | }
|
269 |
|
270 | function triggerFocus(element) {
|
271 | triggerFocusChange(element, 'focus');
|
272 | }
|
273 |
|
274 | function triggerBlur(element) {
|
275 | triggerFocusChange(element, 'blur');
|
276 | }
|
277 |
|
278 |
|
279 | const incrementalInputTypes = new Set([
|
280 | 'text',
|
281 | 'email',
|
282 | 'hidden',
|
283 | 'password',
|
284 | 'search',
|
285 | 'tel',
|
286 | 'url',
|
287 | ]);
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | function isTextInput(element) {
|
293 | const nodeName = element.nodeName.toLowerCase();
|
294 | return nodeName === 'input' || nodeName === 'textarea';
|
295 | }
|
296 | function typeInElement(element, ...modifiersAndKeys) {
|
297 | const first = modifiersAndKeys[0];
|
298 | let modifiers;
|
299 | let rest;
|
300 | if (first !== undefined &&
|
301 | typeof first !== 'string' &&
|
302 | first.keyCode === undefined &&
|
303 | first.key === undefined) {
|
304 | modifiers = first;
|
305 | rest = modifiersAndKeys.slice(1);
|
306 | }
|
307 | else {
|
308 | modifiers = {};
|
309 | rest = modifiersAndKeys;
|
310 | }
|
311 | const isInput = isTextInput(element);
|
312 | const inputType = element.getAttribute('type') || 'text';
|
313 | const keys = rest
|
314 | .map(k => typeof k === 'string'
|
315 | ? k.split('').map(c => ({ keyCode: c.toUpperCase().charCodeAt(0), key: c }))
|
316 | : [k])
|
317 | .reduce((arr, k) => arr.concat(k), []);
|
318 |
|
319 |
|
320 | if (keys.length === 0) {
|
321 | throw getNoKeysSpecifiedError();
|
322 | }
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | const enterValueIncrementally = inputType === 'number'
|
329 | ?
|
330 | keys.every(key => key.key !== '.' && key.key !== '-' && key.keyCode !== PERIOD)
|
331 | : incrementalInputTypes.has(inputType);
|
332 | triggerFocus(element);
|
333 |
|
334 |
|
335 | if (!enterValueIncrementally) {
|
336 | element.value = keys.reduce((value, key) => value + (key.key || ''), '');
|
337 | }
|
338 | for (const key of keys) {
|
339 | dispatchKeyboardEvent(element, 'keydown', key.keyCode, key.key, modifiers);
|
340 | dispatchKeyboardEvent(element, 'keypress', key.keyCode, key.key, modifiers);
|
341 | if (isInput && key.key && key.key.length === 1) {
|
342 | if (enterValueIncrementally) {
|
343 | element.value += key.key;
|
344 | dispatchFakeEvent(element, 'input');
|
345 | }
|
346 | }
|
347 | dispatchKeyboardEvent(element, 'keyup', key.keyCode, key.key, modifiers);
|
348 | }
|
349 |
|
350 | if (!enterValueIncrementally) {
|
351 | dispatchFakeEvent(element, 'input');
|
352 | }
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | function clearElement(element) {
|
359 | triggerFocus(element);
|
360 | element.value = '';
|
361 | dispatchFakeEvent(element, 'input');
|
362 | }
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | const keyMap = {
|
368 | [TestKey.BACKSPACE]: { keyCode: keyCodes.BACKSPACE, key: 'Backspace' },
|
369 | [TestKey.TAB]: { keyCode: keyCodes.TAB, key: 'Tab' },
|
370 | [TestKey.ENTER]: { keyCode: keyCodes.ENTER, key: 'Enter' },
|
371 | [TestKey.SHIFT]: { keyCode: keyCodes.SHIFT, key: 'Shift' },
|
372 | [TestKey.CONTROL]: { keyCode: keyCodes.CONTROL, key: 'Control' },
|
373 | [TestKey.ALT]: { keyCode: keyCodes.ALT, key: 'Alt' },
|
374 | [TestKey.ESCAPE]: { keyCode: keyCodes.ESCAPE, key: 'Escape' },
|
375 | [TestKey.PAGE_UP]: { keyCode: keyCodes.PAGE_UP, key: 'PageUp' },
|
376 | [TestKey.PAGE_DOWN]: { keyCode: keyCodes.PAGE_DOWN, key: 'PageDown' },
|
377 | [TestKey.END]: { keyCode: keyCodes.END, key: 'End' },
|
378 | [TestKey.HOME]: { keyCode: keyCodes.HOME, key: 'Home' },
|
379 | [TestKey.LEFT_ARROW]: { keyCode: keyCodes.LEFT_ARROW, key: 'ArrowLeft' },
|
380 | [TestKey.UP_ARROW]: { keyCode: keyCodes.UP_ARROW, key: 'ArrowUp' },
|
381 | [TestKey.RIGHT_ARROW]: { keyCode: keyCodes.RIGHT_ARROW, key: 'ArrowRight' },
|
382 | [TestKey.DOWN_ARROW]: { keyCode: keyCodes.DOWN_ARROW, key: 'ArrowDown' },
|
383 | [TestKey.INSERT]: { keyCode: keyCodes.INSERT, key: 'Insert' },
|
384 | [TestKey.DELETE]: { keyCode: keyCodes.DELETE, key: 'Delete' },
|
385 | [TestKey.F1]: { keyCode: keyCodes.F1, key: 'F1' },
|
386 | [TestKey.F2]: { keyCode: keyCodes.F2, key: 'F2' },
|
387 | [TestKey.F3]: { keyCode: keyCodes.F3, key: 'F3' },
|
388 | [TestKey.F4]: { keyCode: keyCodes.F4, key: 'F4' },
|
389 | [TestKey.F5]: { keyCode: keyCodes.F5, key: 'F5' },
|
390 | [TestKey.F6]: { keyCode: keyCodes.F6, key: 'F6' },
|
391 | [TestKey.F7]: { keyCode: keyCodes.F7, key: 'F7' },
|
392 | [TestKey.F8]: { keyCode: keyCodes.F8, key: 'F8' },
|
393 | [TestKey.F9]: { keyCode: keyCodes.F9, key: 'F9' },
|
394 | [TestKey.F10]: { keyCode: keyCodes.F10, key: 'F10' },
|
395 | [TestKey.F11]: { keyCode: keyCodes.F11, key: 'F11' },
|
396 | [TestKey.F12]: { keyCode: keyCodes.F12, key: 'F12' },
|
397 | [TestKey.META]: { keyCode: keyCodes.META, key: 'Meta' },
|
398 | };
|
399 |
|
400 | class UnitTestElement {
|
401 | constructor(element, _stabilize) {
|
402 | this.element = element;
|
403 | this._stabilize = _stabilize;
|
404 | }
|
405 |
|
406 | async blur() {
|
407 | triggerBlur(this.element);
|
408 | await this._stabilize();
|
409 | }
|
410 |
|
411 | async clear() {
|
412 | if (!isTextInput(this.element)) {
|
413 | throw Error('Attempting to clear an invalid element');
|
414 | }
|
415 | clearElement(this.element);
|
416 | await this._stabilize();
|
417 | }
|
418 | async click(...args) {
|
419 | const isDisabled = this.element.disabled === true;
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | await this._dispatchMouseEventSequence(isDisabled ? null : 'click', args, 0);
|
427 | await this._stabilize();
|
428 | }
|
429 | async rightClick(...args) {
|
430 | await this._dispatchMouseEventSequence('contextmenu', args, 2);
|
431 | await this._stabilize();
|
432 | }
|
433 |
|
434 | async focus() {
|
435 | triggerFocus(this.element);
|
436 | await this._stabilize();
|
437 | }
|
438 |
|
439 | async getCssValue(property) {
|
440 | await this._stabilize();
|
441 |
|
442 |
|
443 | return getComputedStyle(this.element).getPropertyValue(property);
|
444 | }
|
445 |
|
446 | async hover() {
|
447 | this._dispatchPointerEventIfSupported('pointerenter');
|
448 | dispatchMouseEvent(this.element, 'mouseover');
|
449 | dispatchMouseEvent(this.element, 'mouseenter');
|
450 | await this._stabilize();
|
451 | }
|
452 |
|
453 | async mouseAway() {
|
454 | this._dispatchPointerEventIfSupported('pointerleave');
|
455 | dispatchMouseEvent(this.element, 'mouseout');
|
456 | dispatchMouseEvent(this.element, 'mouseleave');
|
457 | await this._stabilize();
|
458 | }
|
459 | async sendKeys(...modifiersAndKeys) {
|
460 | const args = modifiersAndKeys.map(k => (typeof k === 'number' ? keyMap[k] : k));
|
461 | typeInElement(this.element, ...args);
|
462 | await this._stabilize();
|
463 | }
|
464 | |
465 |
|
466 |
|
467 |
|
468 | async text(options) {
|
469 | await this._stabilize();
|
470 | if (options?.exclude) {
|
471 | return _getTextWithExcludedElements(this.element, options.exclude);
|
472 | }
|
473 | return (this.element.textContent || '').trim();
|
474 | }
|
475 | |
476 |
|
477 |
|
478 |
|
479 | async setContenteditableValue(value) {
|
480 | const contenteditableAttr = await this.getAttribute('contenteditable');
|
481 | if (contenteditableAttr !== '' && contenteditableAttr !== 'true') {
|
482 | throw new Error('setContenteditableValue can only be called on a `contenteditable` element.');
|
483 | }
|
484 | await this._stabilize();
|
485 | this.element.textContent = value;
|
486 | }
|
487 |
|
488 | async getAttribute(name) {
|
489 | await this._stabilize();
|
490 | return this.element.getAttribute(name);
|
491 | }
|
492 |
|
493 | async hasClass(name) {
|
494 | await this._stabilize();
|
495 | return this.element.classList.contains(name);
|
496 | }
|
497 |
|
498 | async getDimensions() {
|
499 | await this._stabilize();
|
500 | return this.element.getBoundingClientRect();
|
501 | }
|
502 |
|
503 | async getProperty(name) {
|
504 | await this._stabilize();
|
505 | return this.element[name];
|
506 | }
|
507 |
|
508 | async setInputValue(value) {
|
509 | this.element.value = value;
|
510 | await this._stabilize();
|
511 | }
|
512 |
|
513 | async selectOptions(...optionIndexes) {
|
514 | let hasChanged = false;
|
515 | const options = this.element.querySelectorAll('option');
|
516 | const indexes = new Set(optionIndexes);
|
517 | for (let i = 0; i < options.length; i++) {
|
518 | const option = options[i];
|
519 | const wasSelected = option.selected;
|
520 |
|
521 |
|
522 | option.selected = indexes.has(i);
|
523 | if (option.selected !== wasSelected) {
|
524 | hasChanged = true;
|
525 | dispatchFakeEvent(this.element, 'change');
|
526 | }
|
527 | }
|
528 | if (hasChanged) {
|
529 | await this._stabilize();
|
530 | }
|
531 | }
|
532 |
|
533 | async matchesSelector(selector) {
|
534 | await this._stabilize();
|
535 | const elementPrototype = Element.prototype;
|
536 | return (elementPrototype['matches'] || elementPrototype['msMatchesSelector']).call(this.element, selector);
|
537 | }
|
538 |
|
539 | async isFocused() {
|
540 | await this._stabilize();
|
541 | return document.activeElement === this.element;
|
542 | }
|
543 | |
544 |
|
545 |
|
546 |
|
547 | async dispatchEvent(name, data) {
|
548 | const event = createFakeEvent(name);
|
549 | if (data) {
|
550 |
|
551 | Object.assign(event, data);
|
552 | }
|
553 | dispatchEvent(this.element, event);
|
554 | await this._stabilize();
|
555 | }
|
556 | |
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | _dispatchPointerEventIfSupported(name, clientX, clientY, offsetX, offsetY, button) {
|
564 |
|
565 |
|
566 |
|
567 |
|
568 | if (typeof PointerEvent !== 'undefined' && PointerEvent) {
|
569 | dispatchPointerEvent(this.element, name, clientX, clientY, offsetX, offsetY, {
|
570 | isPrimary: true,
|
571 | button,
|
572 | });
|
573 | }
|
574 | }
|
575 | |
576 |
|
577 |
|
578 |
|
579 | async _dispatchMouseEventSequence(primaryEventName, args, button) {
|
580 | let clientX = undefined;
|
581 | let clientY = undefined;
|
582 | let offsetX = undefined;
|
583 | let offsetY = undefined;
|
584 | let modifiers = {};
|
585 | if (args.length && typeof args[args.length - 1] === 'object') {
|
586 | modifiers = args.pop();
|
587 | }
|
588 | if (args.length) {
|
589 | const { left, top, width, height } = await this.getDimensions();
|
590 | offsetX = args[0] === 'center' ? width / 2 : args[0];
|
591 | offsetY = args[0] === 'center' ? height / 2 : args[1];
|
592 |
|
593 |
|
594 | clientX = Math.round(left + offsetX);
|
595 | clientY = Math.round(top + offsetY);
|
596 | }
|
597 | this._dispatchPointerEventIfSupported('pointerdown', clientX, clientY, offsetX, offsetY, button);
|
598 | dispatchMouseEvent(this.element, 'mousedown', clientX, clientY, offsetX, offsetY, button, modifiers);
|
599 | this._dispatchPointerEventIfSupported('pointerup', clientX, clientY, offsetX, offsetY, button);
|
600 | dispatchMouseEvent(this.element, 'mouseup', clientX, clientY, offsetX, offsetY, button, modifiers);
|
601 |
|
602 | if (primaryEventName !== null) {
|
603 | dispatchMouseEvent(this.element, primaryEventName, clientX, clientY, offsetX, offsetY, button, modifiers);
|
604 | }
|
605 |
|
606 |
|
607 |
|
608 |
|
609 | await this._stabilize();
|
610 | }
|
611 | }
|
612 |
|
613 |
|
614 | const defaultEnvironmentOptions = {
|
615 | queryFn: (selector, root) => root.querySelectorAll(selector),
|
616 | };
|
617 |
|
618 | let disableAutoChangeDetection = false;
|
619 |
|
620 |
|
621 |
|
622 | const activeFixtures = new Set();
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | function installAutoChangeDetectionStatusHandler(fixture) {
|
628 | if (!activeFixtures.size) {
|
629 | handleAutoChangeDetectionStatus(({ isDisabled, onDetectChangesNow }) => {
|
630 | disableAutoChangeDetection = isDisabled;
|
631 | if (onDetectChangesNow) {
|
632 | Promise.all(Array.from(activeFixtures).map(detectChanges)).then(onDetectChangesNow);
|
633 | }
|
634 | });
|
635 | }
|
636 | activeFixtures.add(fixture);
|
637 | }
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | function uninstallAutoChangeDetectionStatusHandler(fixture) {
|
643 | activeFixtures.delete(fixture);
|
644 | if (!activeFixtures.size) {
|
645 | stopHandlingAutoChangeDetectionStatus();
|
646 | }
|
647 | }
|
648 |
|
649 | function isInFakeAsyncZone() {
|
650 | return Zone.current.get('FakeAsyncTestZoneSpec') != null;
|
651 | }
|
652 |
|
653 |
|
654 |
|
655 |
|
656 | async function detectChanges(fixture) {
|
657 | fixture.detectChanges();
|
658 | if (isInFakeAsyncZone()) {
|
659 | flush();
|
660 | }
|
661 | else {
|
662 | await fixture.whenStable();
|
663 | }
|
664 | }
|
665 |
|
666 | class TestbedHarnessEnvironment extends HarnessEnvironment {
|
667 | constructor(rawRootElement, _fixture, options) {
|
668 | super(rawRootElement);
|
669 | this._fixture = _fixture;
|
670 |
|
671 | this._destroyed = false;
|
672 | this._options = { ...defaultEnvironmentOptions, ...options };
|
673 | this._taskState = TaskStateZoneInterceptor.setup();
|
674 | this._stabilizeCallback = () => this.forceStabilize();
|
675 | installAutoChangeDetectionStatusHandler(_fixture);
|
676 | _fixture.componentRef.onDestroy(() => {
|
677 | uninstallAutoChangeDetectionStatusHandler(_fixture);
|
678 | this._destroyed = true;
|
679 | });
|
680 | }
|
681 |
|
682 | static loader(fixture, options) {
|
683 | return new TestbedHarnessEnvironment(fixture.nativeElement, fixture, options);
|
684 | }
|
685 | |
686 |
|
687 |
|
688 |
|
689 | static documentRootLoader(fixture, options) {
|
690 | return new TestbedHarnessEnvironment(document.body, fixture, options);
|
691 | }
|
692 |
|
693 | static getNativeElement(el) {
|
694 | if (el instanceof UnitTestElement) {
|
695 | return el.element;
|
696 | }
|
697 | throw Error('This TestElement was not created by the TestbedHarnessEnvironment');
|
698 | }
|
699 | |
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 | static async harnessForFixture(fixture, harnessType, options) {
|
706 | const environment = new TestbedHarnessEnvironment(fixture.nativeElement, fixture, options);
|
707 | await environment.forceStabilize();
|
708 | return environment.createComponentHarness(harnessType, fixture.nativeElement);
|
709 | }
|
710 | |
711 |
|
712 |
|
713 |
|
714 |
|
715 | async forceStabilize() {
|
716 | if (!disableAutoChangeDetection) {
|
717 | if (this._destroyed) {
|
718 | throw Error('Harness is attempting to use a fixture that has already been destroyed.');
|
719 | }
|
720 | await detectChanges(this._fixture);
|
721 | }
|
722 | }
|
723 | |
724 |
|
725 |
|
726 |
|
727 | async waitForTasksOutsideAngular() {
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 | if (isInFakeAsyncZone()) {
|
735 | flush();
|
736 | }
|
737 |
|
738 |
|
739 |
|
740 |
|
741 | await this._taskState.pipe(takeWhile(state => !state.stable)).toPromise();
|
742 | }
|
743 |
|
744 | getDocumentRoot() {
|
745 | return document.body;
|
746 | }
|
747 |
|
748 | createTestElement(element) {
|
749 | return new UnitTestElement(element, this._stabilizeCallback);
|
750 | }
|
751 |
|
752 | createEnvironment(element) {
|
753 | return new TestbedHarnessEnvironment(element, this._fixture, this._options);
|
754 | }
|
755 | |
756 |
|
757 |
|
758 | async getAllRawElements(selector) {
|
759 | await this.forceStabilize();
|
760 | return Array.from(this._options.queryFn(selector, this.rawRootElement));
|
761 | }
|
762 | }
|
763 |
|
764 | export { TestbedHarnessEnvironment, UnitTestElement };
|
765 |
|