|
| 1 | +import {Type, isPresent, BaseException, isBlank} from 'angular2/src/facade/lang'; |
| 2 | +import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection'; |
| 3 | + |
| 4 | +import {DOM} from 'angular2/src/dom/dom_adapter'; |
| 5 | + |
| 6 | +import {ElementInjector} from 'angular2/src/core/compiler/element_injector'; |
| 7 | +import {AppView} from 'angular2/src/core/compiler/view'; |
| 8 | +import {internalView} from 'angular2/src/core/compiler/view_ref'; |
| 9 | +import {ElementRef} from 'angular2/src/core/compiler/element_ref'; |
| 10 | + |
| 11 | +import {resolveInternalDomView} from 'angular2/src/render/dom/view/view'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @exportedAs angular2/test |
| 15 | + * |
| 16 | + * An DebugElement contains information from the Angular compiler about an |
| 17 | + * element and provides access to the corresponding ElementInjector and |
| 18 | + * underlying dom Element, as well as a way to query for children. |
| 19 | + */ |
| 20 | +export class DebugElement { |
| 21 | + _elementInjector: ElementInjector; |
| 22 | + |
| 23 | + constructor(private _parentView: AppView, private _boundElementIndex: number) { |
| 24 | + this._elementInjector = this._parentView.elementInjectors[this._boundElementIndex]; |
| 25 | + } |
| 26 | + |
| 27 | + static create(elementRef: ElementRef): DebugElement { |
| 28 | + return new DebugElement(internalView(elementRef.parentView), elementRef.boundElementIndex); |
| 29 | + } |
| 30 | + |
| 31 | + get componentInstance(): any { |
| 32 | + if (!isPresent(this._elementInjector)) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + return this._elementInjector.getComponent(); |
| 36 | + } |
| 37 | + |
| 38 | + get dynamicallyCreatedComponentInstance(): any { |
| 39 | + if (!isPresent(this._elementInjector)) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + return this._elementInjector.getDynamicallyLoadedComponent(); |
| 43 | + } |
| 44 | + |
| 45 | + get domElement(): any { |
| 46 | + return resolveInternalDomView(this._parentView.render).boundElements[this._boundElementIndex]; |
| 47 | + } |
| 48 | + |
| 49 | + getDirectiveInstance(directiveIndex: number): any { |
| 50 | + return this._elementInjector.getDirectiveAtIndex(directiveIndex); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get child DebugElements from within the Light DOM. |
| 55 | + * |
| 56 | + * @return {List<DebugElement>} |
| 57 | + */ |
| 58 | + get children(): List<DebugElement> { |
| 59 | + var thisElementBinder = this._parentView.proto.elementBinders[this._boundElementIndex]; |
| 60 | + |
| 61 | + return this._getChildElements(this._parentView, thisElementBinder.index); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the root DebugElement children of a component. Returns an empty |
| 66 | + * list if the current DebugElement is not a component root. |
| 67 | + * |
| 68 | + * @return {List<DebugElement>} |
| 69 | + */ |
| 70 | + get componentViewChildren(): List<DebugElement> { |
| 71 | + var shadowView = this._parentView.componentChildViews[this._boundElementIndex]; |
| 72 | + |
| 73 | + if (!isPresent(shadowView)) { |
| 74 | + // The current element is not a component. |
| 75 | + return ListWrapper.create(); |
| 76 | + } |
| 77 | + |
| 78 | + return this._getChildElements(shadowView, null); |
| 79 | + } |
| 80 | + |
| 81 | + triggerEventHandler(eventName, eventObj): void { |
| 82 | + this._parentView.triggerEventHandlers(eventName, eventObj, this._boundElementIndex); |
| 83 | + } |
| 84 | + |
| 85 | + hasDirective(type: Type): boolean { |
| 86 | + if (!isPresent(this._elementInjector)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + return this._elementInjector.hasDirective(type); |
| 90 | + } |
| 91 | + |
| 92 | + inject(type: Type): any { |
| 93 | + if (!isPresent(this._elementInjector)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + return this._elementInjector.get(type); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return the first descendant TestElememt matching the given predicate |
| 101 | + * and scope. |
| 102 | + * |
| 103 | + * @param {Function: boolean} predicate |
| 104 | + * @param {Scope} scope |
| 105 | + * |
| 106 | + * @return {DebugElement} |
| 107 | + */ |
| 108 | + query(predicate: Function, scope = Scope.all): DebugElement { |
| 109 | + var results = this.queryAll(predicate, scope); |
| 110 | + return results.length > 0 ? results[0] : null; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return descendant TestElememts matching the given predicate |
| 115 | + * and scope. |
| 116 | + * |
| 117 | + * @param {Function: boolean} predicate |
| 118 | + * @param {Scope} scope |
| 119 | + * |
| 120 | + * @return {List<DebugElement>} |
| 121 | + */ |
| 122 | + queryAll(predicate: Function, scope = Scope.all): List<DebugElement> { |
| 123 | + var elementsInScope = scope(this); |
| 124 | + |
| 125 | + return ListWrapper.filter(elementsInScope, predicate); |
| 126 | + } |
| 127 | + |
| 128 | + _getChildElements(view: AppView, parentBoundElementIndex: number): List<DebugElement> { |
| 129 | + var els = ListWrapper.create(); |
| 130 | + var parentElementBinder = null; |
| 131 | + if (isPresent(parentBoundElementIndex)) { |
| 132 | + parentElementBinder = view.proto.elementBinders[parentBoundElementIndex]; |
| 133 | + } |
| 134 | + for (var i = 0; i < view.proto.elementBinders.length; ++i) { |
| 135 | + var binder = view.proto.elementBinders[i]; |
| 136 | + if (binder.parent == parentElementBinder) { |
| 137 | + ListWrapper.push(els, new DebugElement(view, i)); |
| 138 | + |
| 139 | + var views = view.viewContainers[i]; |
| 140 | + if (isPresent(views)) { |
| 141 | + ListWrapper.forEach(views.views, (nextView) => { |
| 142 | + els = ListWrapper.concat(els, this._getChildElements(nextView, null)); |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return els; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +export function inspectElement(elementRef: ElementRef): DebugElement { |
| 152 | + return DebugElement.create(elementRef); |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * @exportedAs angular2/test |
| 157 | + */ |
| 158 | +export class Scope { |
| 159 | + static all(debugElement): List<DebugElement> { |
| 160 | + var scope = ListWrapper.create(); |
| 161 | + ListWrapper.push(scope, debugElement); |
| 162 | + |
| 163 | + ListWrapper.forEach(debugElement.children, |
| 164 | + (child) => { scope = ListWrapper.concat(scope, Scope.all(child)); }); |
| 165 | + |
| 166 | + ListWrapper.forEach(debugElement.componentViewChildren, |
| 167 | + (child) => { scope = ListWrapper.concat(scope, Scope.all(child)); }); |
| 168 | + |
| 169 | + return scope; |
| 170 | + } |
| 171 | + static light(debugElement): List<DebugElement> { |
| 172 | + var scope = ListWrapper.create(); |
| 173 | + ListWrapper.forEach(debugElement.children, (child) => { |
| 174 | + ListWrapper.push(scope, child); |
| 175 | + scope = ListWrapper.concat(scope, Scope.light(child)); |
| 176 | + }); |
| 177 | + return scope; |
| 178 | + } |
| 179 | + |
| 180 | + static view(debugElement): List<DebugElement> { |
| 181 | + var scope = ListWrapper.create(); |
| 182 | + |
| 183 | + ListWrapper.forEach(debugElement.componentViewChildren, (child) => { |
| 184 | + ListWrapper.push(scope, child); |
| 185 | + scope = ListWrapper.concat(scope, Scope.light(child)); |
| 186 | + }); |
| 187 | + return scope; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * @exportedAs angular2/test |
| 193 | + */ |
| 194 | +export class By { |
| 195 | + static all(): Function { return (debugElement) => true; } |
| 196 | + |
| 197 | + static css(selector: string): Function { |
| 198 | + return (debugElement) => { return DOM.elementMatches(debugElement.domElement, selector); }; |
| 199 | + } |
| 200 | + static directive(type: Type): Function { |
| 201 | + return (debugElement) => { return debugElement.hasDirective(type); }; |
| 202 | + } |
| 203 | +} |
0 commit comments