|
| 1 | +import {Injector} from 'angular2/di'; |
| 2 | + |
| 3 | +import {Type, isPresent, BaseException} from 'angular2/src/facade/lang'; |
| 4 | +import {Promise} from 'angular2/src/facade/async'; |
| 5 | +import {isBlank} from 'angular2/src/facade/lang'; |
| 6 | +import {List} from 'angular2/src/facade/collection'; |
| 7 | + |
| 8 | +import {Template} from 'angular2/src/core/annotations/template'; |
| 9 | + |
| 10 | +import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver'; |
| 11 | +import {Compiler} from 'angular2/src/core/compiler/compiler'; |
| 12 | +import {View} from 'angular2/src/core/compiler/view'; |
| 13 | + |
| 14 | +import {EventManager} from 'angular2/src/render/dom/events/event_manager'; |
| 15 | + |
| 16 | +import {queryView} from './utils'; |
| 17 | +import {instantiateType, getTypeOf} from './lang_utils'; |
| 18 | + |
| 19 | +export class TestBed { |
| 20 | + _injector: Injector; |
| 21 | + |
| 22 | + constructor(injector: Injector) { |
| 23 | + this._injector = injector; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Overrides the [Template] of a [Component]. |
| 28 | + * |
| 29 | + * @see setInlineTemplate() to only override the html |
| 30 | + * |
| 31 | + * @param {Type} component |
| 32 | + * @param {Template} template |
| 33 | + */ |
| 34 | + overrideTemplate(component: Type, template: Template): void { |
| 35 | + this._injector.get(TemplateResolver).setTemplate(component, template); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Overrides only the html of a [Component]. |
| 40 | + * All the other propoerties of the component's [Template] are preserved. |
| 41 | + * |
| 42 | + * @param {Type} component |
| 43 | + * @param {string} html |
| 44 | + */ |
| 45 | + setInlineTemplate(component: Type, html: string): void { |
| 46 | + this._injector.get(TemplateResolver).setInlineTemplate(component, html); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Overrides the directives from the component [Template]. |
| 51 | + * |
| 52 | + * @param {Type} component |
| 53 | + * @param {Type} from |
| 54 | + * @param {Type} to |
| 55 | + */ |
| 56 | + overrideDirective(component: Type, from: Type, to: Type): void { |
| 57 | + this._injector.get(TemplateResolver).overrideTemplateDirective(component, from, to); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a [View] for the given component. |
| 62 | + * |
| 63 | + * Only either a component or a context needs to be specified but both can be provided for |
| 64 | + * advanced use cases (ie subclassing the context). |
| 65 | + * |
| 66 | + * @param {Type} component |
| 67 | + * @param {*} context |
| 68 | + * @param {string} html Use as the component template when specified (shortcut for setInlineTemplate) |
| 69 | + * @return {Promise<ViewProxy>} |
| 70 | + */ |
| 71 | + createView(component: Type, |
| 72 | + {context = null, html = null}: {context:any, html: string} = {}): Promise<View> { |
| 73 | + |
| 74 | + if (isBlank(component) && isBlank(context)) { |
| 75 | + throw new BaseException('You must specified at least a component or a context'); |
| 76 | + } |
| 77 | + |
| 78 | + if (isBlank(component)) { |
| 79 | + component = getTypeOf(context); |
| 80 | + } else if (isBlank(context)) { |
| 81 | + context = instantiateType(component); |
| 82 | + } |
| 83 | + |
| 84 | + if (isPresent(html)) { |
| 85 | + this.setInlineTemplate(component, html); |
| 86 | + } |
| 87 | + |
| 88 | + return this._injector.get(Compiler).compile(component).then((pv) => { |
| 89 | + var eventManager = this._injector.get(EventManager); |
| 90 | + var view = pv.instantiate(null, eventManager); |
| 91 | + view.hydrate(this._injector, null, null, context, null); |
| 92 | + return new ViewProxy(view); |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Proxy to [View] return by [TestBed.createView] which offers a high level API for tests. |
| 99 | + */ |
| 100 | +export class ViewProxy { |
| 101 | + _view: View; |
| 102 | + |
| 103 | + constructor(view: View) { |
| 104 | + this._view = view; |
| 105 | + } |
| 106 | + |
| 107 | + get context(): any { |
| 108 | + return this._view.context; |
| 109 | + } |
| 110 | + |
| 111 | + get nodes(): List { |
| 112 | + return this._view.nodes; |
| 113 | + } |
| 114 | + |
| 115 | + detectChanges(): void { |
| 116 | + this._view.changeDetector.detectChanges(); |
| 117 | + } |
| 118 | + |
| 119 | + querySelector(selector) { |
| 120 | + return queryView(this._view, selector); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @returns {View} return the underlying [View]. |
| 125 | + * |
| 126 | + * Prefer using the other methods which hide implementation details. |
| 127 | + */ |
| 128 | + get rawView(): View { |
| 129 | + return this._view; |
| 130 | + } |
| 131 | +} |
0 commit comments