-
Notifications
You must be signed in to change notification settings - Fork 25.7k
/
Copy pathtestability.ts
67 lines (58 loc) · 2.19 KB
/
testability.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @license
* Copyright Google Inc. 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 {GetTestability, Testability, TestabilityRegistry, setTestabilityGetter, ɵglobal as global} from '@angular/core';
import {getDOM} from '../dom/dom_adapter';
export class BrowserGetTestability implements GetTestability {
static init() { setTestabilityGetter(new BrowserGetTestability()); }
addToWindow(registry: TestabilityRegistry): void {
global['getAngularTestability'] = (elem: any, findInAncestors: boolean = true) => {
const testability = registry.findTestabilityInTree(elem, findInAncestors);
if (testability == null) {
throw new Error('Could not find testability for element.');
}
return testability;
};
global['getAllAngularTestabilities'] = () => registry.getAllTestabilities();
global['getAllAngularRootElements'] = () => registry.getAllRootElements();
const whenAllStable = (callback: any /** TODO #9100 */) => {
const testabilities = global['getAllAngularTestabilities']();
let count = testabilities.length;
let didWork = false;
const decrement = function(didWork_: any /** TODO #9100 */) {
didWork = didWork || didWork_;
count--;
if (count == 0) {
callback(didWork);
}
};
testabilities.forEach(function(testability: any /** TODO #9100 */) {
testability.whenStable(decrement);
});
};
if (!global['frameworkStabilizers']) {
global['frameworkStabilizers'] = [];
}
global['frameworkStabilizers'].push(whenAllStable);
}
findTestabilityInTree(registry: TestabilityRegistry, elem: any, findInAncestors: boolean):
Testability|null {
if (elem == null) {
return null;
}
const t = registry.getTestability(elem);
if (t != null) {
return t;
} else if (!findInAncestors) {
return null;
}
if (getDOM().isShadowRoot(elem)) {
return this.findTestabilityInTree(registry, getDOM().getHost(elem), true);
}
return this.findTestabilityInTree(registry, getDOM().parentElement(elem), true);
}
}