| 92 | */ |
| 93 | @Injectable() |
| 94 | export class Testability implements PublicTestability { |
| 95 | private _isZoneStable: boolean = true; |
| 96 | private _callbacks: WaitCallback[] = []; |
| 97 | |
| 98 | private _taskTrackingZone: {macroTasks: Task[]} | null = null; |
| 99 | |
| 100 | private _destroyRef?: DestroyRef; |
| 101 | |
| 102 | private readonly pendingTasksInternal = inject(PendingTasksInternal); |
| 103 | private readonly _usePendingTasks = inject(USE_PENDING_TASKS); |
| 104 | constructor( |
| 105 | private _ngZone: NgZone, |
| 106 | private registry: TestabilityRegistry, |
| 107 | @Inject(TESTABILITY_GETTER) testabilityGetter: GetTestability, |
| 108 | ) { |
| 109 | // Attempt to retrieve a `DestroyRef` optionally. |
| 110 | // For backwards compatibility reasons, this cannot be required. |
| 111 | if (isInInjectionContext()) { |
| 112 | this._destroyRef = inject(DestroyRef, {optional: true}) ?? undefined; |
| 113 | } |
| 114 | |
| 115 | // If there was no Testability logic registered in the global scope |
| 116 | // before, register the current testability getter as a global one. |
| 117 | if (!_testabilityGetter) { |
| 118 | setTestabilityGetter(testabilityGetter); |
| 119 | testabilityGetter.addToWindow(registry); |
| 120 | } |
| 121 | this._watchAngularEvents(); |
| 122 | _ngZone.run(() => { |
| 123 | this._taskTrackingZone = |
| 124 | typeof Zone == 'undefined' ? null : Zone.current.get('TaskTrackingZone'); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | private _watchAngularEvents(): void { |
| 129 | const onUnstableSubscription = this._ngZone.onUnstable.subscribe({ |
| 130 | next: () => { |
| 131 | this._isZoneStable = false; |
| 132 | }, |
| 133 | }); |
| 134 | |
| 135 | let pendingTasksSubscription: any; |
| 136 | let onStableSubscription: any; |
| 137 | |
| 138 | this._ngZone.runOutsideAngular(() => { |
| 139 | if (this._usePendingTasks) { |
| 140 | pendingTasksSubscription = this.pendingTasksInternal.hasPendingTasksObservable.subscribe( |
| 141 | () => { |
| 142 | if (this.isStable()) { |
| 143 | this._ngZone.runOutsideAngular(() => { |
| 144 | this._runCallbacksIfReady(); |
| 145 | }); |
| 146 | } |
| 147 | }, |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | onStableSubscription = this._ngZone.onStable.subscribe({ |
nothing calls this directly
no test coverage detected
searching dependent graphs…