/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { handleAutoChangeDetectionStatus, HarnessEnvironment, stopHandlingAutoChangeDetectionStatus, } from '@angular/cdk/testing'; import { flush } from '@angular/core/testing'; import { takeWhile } from 'rxjs/operators'; import { TaskStateZoneInterceptor } from './task-state-zone-interceptor'; import { UnitTestElement } from './unit-test-element'; /** The default environment options. */ const defaultEnvironmentOptions = { queryFn: (selector, root) => root.querySelectorAll(selector), }; /** Whether auto change detection is currently disabled. */ let disableAutoChangeDetection = false; /** * The set of non-destroyed fixtures currently being used by `TestbedHarnessEnvironment` instances. */ const activeFixtures = new Set(); /** * Installs a handler for change detection batching status changes for a specific fixture. * @param fixture The fixture to handle change detection batching for. */ function installAutoChangeDetectionStatusHandler(fixture) { if (!activeFixtures.size) { handleAutoChangeDetectionStatus(({ isDisabled, onDetectChangesNow }) => { disableAutoChangeDetection = isDisabled; if (onDetectChangesNow) { Promise.all(Array.from(activeFixtures).map(detectChanges)).then(onDetectChangesNow); } }); } activeFixtures.add(fixture); } /** * Uninstalls a handler for change detection batching status changes for a specific fixture. * @param fixture The fixture to stop handling change detection batching for. */ function uninstallAutoChangeDetectionStatusHandler(fixture) { activeFixtures.delete(fixture); if (!activeFixtures.size) { stopHandlingAutoChangeDetectionStatus(); } } /** Whether we are currently in the fake async zone. */ function isInFakeAsyncZone() { return Zone.current.get('FakeAsyncTestZoneSpec') != null; } /** * Triggers change detection for a specific fixture. * @param fixture The fixture to trigger change detection for. */ async function detectChanges(fixture) { fixture.detectChanges(); if (isInFakeAsyncZone()) { flush(); } else { await fixture.whenStable(); } } /** A `HarnessEnvironment` implementation for Angular's Testbed. */ export class TestbedHarnessEnvironment extends HarnessEnvironment { constructor(rawRootElement, _fixture, options) { super(rawRootElement); this._fixture = _fixture; /** Whether the environment has been destroyed. */ this._destroyed = false; this._options = { ...defaultEnvironmentOptions, ...options }; this._taskState = TaskStateZoneInterceptor.setup(); this._stabilizeCallback = () => this.forceStabilize(); installAutoChangeDetectionStatusHandler(_fixture); _fixture.componentRef.onDestroy(() => { uninstallAutoChangeDetectionStatusHandler(_fixture); this._destroyed = true; }); } /** Creates a `HarnessLoader` rooted at the given fixture's root element. */ static loader(fixture, options) { return new TestbedHarnessEnvironment(fixture.nativeElement, fixture, options); } /** * Creates a `HarnessLoader` at the document root. This can be used if harnesses are * located outside of a fixture (e.g. overlays appended to the document body). */ static documentRootLoader(fixture, options) { return new TestbedHarnessEnvironment(document.body, fixture, options); } /** Gets the native DOM element corresponding to the given TestElement. */ static getNativeElement(el) { if (el instanceof UnitTestElement) { return el.element; } throw Error('This TestElement was not created by the TestbedHarnessEnvironment'); } /** * Creates an instance of the given harness type, using the fixture's root element as the * harness's host element. This method should be used when creating a harness for the root element * of a fixture, as components do not have the correct selector when they are created as the root * of the fixture. */ static async harnessForFixture(fixture, harnessType, options) { const environment = new TestbedHarnessEnvironment(fixture.nativeElement, fixture, options); await environment.forceStabilize(); return environment.createComponentHarness(harnessType, fixture.nativeElement); } /** * Flushes change detection and async tasks captured in the Angular zone. * In most cases it should not be necessary to call this manually. However, there may be some edge * cases where it is needed to fully flush animation events. */ async forceStabilize() { if (!disableAutoChangeDetection) { if (this._destroyed) { throw Error('Harness is attempting to use a fixture that has already been destroyed.'); } await detectChanges(this._fixture); } } /** * Waits for all scheduled or running async tasks to complete. This allows harness * authors to wait for async tasks outside of the Angular zone. */ async waitForTasksOutsideAngular() { // If we run in the fake async zone, we run "flush" to run any scheduled tasks. This // ensures that the harnesses behave inside of the FakeAsyncTestZone similar to the // "AsyncTestZone" and the root zone (i.e. neither fakeAsync or async). Note that we // cannot just rely on the task state observable to become stable because the state will // never change. This is because the task queue will be only drained if the fake async // zone is being flushed. if (isInFakeAsyncZone()) { flush(); } // Wait until the task queue has been drained and the zone is stable. Note that // we cannot rely on "fixture.whenStable" since it does not catch tasks scheduled // outside of the Angular zone. For test harnesses, we want to ensure that the // app is fully stabilized and therefore need to use our own zone interceptor. await this._taskState.pipe(takeWhile(state => !state.stable)).toPromise(); } /** Gets the root element for the document. */ getDocumentRoot() { return document.body; } /** Creates a `TestElement` from a raw element. */ createTestElement(element) { return new UnitTestElement(element, this._stabilizeCallback); } /** Creates a `HarnessLoader` rooted at the given raw element. */ createEnvironment(element) { return new TestbedHarnessEnvironment(element, this._fixture, this._options); } /** * Gets a list of all elements matching the given selector under this environment's root element. */ async getAllRawElements(selector) { await this.forceStabilize(); return Array.from(this._options.queryFn(selector, this.rawRootElement)); } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGJlZC1oYXJuZXNzLWVudmlyb25tZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay90ZXN0aW5nL3Rlc3RiZWQvdGVzdGJlZC1oYXJuZXNzLWVudmlyb25tZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVILE9BQU8sRUFHTCwrQkFBK0IsRUFDL0Isa0JBQWtCLEVBRWxCLHFDQUFxQyxHQUV0QyxNQUFNLHNCQUFzQixDQUFDO0FBQzlCLE9BQU8sRUFBbUIsS0FBSyxFQUFDLE1BQU0sdUJBQXVCLENBQUM7QUFFOUQsT0FBTyxFQUFDLFNBQVMsRUFBQyxNQUFNLGdCQUFnQixDQUFDO0FBQ3pDLE9BQU8sRUFBWSx3QkFBd0IsRUFBQyxNQUFNLCtCQUErQixDQUFDO0FBQ2xGLE9BQU8sRUFBQyxlQUFlLEVBQUMsTUFBTSxxQkFBcUIsQ0FBQztBQVFwRCx1Q0FBdUM7QUFDdkMsTUFBTSx5QkFBeUIsR0FBcUM7SUFDbEUsT0FBTyxFQUFFLENBQUMsUUFBZ0IsRUFBRSxJQUFhLEVBQUUsRUFBRSxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsQ0FB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