2015-09-04 01:01:36 -04:00
|
|
|
import {Injector, bind, Injectable} from 'angular2/src/core/di';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-09-10 18:25:36 -04:00
|
|
|
import {Type, isPresent, isBlank} from 'angular2/src/core/facade/lang';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {Promise} from 'angular2/src/core/facade/async';
|
2015-08-28 14:29:19 -04:00
|
|
|
import {ListWrapper, MapWrapper} from 'angular2/src/core/facade/collection';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-08-14 13:03:45 -04:00
|
|
|
import {ViewMetadata} from '../core/metadata';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-10-02 10:37:23 -04:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
|
|
|
|
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
|
|
|
|
import {AppView} from 'angular2/src/core/linker/view';
|
|
|
|
import {internalView, ViewRef} from 'angular2/src/core/linker/view_ref';
|
2015-05-15 19:42:52 -04:00
|
|
|
import {
|
|
|
|
DynamicComponentLoader,
|
|
|
|
ComponentRef
|
2015-10-02 10:37:23 -04:00
|
|
|
} from 'angular2/src/core/linker/dynamic_component_loader';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
import {el} from './utils';
|
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {DOCUMENT} from 'angular2/src/core/render/render';
|
|
|
|
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {DebugElement} from 'angular2/src/core/debug/debug_element';
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-09-11 16:45:31 -04:00
|
|
|
export class RootTestComponent {
|
2015-05-15 19:42:52 -04:00
|
|
|
_componentRef: ComponentRef;
|
|
|
|
_componentParentView: AppView;
|
2015-09-11 16:45:31 -04:00
|
|
|
debugElement: DebugElement;
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-09-11 16:45:31 -04:00
|
|
|
/**
|
2015-10-02 16:32:48 -04:00
|
|
|
* @internal
|
2015-09-11 16:45:31 -04:00
|
|
|
*/
|
2015-05-15 19:42:52 -04:00
|
|
|
constructor(componentRef: ComponentRef) {
|
2015-09-11 16:45:31 -04:00
|
|
|
this.debugElement = new DebugElement(internalView(<ViewRef>componentRef.hostView), 0);
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-07-11 11:26:48 -04:00
|
|
|
this._componentParentView = internalView(<ViewRef>componentRef.hostView);
|
2015-05-15 19:42:52 -04:00
|
|
|
this._componentRef = componentRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
detectChanges(): void {
|
|
|
|
this._componentParentView.changeDetector.detectChanges();
|
|
|
|
this._componentParentView.changeDetector.checkNoChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void { this._componentRef.dispose(); }
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
var _nextRootElementId = 0;
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a RootTestComponent for use in component level tests.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class TestComponentBuilder {
|
2015-09-29 14:11:06 -04:00
|
|
|
_bindingsOverrides = new Map<Type, any[]>();
|
|
|
|
_directiveOverrides = new Map<Type, Map<Type, Type>>();
|
|
|
|
_templateOverrides = new Map<Type, string>();
|
|
|
|
_viewBindingsOverrides = new Map<Type, any[]>();
|
|
|
|
_viewOverrides = new Map<Type, ViewMetadata>();
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private _injector: Injector) {}
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
_clone(): TestComponentBuilder {
|
|
|
|
var clone = new TestComponentBuilder(this._injector);
|
|
|
|
clone._viewOverrides = MapWrapper.clone(this._viewOverrides);
|
|
|
|
clone._directiveOverrides = MapWrapper.clone(this._directiveOverrides);
|
|
|
|
clone._templateOverrides = MapWrapper.clone(this._templateOverrides);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides only the html of a {@link ComponentMetadata}.
|
|
|
|
* All the other properties of the component's {@link ViewMetadata} are preserved.
|
2015-05-15 19:42:52 -04:00
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {string} html
|
|
|
|
*
|
|
|
|
* @return {TestComponentBuilder}
|
|
|
|
*/
|
|
|
|
overrideTemplate(componentType: Type, template: string): TestComponentBuilder {
|
|
|
|
var clone = this._clone();
|
2015-06-17 19:21:40 -04:00
|
|
|
clone._templateOverrides.set(componentType, template);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides a component's {@link ViewMetadata}.
|
2015-05-15 19:42:52 -04:00
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {view} View
|
|
|
|
*
|
|
|
|
* @return {TestComponentBuilder}
|
|
|
|
*/
|
2015-08-14 13:03:45 -04:00
|
|
|
overrideView(componentType: Type, view: ViewMetadata): TestComponentBuilder {
|
2015-05-15 19:42:52 -04:00
|
|
|
var clone = this._clone();
|
2015-06-17 19:21:40 -04:00
|
|
|
clone._viewOverrides.set(componentType, view);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides the directives from the component {@link ViewMetadata}.
|
2015-05-15 19:42:52 -04:00
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {Type} from
|
|
|
|
* @param {Type} to
|
|
|
|
*
|
|
|
|
* @return {TestComponentBuilder}
|
|
|
|
*/
|
|
|
|
overrideDirective(componentType: Type, from: Type, to: Type): TestComponentBuilder {
|
|
|
|
var clone = this._clone();
|
2015-06-17 19:21:40 -04:00
|
|
|
var overridesForComponent = clone._directiveOverrides.get(componentType);
|
2015-05-15 19:42:52 -04:00
|
|
|
if (!isPresent(overridesForComponent)) {
|
2015-09-29 14:11:06 -04:00
|
|
|
clone._directiveOverrides.set(componentType, new Map<Type, Type>());
|
2015-06-17 19:21:40 -04:00
|
|
|
overridesForComponent = clone._directiveOverrides.get(componentType);
|
2015-05-15 19:42:52 -04:00
|
|
|
}
|
2015-06-17 19:21:40 -04:00
|
|
|
overridesForComponent.set(from, to);
|
2015-05-15 19:42:52 -04:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:14:57 -04:00
|
|
|
/**
|
|
|
|
* Overrides one or more injectables configured via `bindings` metadata property of a directive or
|
|
|
|
* component.
|
|
|
|
* Very useful when certain bindings need to be mocked out.
|
|
|
|
*
|
|
|
|
* The bindings specified via this method are appended to the existing `bindings` causing the
|
|
|
|
* duplicated bindings to
|
|
|
|
* be overridden.
|
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {any[]} bindings
|
|
|
|
*
|
|
|
|
* @return {TestComponentBuilder}
|
|
|
|
*/
|
|
|
|
overrideBindings(type: Type, bindings: any[]): TestComponentBuilder {
|
|
|
|
var clone = this._clone();
|
|
|
|
clone._bindingsOverrides.set(type, bindings);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides one or more injectables configured via `bindings` metadata property of a directive or
|
|
|
|
* component.
|
|
|
|
* Very useful when certain bindings need to be mocked out.
|
|
|
|
*
|
|
|
|
* The bindings specified via this method are appended to the existing `bindings` causing the
|
|
|
|
* duplicated bindings to
|
|
|
|
* be overridden.
|
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {any[]} bindings
|
|
|
|
*
|
|
|
|
* @return {TestComponentBuilder}
|
|
|
|
*/
|
|
|
|
overrideViewBindings(type: Type, bindings: any[]): TestComponentBuilder {
|
|
|
|
var clone = this._clone();
|
|
|
|
clone._viewBindingsOverrides.set(type, bindings);
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
/**
|
|
|
|
* Builds and returns a RootTestComponent.
|
|
|
|
*
|
|
|
|
* @return {Promise<RootTestComponent>}
|
|
|
|
*/
|
|
|
|
createAsync(rootComponentType: Type): Promise<RootTestComponent> {
|
2015-09-08 13:14:57 -04:00
|
|
|
var mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
2015-06-24 04:54:04 -04:00
|
|
|
var mockViewResolver = this._injector.get(ViewResolver);
|
2015-05-15 19:42:52 -04:00
|
|
|
MapWrapper.forEach(this._viewOverrides,
|
2015-06-24 04:54:04 -04:00
|
|
|
(view, type) => { mockViewResolver.setView(type, view); });
|
2015-06-24 05:10:29 -04:00
|
|
|
MapWrapper.forEach(this._templateOverrides,
|
|
|
|
(template, type) => { mockViewResolver.setInlineTemplate(type, template); });
|
2015-05-15 19:42:52 -04:00
|
|
|
MapWrapper.forEach(this._directiveOverrides, (overrides, component) => {
|
|
|
|
MapWrapper.forEach(overrides, (to, from) => {
|
2015-06-24 04:54:04 -04:00
|
|
|
mockViewResolver.overrideViewDirective(component, from, to);
|
2015-05-15 19:42:52 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-08 13:14:57 -04:00
|
|
|
this._bindingsOverrides.forEach((bindings, type) =>
|
|
|
|
mockDirectiveResolver.setBindingsOverride(type, bindings));
|
|
|
|
this._viewBindingsOverrides.forEach(
|
|
|
|
(bindings, type) => mockDirectiveResolver.setViewBindingsOverride(type, bindings));
|
|
|
|
|
2015-05-28 18:02:20 -04:00
|
|
|
var rootElId = `root${_nextRootElementId++}`;
|
|
|
|
var rootEl = el(`<div id="${rootElId}"></div>`);
|
2015-08-11 00:42:47 -04:00
|
|
|
var doc = this._injector.get(DOCUMENT);
|
2015-05-15 19:42:52 -04:00
|
|
|
|
|
|
|
// TODO(juliemr): can/should this be optional?
|
2015-08-06 06:28:36 -04:00
|
|
|
var oldRoots = DOM.querySelectorAll(doc, '[id^=root]');
|
|
|
|
for (var i = 0; i < oldRoots.length; i++) {
|
|
|
|
DOM.remove(oldRoots[i]);
|
|
|
|
}
|
2015-05-15 19:42:52 -04:00
|
|
|
DOM.appendChild(doc.body, rootEl);
|
2015-06-26 18:59:18 -04:00
|
|
|
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
return this._injector.get(DynamicComponentLoader)
|
2015-05-28 18:02:20 -04:00
|
|
|
.loadAsRoot(rootComponentType, `#${rootElId}`, this._injector)
|
2015-05-15 19:42:52 -04:00
|
|
|
.then((componentRef) => { return new RootTestComponent(componentRef); });
|
|
|
|
}
|
|
|
|
}
|