Skip to content

Commit 6534c03

Browse files
committed
fix(core): Remove deprecated Testability methods (angular#53768)
This commit removes the long-deprecated Testability methods that track pending tasks. This is done by NgZone today and will be done by other APIs in zoneless. BREAKING CHANGE: Testability methods `increasePendingRequestCount`, `decreasePendingRequestCount` and `getPendingRequestCount` have been removed. This information is tracked with zones. PR Close angular#53768
1 parent fdd560e commit 6534c03

File tree

5 files changed

+36
-137
lines changed

5 files changed

+36
-137
lines changed

goldens/public-api/core/index.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,13 +1596,7 @@ export abstract class TemplateRef<C> {
15961596
// @public
15971597
export class Testability implements PublicTestability {
15981598
constructor(_ngZone: NgZone, registry: TestabilityRegistry, testabilityGetter: GetTestability);
1599-
// @deprecated
1600-
decreasePendingRequestCount(): number;
16011599
findProviders(using: any, provider: string, exactMatch: boolean): any[];
1602-
// @deprecated
1603-
getPendingRequestCount(): number;
1604-
// @deprecated
1605-
increasePendingRequestCount(): number;
16061600
isStable(): boolean;
16071601
whenStable(doneCb: Function, timeout?: number, updateCb?: Function): void;
16081602
// (undocumented)

packages/core/src/testability/testability.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export const TESTABILITY_GETTER = new InjectionToken<GetTestability>('');
8181
*/
8282
@Injectable()
8383
export class Testability implements PublicTestability {
84-
private _pendingCount: number = 0;
8584
private _isZoneStable: boolean = true;
8685
private _callbacks: WaitCallback[] = [];
8786

@@ -123,33 +122,11 @@ export class Testability implements PublicTestability {
123122
});
124123
}
125124

126-
/**
127-
* Increases the number of pending request
128-
* @deprecated pending requests are now tracked with zones.
129-
*/
130-
increasePendingRequestCount(): number {
131-
this._pendingCount += 1;
132-
return this._pendingCount;
133-
}
134-
135-
/**
136-
* Decreases the number of pending request
137-
* @deprecated pending requests are now tracked with zones
138-
*/
139-
decreasePendingRequestCount(): number {
140-
this._pendingCount -= 1;
141-
if (this._pendingCount < 0) {
142-
throw new Error('pending async requests below zero');
143-
}
144-
this._runCallbacksIfReady();
145-
return this._pendingCount;
146-
}
147-
148125
/**
149126
* Whether an associated application is stable
150127
*/
151128
isStable(): boolean {
152-
return this._isZoneStable && this._pendingCount === 0 && !this._ngZone.hasPendingMacrotasks;
129+
return this._isZoneStable && !this._ngZone.hasPendingMacrotasks;
153130
}
154131

155132
private _runCallbacksIfReady(): void {
@@ -226,13 +203,6 @@ export class Testability implements PublicTestability {
226203
this._runCallbacksIfReady();
227204
}
228205

229-
/**
230-
* Get the number of pending requests
231-
* @deprecated pending requests are now tracked with zones
232-
*/
233-
getPendingRequestCount(): number {
234-
return this._pendingCount;
235-
}
236206
/**
237207
* Registers an application with a testability hook so that it can be tracked.
238208
* @param token token of application, root element

packages/core/test/testability/testability_spec.ts

Lines changed: 20 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {EventEmitter} from '@angular/core';
1010
import {Injectable} from '@angular/core/src/di';
11-
import {GetTestability, PendingMacrotask, Testability, TestabilityRegistry} from '@angular/core/src/testability/testability';
11+
import {GetTestability, PendingMacrotask, Testability, TestabilityRegistry,} from '@angular/core/src/testability/testability';
1212
import {NgZone} from '@angular/core/src/zone/ng_zone';
1313
import {fakeAsync, tick, waitForAsync} from '@angular/core/testing';
1414

@@ -25,8 +25,11 @@ function microTask(fn: Function): void {
2525

2626
class NoopGetTestability implements GetTestability {
2727
addToWindow(registry: TestabilityRegistry): void {}
28-
findTestabilityInTree(registry: TestabilityRegistry, elem: any, findInAncestors: boolean):
29-
Testability|null {
28+
findTestabilityInTree(
29+
registry: TestabilityRegistry,
30+
elem: any,
31+
findInAncestors: boolean,
32+
): Testability|null {
3033
return null;
3134
}
3235
}
@@ -77,62 +80,6 @@ describe('Testability', () => {
7780
// (injected into a constructor) across all instances.
7881
setTestabilityGetter(null! as GetTestability);
7982
});
80-
describe('Pending count logic', () => {
81-
it('should start with a pending count of 0', () => {
82-
expect(testability.getPendingRequestCount()).toEqual(0);
83-
});
84-
85-
it('should fire whenstable callbacks if pending count is 0', waitForAsync(() => {
86-
testability.whenStable(execute);
87-
88-
microTask(() => {
89-
expect(execute).toHaveBeenCalled();
90-
});
91-
}));
92-
93-
it('should not fire whenstable callbacks synchronously if pending count is 0', () => {
94-
testability.whenStable(execute);
95-
expect(execute).not.toHaveBeenCalled();
96-
});
97-
98-
it('should not call whenstable callbacks when there are pending counts', waitForAsync(() => {
99-
testability.increasePendingRequestCount();
100-
testability.increasePendingRequestCount();
101-
testability.whenStable(execute);
102-
103-
microTask(() => {
104-
expect(execute).not.toHaveBeenCalled();
105-
testability.decreasePendingRequestCount();
106-
107-
microTask(() => {
108-
expect(execute).not.toHaveBeenCalled();
109-
});
110-
});
111-
}));
112-
113-
it('should fire whenstable callbacks when pending drops to 0', waitForAsync(() => {
114-
testability.increasePendingRequestCount();
115-
testability.whenStable(execute);
116-
117-
microTask(() => {
118-
expect(execute).not.toHaveBeenCalled();
119-
testability.decreasePendingRequestCount();
120-
121-
microTask(() => {
122-
expect(execute).toHaveBeenCalled();
123-
});
124-
});
125-
}));
126-
127-
it('should not fire whenstable callbacks synchronously when pending drops to 0',
128-
waitForAsync(() => {
129-
testability.increasePendingRequestCount();
130-
testability.whenStable(execute);
131-
testability.decreasePendingRequestCount();
132-
133-
expect(execute).not.toHaveBeenCalled();
134-
}));
135-
});
13683

13784
describe('NgZone callback logic', () => {
13885
describe('whenStable with timeout', () => {
@@ -159,7 +106,7 @@ describe('Testability', () => {
159106

160107
it('calls the done callback when angular is stable', fakeAsync(() => {
161108
let timeout1Done = false;
162-
ngZone.run(() => setTimeout(() => timeout1Done = true, 500));
109+
ngZone.run(() => setTimeout(() => (timeout1Done = true), 500));
163110
testability.whenStable(execute, 1000);
164111

165112
tick(600);
@@ -176,16 +123,15 @@ describe('Testability', () => {
176123
expect(execute.calls.count()).toEqual(1);
177124
}));
178125

179-
180126
it('calls update when macro tasks change', fakeAsync(() => {
181127
let timeout1Done = false;
182128
let timeout2Done = false;
183-
ngZone.run(() => setTimeout(() => timeout1Done = true, 500));
129+
ngZone.run(() => setTimeout(() => (timeout1Done = true), 500));
184130
tick();
185131
testability.whenStable(execute, 1000, updateCallback);
186132

187133
tick(100);
188-
ngZone.run(() => setTimeout(() => timeout2Done = true, 300));
134+
ngZone.run(() => setTimeout(() => (timeout2Done = true), 300));
189135
expect(updateCallback.calls.count()).toEqual(1);
190136
tick(600);
191137

@@ -209,7 +155,7 @@ describe('Testability', () => {
209155
testability.whenStable(execute, 1000, execute2);
210156

211157
tick(100);
212-
ngZone.run(() => setTimeout(() => timeoutDone = true, 500));
158+
ngZone.run(() => setTimeout(() => (timeoutDone = true), 500));
213159
ngZone.stable();
214160
expect(execute2).toHaveBeenCalled();
215161

@@ -258,43 +204,33 @@ describe('Testability', () => {
258204
expect(execute).not.toHaveBeenCalled();
259205
});
260206

261-
it('should not fire whenstable callback when event did not finish', fakeAsync(() => {
207+
it('should fire whenstable callback with didWork if event is already finished',
208+
fakeAsync(() => {
262209
ngZone.unstable();
263-
testability.increasePendingRequestCount();
210+
ngZone.stable();
264211
testability.whenStable(execute);
265212

266213
tick();
267-
expect(execute).not.toHaveBeenCalled();
268-
testability.decreasePendingRequestCount();
269-
270-
tick();
271-
expect(execute).not.toHaveBeenCalled();
272-
ngZone.stable();
214+
expect(execute).toHaveBeenCalled();
215+
testability.whenStable(execute2);
273216

274217
tick();
275-
expect(execute).toHaveBeenCalled();
218+
expect(execute2).toHaveBeenCalled();
276219
}));
277220

278-
it('should not fire whenstable callback when there are pending counts', fakeAsync(() => {
221+
it('should fire whenstable callback with didwork when event finishes', fakeAsync(() => {
279222
ngZone.unstable();
280-
testability.increasePendingRequestCount();
281-
testability.increasePendingRequestCount();
282223
testability.whenStable(execute);
283224

284225
tick();
285-
expect(execute).not.toHaveBeenCalled();
286226
ngZone.stable();
287227

288228
tick();
289-
expect(execute).not.toHaveBeenCalled();
290-
testability.decreasePendingRequestCount();
291-
292-
tick();
293-
expect(execute).not.toHaveBeenCalled();
294-
testability.decreasePendingRequestCount();
229+
expect(execute).toHaveBeenCalled();
230+
testability.whenStable(execute2);
295231

296232
tick();
297-
expect(execute).toHaveBeenCalled();
233+
expect(execute2).toHaveBeenCalled();
298234
}));
299235
});
300236
});

packages/upgrade/src/dynamic/test/upgrade_spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,19 +3419,19 @@ withEachNg1Version(() => {
34193419
angular.module_('ng1', []);
34203420
const element = html('<div></div>');
34213421
adapter.bootstrap(element, ['ng1']).ready((ref) => {
3422-
const ng2Testability: Testability = ref.ng2Injector.get(Testability);
3423-
ng2Testability.increasePendingRequestCount();
3422+
const zone = ref.ng2Injector.get(NgZone);
34243423
let ng2Stable = false;
34253424

3425+
zone.run(() => {
3426+
setTimeout(() => {
3427+
ng2Stable = true;
3428+
}, 100);
3429+
});
3430+
34263431
angular.getTestability(element).whenStable(() => {
34273432
expect(ng2Stable).toEqual(true);
34283433
ref.dispose();
34293434
});
3430-
3431-
setTimeout(() => {
3432-
ng2Stable = true;
3433-
ng2Testability.decreasePendingRequestCount();
3434-
}, 100);
34353435
});
34363436
}));
34373437
});

packages/upgrade/static/test/integration/testability_spec.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {destroyPlatform, NgModule, Testability} from '@angular/core';
10-
import {NgZone} from '@angular/core/src/zone/ng_zone';
9+
import {destroyPlatform, NgModule, Testability, NgZone} from '@angular/core';
1110
import {fakeAsync, flush, tick} from '@angular/core/testing';
1211
import {BrowserModule} from '@angular/platform-browser';
1312
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
@@ -78,20 +77,20 @@ withEachNg1Version(() => {
7877
const element = html('<div></div>');
7978

8079
bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => {
81-
const ng2Testability: Testability = upgrade.injector.get(Testability);
82-
ng2Testability.increasePendingRequestCount();
80+
const zone = upgrade.injector.get(NgZone);
8381
let ng2Stable = false;
8482
let ng1Stable = false;
8583

84+
zone.run(() => {
85+
setTimeout(() => {
86+
ng2Stable = true;
87+
}, 100);
88+
});
89+
8690
angular.getTestability(element).whenStable(() => {
8791
ng1Stable = true;
8892
});
8993

90-
setTimeout(() => {
91-
ng2Stable = true;
92-
ng2Testability.decreasePendingRequestCount();
93-
}, 100);
94-
9594
expect(ng1Stable).toEqual(false);
9695
expect(ng2Stable).toEqual(false);
9796
tick(100);

0 commit comments

Comments
 (0)